Skip to contentSkip to conversation

fuse: skip lookup during atomic_open() when O_CREAT is set

PATCH In review Jim Harris 6 participants 13 messages Feb 20 – Feb 27

Subsystems (2)
lore
Share & tools
lore

Jim Harris

author
#0 lore raw

PATCH 1/1 fuse: skip lookup during atomic_open() when O_CREAT is set

From: Jim Harris <[email protected]>

When O_CREAT is set, we don't need the lookup. The lookup doesn't harm anything, but it's an extra FUSE operation that's not required.

---
 fs/fuse/dir.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
fs/fuse/dir.c +9 -7
index f25ee47822ad..35f65d49ed2a 100644
@@ -895,7 +895,8 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,
 		goto out_err;
 	}
 	kfree(forget);
-	d_instantiate(entry, inode);
+	d_drop(entry);
+	d_splice_alias(inode, entry);
 	entry->d_time = epoch;
 	fuse_change_entry_timeout(entry, &outentry);
 	fuse_dir_changed(dir);
@@ -936,14 +937,15 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
 	if (fuse_is_bad(dir))
 		return -EIO;
 
-	if (d_in_lookup(entry)) {
-		struct dentry *res = fuse_lookup(dir, entry, 0);
-		if (res || d_really_is_positive(entry))
-			return finish_no_open(file, res);
-	}
+	if (!(flags & O_CREAT)) {
+		if (d_in_lookup(entry)) {
+			struct dentry *res = fuse_lookup(dir, entry, 0);
 
-	if (!(flags & O_CREAT))
+			if (res || d_really_is_positive(entry))
+				return finish_no_open(file, res);
+		}
 		return finish_no_open(file, NULL);
+	}
 
 	/* Only creates */
 	file->f_mode |= FMODE_CREATED;
Signature · 1 line
2.43.0
#1 lore raw

Hi Jim,

Quoted 50 lines · Jim Harris

From: Jim Harris <[email protected]>

When O_CREAT is set, we don't need the lookup. The lookup doesn't harm anything, but it's an extra FUSE operation that's not required.

---
 fs/fuse/dir.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
fs/fuse/dir.c +9 -7
index f25ee47822ad..35f65d49ed2a 100644
@@ -895,7 +895,8 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,
 		goto out_err;
 	}
 	kfree(forget);
-	d_instantiate(entry, inode);
+	d_drop(entry);
+	d_splice_alias(inode, entry);
 	entry->d_time = epoch;
 	fuse_change_entry_timeout(entry, &outentry);
 	fuse_dir_changed(dir);
@@ -936,14 +937,15 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
 	if (fuse_is_bad(dir))
 		return -EIO;
 
-	if (d_in_lookup(entry)) {
-		struct dentry *res = fuse_lookup(dir, entry, 0);
-		if (res || d_really_is_positive(entry))
-			return finish_no_open(file, res);
-	}
+	if (!(flags & O_CREAT)) {
+		if (d_in_lookup(entry)) {
+			struct dentry *res = fuse_lookup(dir, entry, 0);
 
-	if (!(flags & O_CREAT))
+			if (res || d_really_is_positive(entry))
+				return finish_no_open(file, res);
+		}
 		return finish_no_open(file, NULL);
+	}
 
 	/* Only creates */
 	file->f_mode |= FMODE_CREATED;
Signature · 1 line
2.43.0

I have been looking at that code lately a lot since I was planning to replace it with a compound.
I'm not entirely convinced that your proposal is the right direction. I would involve O_EXCL as well, since that lookup could actually help in that case.

Take a look at what Miklos wrote here:
https://lore.kernel.org/linux-fsdevel/CAJfpegsDxsMsyfP4a_5H1q91xFtwcEdu9-WBnzWKwjUSrPNdmw@mail.gmail.com/

Cheers,
Horst

maintainer
#11 lore raw
In reply to Jim Harris
Quoted 37 lines · Jim Harris

From: Jim Harris <[email protected]>

When O_CREAT is set, we don't need the lookup. The lookup doesn't harm anything, but it's an extra FUSE operation that's not required.

---
 fs/fuse/dir.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
fs/fuse/dir.c +5 -6
index f25ee47822ad..35f65d49ed2a 100644
@@ -895,7 +895,8 @@ static int fuse_create_open(struct mnt_idmap *idmap, struct inode *dir,
                goto out_err;
        }
        kfree(forget);
-       d_instantiate(entry, inode);
+       d_drop(entry);
+       d_splice_alias(inode, entry);
        entry->d_time = epoch;
        fuse_change_entry_timeout(entry, &outentry);
        fuse_dir_changed(dir);
@@ -936,14 +937,15 @@ static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
        if (fuse_is_bad(dir))
                return -EIO;
 
