Skip to content

Commit 6820ada

Browse files
retragerbradford
authored andcommitted
block: Improve block device request error handling
The current implementation of `VirtioBlockDevice::request()` panics if `data` is `None` or `data` size is not equal to sector size. This commit removes the panics and adds error handlings. Signed-off-by: Akira Moroo <retrage01@gmail.com>
1 parent 5bc8086 commit 6820ada

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/block.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ struct DriverState {
7777

7878
#[derive(Debug, PartialEq, Eq)]
7979
pub enum Error {
80-
BlockIOError,
80+
BlockIO,
81+
NoDataBuf,
82+
InvalidDataBufSize,
8183

8284
BlockNotSupported,
8385
}
@@ -236,10 +238,6 @@ impl<'a> VirtioBlockDevice<'a> {
236238
data: Option<&mut [u8]>,
237239
request: RequestType,
238240
) -> Result<(), Error> {
239-
if request != RequestType::Flush {
240-
assert_eq!(SectorBuf::len(), data.as_ref().unwrap().len());
241-
}
242-
243241
const VIRTQ_DESC_F_NEXT: u16 = 1;
244242
const VIRTQ_DESC_F_WRITE: u16 = 2;
245243

@@ -268,8 +266,18 @@ impl<'a> VirtioBlockDevice<'a> {
268266
let mut d = &mut state.descriptors[next_desc];
269267
let next_desc = (next_desc + 1) % QUEUE_SIZE;
270268
if request != RequestType::Flush {
271-
d.addr = data.unwrap().as_ptr() as u64;
272-
d.length = SectorBuf::len() as u32;
269+
match data {
270+
None => {
271+
return Err(Error::NoDataBuf);
272+
}
273+
Some(data) => {
274+
if data.len() != SectorBuf::len() {
275+
return Err(Error::InvalidDataBufSize);
276+
}
277+
d.addr = data.as_ptr() as u64;
278+
d.length = SectorBuf::len() as u32;
279+
}
280+
}
273281
}
274282

275283
d.flags = VIRTQ_DESC_F_NEXT
@@ -306,7 +314,7 @@ impl<'a> VirtioBlockDevice<'a> {
306314

307315
match footer.status {
308316
VIRTIO_BLK_S_OK => Ok(()),
309-
VIRTIO_BLK_S_IOERR => Err(Error::BlockIOError),
317+
VIRTIO_BLK_S_IOERR => Err(Error::BlockIO),
310318
VIRTIO_BLK_S_UNSUPP => Err(Error::BlockNotSupported),
311319
_ => Err(Error::BlockNotSupported),
312320
}

src/fat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl<'a> Read for File<'a> {
554554
impl<'a> SectorRead for Filesystem<'a> {
555555
fn read(&self, sector: u64, data: &mut [u8]) -> Result<(), crate::block::Error> {
556556
if self.start + sector > self.last {
557-
Err(crate::block::Error::BlockIOError)
557+
Err(crate::block::Error::BlockIO)
558558
} else {
559559
self.device.read(self.start + sector, data)
560560
}

src/part.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ pub mod tests {
182182
let mut file = self.file.borrow_mut();
183183
match file.seek(SeekFrom::Start(sector * SectorBuf::len() as u64)) {
184184
Ok(_) => {}
185-
Err(_) => return Err(block::Error::BlockIOError),
185+
Err(_) => return Err(block::Error::BlockIO),
186186
}
187187
match file.read(data) {
188188
Ok(_) => {}
189-
Err(_) => return Err(block::Error::BlockIOError),
189+
Err(_) => return Err(block::Error::BlockIO),
190190
}
191191
Ok(())
192192
}

0 commit comments

Comments
 (0)