Преглед изворни кода

Improve error reporting of VirtIOBlk (#52)

* Return IoError from complete_read_block and complete_write_block.

This is consistent with how the blocking versions work.

* Convert different response statuses to different Error variants.
Andrew Walbran пре 2 година
родитељ
комит
66b081ebcf
2 измењених фајлова са 18 додато и 10 уклоњено
  1. 16 10
      src/device/blk.rs
  2. 2 0
      src/lib.rs

+ 16 - 10
src/device/blk.rs

@@ -111,10 +111,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
             &[buf, resp.as_bytes_mut()],
             &mut self.transport,
         )?;
-        match resp.status {
-            RespStatus::OK => Ok(()),
-            _ => Err(Error::IoError),
-        }
+        resp.status.into()
     }
 
     /// Submits a request to read a block, but returns immediately without waiting for the read to
@@ -209,7 +206,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
     ) -> Result<()> {
         self.queue
             .pop_used(token, &[req.as_bytes()], &[buf, resp.as_bytes_mut()])?;
-        Ok(())
+        resp.status.into()
     }
 
     /// Writes the contents of the given buffer to a block.
@@ -228,10 +225,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
             &[resp.as_bytes_mut()],
             &mut self.transport,
         )?;
-        match resp.status {
-            RespStatus::OK => Ok(()),
-            _ => Err(Error::IoError),
-        }
+        resp.status.into()
     }
 
     /// Submits a request to write a block, but returns immediately without waiting for the write to
@@ -291,7 +285,7 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> {
     ) -> Result<()> {
         self.queue
             .pop_used(token, &[req.as_bytes(), buf], &[resp.as_bytes_mut()])?;
-        Ok(())
+        resp.status.into()
     }
 
     /// Fetches the token of the next completed request from the used ring and returns it, without
@@ -393,6 +387,18 @@ impl RespStatus {
     pub const NOT_READY: RespStatus = RespStatus(3);
 }
 
+impl From<RespStatus> for Result {
+    fn from(status: RespStatus) -> Self {
+        match status {
+            RespStatus::OK => Ok(()),
+            RespStatus::IO_ERR => Err(Error::IoError),
+            RespStatus::UNSUPPORTED => Err(Error::Unsupported),
+            RespStatus::NOT_READY => Err(Error::NotReady),
+            _ => Err(Error::IoError),
+        }
+    }
+}
+
 impl Default for BlkResp {
     fn default() -> Self {
         BlkResp {

+ 2 - 0
src/lib.rs

@@ -78,6 +78,8 @@ pub enum Error {
     DmaError,
     /// I/O Error
     IoError,
+    /// The request was not supported by the device.
+    Unsupported,
     /// The config space advertised by the device is smaller than the driver expected.
     ConfigSpaceTooSmall,
     /// The device doesn't have any config space, but the driver expects some.