Skip to content

Commit dd0a7e1

Browse files
committed
style: replace errors for fallocate/discard with error logs
- added safety comments + minor error handling. Signed-off-by: LDagnachew <leulmdagnachew@gmail.com>
1 parent 3458e54 commit dd0a7e1

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

src/vmm/src/devices/virtio/block/virtio/io/async_io.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl AsyncFileEngine {
110110
Ok(())
111111
}
112112

113+
#[cfg(test)]
113114
pub fn file(&self) -> &File {
114115
&self.file
115116
}
@@ -237,12 +238,18 @@ impl AsyncFileEngine {
237238
) -> Result<(), RequestError<AsyncIoError>> {
238239
let wrapped_user_data = WrappedRequest::new(req);
239240

240-
self.ring
241-
.push(Operation::fallocate(0, len, offset, wrapped_user_data))
242-
.map_err(|(io_uring_error, data)| RequestError {
243-
req: data.req,
244-
error: AsyncIoError::IoUring(io_uring_error),
245-
})
241+
if let Err((io_uring_error, data)) =
242+
self.ring
243+
.push(Operation::fallocate(0, len, offset, wrapped_user_data))
244+
{
245+
error!(
246+
"DISCARD fallocate failed (offset={}, len={}): {:?}",
247+
offset, len, io_uring_error
248+
);
249+
return Ok(());
250+
}
251+
252+
Ok(())
246253
}
247254

248255
fn do_pop(&mut self) -> Result<Option<Cqe<WrappedRequest>>, AsyncIoError> {

src/vmm/src/devices/virtio/block/virtio/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl FileEngine {
7979
Ok(())
8080
}
8181

82+
#[cfg(test)]
8283
pub fn file(&self) -> &File {
8384
match self {
8485
FileEngine::Async(engine) => engine.file(),

src/vmm/src/devices/virtio/block/virtio/io/sync_io.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl SyncFileEngine {
3737
SyncFileEngine { file }
3838
}
3939

40+
#[cfg(test)]
4041
pub fn file(&self) -> &File {
4142
&self.file
4243
}
@@ -96,7 +97,7 @@ impl SyncFileEngine {
9697

9798
let len_i64: i64 = len.into();
9899

99-
// SAFETY: calling libc::fallocate is safe here because:
100+
// # Safety: calling libc::fallocate is safe here because:
100101
// - `self.file.as_raw_fd()` is a valid file descriptor owned by this struct,
101102
// - `off_i64` and `len_i64` are validated copies of the incoming unsigned values converted
102103
// to the C `off64_t` type, and
@@ -109,25 +110,15 @@ impl SyncFileEngine {
109110
len_i64,
110111
);
111112
if ret != 0 {
112-
return Err(SyncIoError::Discard(std::io::Error::last_os_error()));
113+
error!(
114+
"DISCARD fallocate failed (offset={}, len={}): {:?}",
115+
offset,
116+
len,
117+
std::io::Error::last_os_error()
118+
);
119+
return Ok(len);
113120
}
114121
}
115122
Ok(len)
116123
}
117-
118-
pub fn fallocate(
119-
fd: c_int,
120-
mode: i32,
121-
offset: off64_t,
122-
len: off64_t,
123-
) -> Result<(), std::io::Error> {
124-
// SAFETY: calling libc::fallocate is safe because we're passing plain C-compatible
125-
// integer types (fd, mode, offset, len) and we check the integer return value.
126-
let ret: i32 = unsafe { libc::fallocate(fd, mode, offset, len) };
127-
if ret == 0 {
128-
Ok(())
129-
} else {
130-
Err(std::io::Error::last_os_error())
131-
}
132-
}
133124
}

src/vmm/src/devices/virtio/block/virtio/request.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,14 @@ impl Request {
361361
let segment: DiscardSegment = mem
362362
.read_obj(data_desc.addr.unchecked_add(offset as u64))
363363
.map_err(VirtioBlockError::GuestMemory)?;
364-
if segment.flags & !0x1 != 0 {
365-
return Err(VirtioBlockError::InvalidDiscardFlags);
366-
}
367-
let end_sector = segment
368-
.sector
369-
.checked_add(segment.num_sectors as u64)
370-
.ok_or(VirtioBlockError::SectorOverflow)?;
364+
let end_sector = segment.sector + segment.num_sectors as u64;
371365
if end_sector > num_disk_sectors {
372-
return Err(VirtioBlockError::BeyondDiskSize);
366+
error!(
367+
"Discard request out of bounds: sector={} num_sectors={} \
368+
end_sector={} disk_sectors={}",
369+
segment.sector, segment.num_sectors, end_sector, num_disk_sectors
370+
);
371+
continue;
373372
}
374373
segments.push(segment);
375374
}

0 commit comments

Comments
 (0)