-       if (d_in_lookup(entry)) {
-               struct dentry *res = fuse_lookup(dir, entry, 0);
-               if (res || d_really_is_positive(entry))
-                       return finish_no_open(file, res);
-       }
+       if (!(flags & O_CREAT)) {
+               if (d_in_lookup(entry)) {
+                       struct dentry *res = fuse_lookup(dir, entry, 0);

fs/fuse/dir.c static int fuse_atomic_open(struct inode *dir, struct dentry *entry, · line 951

Quoted 10 lines
fs/fuse/dir.c +4 -1
 
-       if (!(flags & O_CREAT))
+                       if (res || d_really_is_positive(entry))
+                               return finish_no_open(file, res);
+               }
                return finish_no_open(file, NULL);
+       }
 
        /* Only creates */
        file->f_mode |= FMODE_CREATED;

Now this is not necessarily true. Setting FMODE_CREATED in case of an existing file will have the effect of not checking permissions to open (think open(path, O_CREAT | O_RDWR, 0444)).

Sad fact is that the same thing could happen with current code: a file created on a remote client during the window between LOOKUP and CREATE.

To prevent that with the current protocol we'd need to add O_EXCL and retry on -EEXIST.

Better fix would be to add a flag to the CREATE reply indicating if the file was created or not.

Thanks,
Miklos

maintainer
#2 lore raw
In reply to Horst Birthelmer
Quoted 8 lines · Horst Birthelmer

I have been looking at that code lately a lot since I was planning to replace it with a compound.
I'm not entirely convinced that your proposal is the right direction. I would involve O_EXCL as well, since that lookup could actually help in that case.

Take a look at what Miklos wrote here:
https://lore.kernel.org/linux-fsdevel/CAJfpegsDxsMsyfP4a_5H1q91xFtwcEdu9-WBnzWKwjUSrPNdmw@mail.gmail.com/

Bernd had actual patches, that got sidetracked unfortunately:

https://lore.kernel.org/all/[email protected]/

Thanks,
Miklos

#3 lore raw
Quoted 8 lines · Miklos Szeredi
Quoted 6 lines · Horst Birthelmer

I have been looking at that code lately a lot since I was planning to replace it with a compound.
I'm not entirely convinced that your proposal is the right direction. I would involve O_EXCL as well, since that lookup could actually help in that case.

Quoted 6 lines
Quoted 2 lines

Take a look at what Miklos wrote here:
https://lore.kernel.org/linux-fsdevel/CAJfpegsDxsMsyfP4a_5H1q91xFtwcEdu9-WBnzWKwjUSrPNdmw@mail.gmail.com/

Bernd had actual patches, that got sidetracked unfortunately:

https://lore.kernel.org/all/[email protected]/

After the discussion about LOOKUO_HANDLE my impression was actually that we want to use compounds for the atomic open.

Thanks,
Bernd

maintainer
#4 lore raw
Quoted 2 lines · Bernd Schubert

After the discussion about LOOKUO_HANDLE my impression was actually that we want to use compounds for the atomic open.

I think we want to introduce an atomic operation that does a lookup + an optional mknod, lets call this LOOKUP_CREATE_FH, this would return a flag indicating whether the file was created or if it existed prior to the operation.

Then, instead of the current CREATE operation there would be a compound with LOOKUP_CREATE_FH + OPEN.

Does that make sense?

Thanks,
Miklos

#12 lore raw
In reply to Jim Harris
Quoted 4 lines · Jim Harris

From: Jim Harris <[email protected]>

When O_CREAT is set, we don't need the lookup. The lookup doesn't harm anything, but it's an extra FUSE operation that's not required.

Problem is that it is a change of behavior - it might cause issues for some fuse server implementations that expect that a node-id was obtained before open of an existing file is done.

Thanks,
Bernd

#5 lore raw
In reply to Miklos Szeredi
Quoted 10 lines · Miklos Szeredi
Quoted 2 lines · Bernd Schubert

After the discussion about LOOKUO_HANDLE my impression was actually that we want to use compounds for the atomic open.

I think we want to introduce an atomic operation that does a lookup + an optional mknod, lets call this LOOKUP_CREATE_FH, this would return a flag indicating whether the file was created or if it existed prior to the operation.

Quoted 4 lines

Then, instead of the current CREATE operation there would be a compound with LOOKUP_CREATE_FH + OPEN.

Does that make sense?

FWIW, what I've been doing is something slightly different. I have been working on implementing MKOBJ_HANDLE+STATX+OPEN. And the plan was to rely on the compound flags, leaving it to user-space to handle the atomicity (e.g. MKOBJ_HANDLE would fail if O_EXCL was set).

I was hoping to share a draft of this very soon, though it's still incomplete. For example, not all required compound operations are already implemented, most notably readdirplus!

Cheers,

Signature · 1 line
Luís
#6 lore raw
In reply to Miklos Szeredi
Quoted 10 lines · Miklos Szeredi
Quoted 2 lines · Bernd Schubert

After the discussion about LOOKUO_HANDLE my impression was actually that we want to use compounds for the atomic open.

I think we want to introduce an atomic operation that does a lookup + an optional mknod, lets call this LOOKUP_CREATE_FH, this would return a flag indicating whether the file was created or if it existed prior to the operation.

Quoted 4 lines

Then, instead of the current CREATE operation there would be a compound with LOOKUP_CREATE_FH + OPEN.

Does that make sense?

Fine with me, but I need to process a bit if we want to that lookup to return attributes or if it should be LOOKUP_CREATE_FH + OPEN + GETATTR. Currently in the wrong time zone to do that today and also not much time in general this week.

Thanks,
Bernd

#7 lore raw
In reply to Miklos Szeredi
Quoted 10 lines · Miklos Szeredi
Quoted 2 lines · Bernd Schubert

After the discussion about LOOKUO_HANDLE my impression was actually that we want to use compounds for the atomic open.

I think we want to introduce an atomic operation that does a lookup + an optional mknod, lets call this LOOKUP_CREATE_FH, this would return a flag indicating whether the file was created or if it existed prior to the operation.

Quoted 4 lines

Then, instead of the current CREATE operation there would be a compound with LOOKUP_CREATE_FH + OPEN.

Does that make sense?

What is wrong with a compound doing LOOKUP + MKNOD + OPEN? If the fuse server knows how to process that 'group' atomically in one big step it will do the right thing,
if not, we will call those in series and sort out the data in kernel afterwards.

If we preserve all flags and the real results we can do pretty much exactly the same thing that is done at the moment with just one call to user space.

That was actually what I was experimenting with.

The MKNOD in the middle is optional depending on the O_CREAT flag.

Quoted 3 lines

Thanks,
Miklos

Cheers,
Horst

maintainer
#8 lore raw
Quoted 6 lines · Horst Birthelmer

What is wrong with a compound doing LOOKUP + MKNOD + OPEN? If the fuse server knows how to process that 'group' atomically in one big step it will do the right thing,
if not, we will call those in series and sort out the data in kernel afterwards.

Quoted 7 lines

If we preserve all flags and the real results we can do pretty much exactly the same thing that is done at the moment with just one call to user space.

That was actually what I was experimenting with.

The MKNOD in the middle is optional depending on the O_CREAT flag.

Okay, I won't stop you experimenting.

My thinking is that it's simpler as a separate op (dir handle and name
are the same for LOOKUP and MKNOD). But adding this special "stop if
error or non-regular, else skip create if positive" dependency would also work.

