Out of order read() completion and buffer filling beyond returned amount
Subsystems (6)
Hi Al, Linus,
Do you have an opinion on whether it's permissible for a filesystem to write into the read() buffer beyond the amount it claims to return, though still within the specified size of the buffer?
I'm working on common DIO routines for 9p, afs, ceph and cifs in netfs lib, and I can see that at least three of those four filesystems either can or must split a read, possibly being required to distribute across multiple servers.
If a filesystem was to emit multiple read RPCs in parallel, there is the possibility that they would complete out of order - particularly if they go to multiple servers.
Would it be a violation of the way the read() family of syscalls work to write the data into the buffers out of order, and then abandon the extra data written at the end if one of the RPCs returned a short read? We would have clobbered some of the buffer that we haven't said we've modified.
For buffered reads, it's not a problem as we can fill the pagecache out of order with no issue.
David
Quoted 4 lines · David Howells
Do you have an opinion on whether it's permissible for a filesystem to write into the
read()buffer beyond the amount it claims to return, though still within the specified size of the buffer?
I'm pretty sure that would seriously violate POSIX in the general case, and maybe even break some programs that do fancy buffer management (ie I could imagine some circular buffer thing that expects any "unwritten" ('unread'?) parts to stay with the old contents)
That said, that's for generic 'read()' cases for things like tty's or pipes etc that can return partial reads in the first place.
If it's a regular file, then any partial read *already* violates POSIX, and nobody sane would do any such buffer management because it's supposed to be a 'can't happen' thing.
And since you mention DIO, that's doubly true, and is already outside basic POSIX, and has already violated things like "all or nothing" rules for visibility of writes-vs-reads (which admittedly most Linux filesystems have violated even outside of DIO, since the strictest reading of the rules are incredibly nasty anyway). But filesystems like XFS which took some of the strict rules more seriously already ignored them for DIO, afaik.
So I suspect you're fine. Buffered reads might care more, but even there the whole "you can't really validly have partial reads anyway" thing is a bigger violation to begin with.
With DIO, I suspect nobody cares about _those_ kinds of semantic details. People who use DIO tend to care primarily about performance - it's why they use it, after all - and are probably more than happy to be lax about other rules.
But maybe somebody would prefer to have a mount option to specify just how out-of-spec things can be (ie like the traditional old nfs 'intr' thing). If only for testing, and for 'in case some odd app breaks'
Linus
Quoted 18 lines · Linus Torvalds
Quoted 4 lines · David Howells
Do you have an opinion on whether it's permissible for a filesystem to write into the
read()buffer beyond the amount it claims to return, though still within the specified size of the buffer?I'm pretty sure that would seriously violate POSIX in the general case, and maybe even break some programs that do fancy buffer management (ie I could imagine some circular buffer thing that expects any "unwritten" ('unread'?) parts to stay with the old contents)
That said, that's for generic '
read()' cases for things like tty's or pipes etc that can return partial reads in the first place.If it's a regular file, then any partial read *already* violates POSIX, and nobody sane would do any such buffer management because it's supposed to be a 'can't happen' thing.
Quoted 7 lines
And since you mention DIO, that's doubly true, and is already outside basic POSIX, and has already violated things like "all or nothing" rules for visibility of writes-vs-reads (which admittedly most Linux filesystems have violated even outside of DIO, since the strictest reading of the rules are incredibly nasty anyway). But filesystems like XFS which took some of the strict rules more seriously already ignored them for DIO, afaik.
I think for DIO, you're sacrificing the entire buffer with any filesystem. If the underlying file is split across multiple drives, or is even just fragmented on a single drive, we'll submit multiple BIOs which will complete independently (even for SCSI which writes sequentially; never mind NVMe which can DMA blocks asynchronously). It might be more apparent in a networking situation where errors are more common, but it's always been a possibility since Linux introduced DIO.
Quoted 7 lines · Matthew Wilcox
I think for DIO, you're sacrificing the entire buffer with any filesystem. If the underlying file is split across multiple drives, or is even just fragmented on a single drive, we'll submit multiple BIOs which will complete independently (even for SCSI which writes sequentially; never mind NVMe which can DMA blocks asynchronously). It might be more apparent in a networking situation where errors are more common, but it's always been a possibility since Linux introduced DIO.
Yes. Probably because of that we also never allow short reads or writes due to I/O errrors but always fail the whole I/O.