Skip to contentSkip to conversation

fuse: fix possible write position calculation error

PATCH In review Peng Hao 2 participants 5 messages Nov 3, 2021 – Nov 11, 2021

Subsystems (2)
lore
Share & tools
lore

Peng Hao

author
#0 lore raw

PATCH 1/1 fuse: fix possible write position calculation error

The 'written' that generic_file_direct_write return through filemap_write_and_wait_range is not necessarily sequential, and its iocb->ki_pos has not been updated.

---
 fs/fuse/file.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/fuse/file.c +2 -4
index 26730e699d68..52aaa1fb484d 100644
@@ -1314,14 +1314,12 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
 		goto out;
 
 	if (iocb->ki_flags & IOCB_DIRECT) {
-		loff_t pos = iocb->ki_pos;
+		loff_t pos;
 		written = generic_file_direct_write(iocb, from);
 		if (written < 0 || !iov_iter_count(from))
 			goto out;
 
-		pos += written;
-
-		written_buffered = fuse_perform_write(iocb, mapping, from, pos);
+		written_buffered = fuse_perform_write(iocb, mapping, from, pos = iocb->ki_pos);
 		if (written_buffered < 0) {
 			err = written_buffered;
 			goto out;
Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 4 lines · Peng Hao

The 'written' that generic_file_direct_write return through filemap_write_and_wait_range is not necessarily sequential, and its iocb->ki_pos has not been updated.

I don't see the bug, but maybe I'm missing something. Can you please explain in detail?

The patch looks good as a cleanup, but I'd very much like to know first if it fixes a bug or not.

Thanks,
Miklos

author
#2 lore raw
Quoted 10 lines · Miklos Szeredi
Quoted 4 lines · Peng Hao

The 'written' that generic_file_direct_write return through filemap_write_and_wait_range is not necessarily sequential, and its iocb->ki_pos has not been updated.

I don't see the bug, but maybe I'm missing something. Can you please explain in detail?

I think we shouldn't add "written" to variable pos. generic_file_direct_write:
....
written = filemap_write_and_wait_range(mapping, pos,
pos + write_len - 1);
if (written) //the number of writes here reflects the
amount of writeback data

                                 // in the previous page cache, and
iocb->ki_pos do not change and
                                //  no data in iocb is written.
                        goto out;
                ...
       out:
              return written;
Quoted 3 lines

The patch looks good as a cleanup, but I'd very much like to know first if it fixes a bug or not.

When the direct read & write and cache read & write of different processes, there is some synchronization in the user mode, but it is still found that there
is data out of synchronization. Just analyze that there may be problems here. Thanks.

Quoted 2 lines

Thanks,
Miklos

maintainer
#3 lore raw
Quoted 12 lines · Hao Peng
Quoted 10 lines · Miklos Szeredi
Quoted 4 lines · Peng Hao

The 'written' that generic_file_direct_write return through filemap_write_and_wait_range is not necessarily sequential, and its iocb->ki_pos has not been updated.

I don't see the bug, but maybe I'm missing something. Can you please explain in detail?

Quoted 7 lines

I think we shouldn't add "written" to variable pos. generic_file_direct_write:
....
written = filemap_write_and_wait_range(mapping, pos,
pos + write_len - 1);
if (written) //the number of writes here reflects the
amount of writeback data

No. It's actually an error code in this case.

It is confusing, though, so I guess cleaning this up (e.g. rename "written" to "retval") would make sense.

Thanks,
Miklos

author
#4 lore raw
Quoted 22 lines · Miklos Szeredi
Quoted 12 lines · Hao Peng
Quoted 10 lines · Miklos Szeredi
Quoted 4 lines · Peng Hao

The 'written' that generic_file_direct_write return through filemap_write_and_wait_range is not necessarily sequential, and its iocb->ki_pos has not been updated.

I don't see the bug, but maybe I'm missing something. Can you please explain in detail?

Quoted 7 lines

I think we shouldn't add "written" to variable pos. generic_file_direct_write:
....
written = filemap_write_and_wait_range(mapping, pos,
pos + write_len - 1);
if (written) //the number of writes here reflects the
amount of writeback data

Quoted 5 lines

No. It's actually an error code in this case.

It is confusing, though, so I guess cleaning this up (e.g. rename "written" to "retval") would make sense.

oh,sorry.
I misunderstood. I should deeply analyze the function called below.

Quoted 2 lines

Thanks,
Miklos