diff --git a/tokio/src/fs/write.rs b/tokio/src/fs/write.rs index 26e5e749e..01c9bebc8 100644 --- a/tokio/src/fs/write.rs +++ b/tokio/src/fs/write.rs @@ -57,20 +57,18 @@ async fn write_uring(path: &Path, mut buf: OwnedBuf) -> io::Result<()> { .into(); let total: usize = buf.as_ref().len(); - let mut offset: usize = 0; - while offset < total { - // There is a cap on how many bytes we can write in a single uring write operation. - // ref: https://github.com/axboe/liburing/discussions/497 - let len = std::cmp::min(total - offset, u32::MAX as usize) as u32; - - let (n, _buf, _fd) = Op::write_at(fd, buf, offset, len, offset as u64)?.await?; + let mut buf_offset: usize = 0; + let mut file_offset: u64 = 0; + while buf_offset < total { + let (n, _buf, _fd) = Op::write_at(fd, buf, buf_offset, file_offset)?.await?; if n == 0 { return Err(io::ErrorKind::WriteZero.into()); } buf = _buf; fd = _fd; - offset += n as usize; + buf_offset += n as usize; + file_offset += n as u64; } Ok(()) diff --git a/tokio/src/io/uring/write.rs b/tokio/src/io/uring/write.rs index 8973fe7a9..92acae947 100644 --- a/tokio/src/io/uring/write.rs +++ b/tokio/src/io/uring/write.rs @@ -28,15 +28,16 @@ impl Cancellable for Write { } impl Op { - /// Issue a write that starts at `buf_offset` within `buf` and writes `len` bytes + /// Issue a write that starts at `buf_offset` within `buf` and writes some bytes /// into `file` at `file_offset`. pub(crate) fn write_at( fd: OwnedFd, buf: OwnedBuf, buf_offset: usize, - len: u32, file_offset: u64, ) -> io::Result { + let len = u32::try_from(buf.as_ref().len() - buf_offset).unwrap_or(u32::MAX); + let ptr = buf.as_ref()[buf_offset..buf_offset + len as usize].as_ptr(); let sqe = opcode::Write::new(types::Fd(fd.as_raw_fd()), ptr, len)