Thanks,
Miklos

#9 lore raw
Quoted 25 lines · Miklos Szeredi

External email: Use caution opening links or attachments

Quoted 6 lines · Horst Birthelmer

What is wrong with a compound doing LOOKUP + MKNOD + OPEN? If the fuse server knows how to process that 'group' atomically in one big step it will do the right thing,
if not, we will call those in series and sort out the data in kernel afterwards.

Quoted 7 lines

If we preserve all flags and the real results we can do pretty much exactly the same thing that is done at the moment with just one call to user space.

That was actually what I was experimenting with.

The MKNOD in the middle is optional depending on the O_CREAT flag.

Okay, I won't stop you experimenting.

Quoted 7 lines

My thinking is that it's simpler as a separate op (dir handle and name
are the same for LOOKUP and MKNOD). But adding this special "stop if
error or non-regular, else skip create if positive" dependency would also work.

Thanks,
Miklos

Thanks for the feedback everyone. Sounds like compounds will be the way forward to optimize this path, once they are ready.

Do we think compounds will be land for 7.1? Or later?

Best regards,

Jim

#10 lore raw
Quoted 30 lines · Jim Harris
Quoted 30 lines · Miklos Szeredi

External email: Use caution opening links or attachments

Quoted 6 lines · Horst Birthelmer

What is wrong with a compound doing LOOKUP + MKNOD + OPEN? If the fuse server knows how to process that 'group' atomically in one big step it will do the right thing,
if not, we will call those in series and sort out the data in kernel afterwards.

Quoted 7 lines

If we preserve all flags and the real results we can do pretty much exactly the same thing that is done at the moment with just one call to user space.

That was actually what I was experimenting with.

The MKNOD in the middle is optional depending on the O_CREAT flag.

Okay, I won't stop you experimenting.

My thinking is that it's simpler as a separate op (dir handle and name
are the same for LOOKUP and MKNOD). But adding this special "stop if
error or non-regular, else skip create if positive" dependency would also work.

Quoted 6 lines
Quoted 2 lines

Thanks,
Miklos

Thanks for the feedback everyone. Sounds like compounds will be the way forward to optimize this path, once they are ready.

Do we think compounds will be land for 7.1? Or later?

I honestly have no idea. I'm going as fast as I can.
BTW, the post you are responding to, wasn't meant to reject the patch IMHO, but the change in behavior could actually become a real problem.

I have the same fear for the compound of open+getattr, which actually solves a bug, but could be trouble for people making the wrong assumptions in their fuse servers.

Quoted 5 lines

Best regards,

Jim

Cheers,
Horst