# VirtIOBlk in virtio_drivers - Rust
The virtio block device is a simple virtual block device (ie. disk).
Keywords: rust, rustlang, rust-lang, VirtIOBlk

# Struct virtio_drivers::VirtIOBlk

source · [−]

    pub struct VirtIOBlk<'a> { /* private fields */ }

Expand description

The virtio block device is a simple virtual block device (ie. disk).

Read and write requests (and other exotic requests) are placed in the queue, and serviced (probably out of order) by the device except where noted.

## Implementations

source

### impl VirtIOBlk<'_>

source

#### pub fn new(header: &'static mut VirtIOHeader) -> Result<Self>

Create a new VirtIO-Blk driver.

source

#### pub fn ack_interrupt(&mut self) -> bool

Acknowledge interrupt.

source

#### pub fn read_block(&mut self, block_id: usize, buf: &mut [ u8 ]) -> Result

Read a block.

source

#### pub unsafe fn read_block_nb(
&mut self,
block_id: usize,
buf: &mut [ u8 ],
resp: &mut BlkResp
) -> Result<u16>

Read a block in a non-blocking way which means that it returns immediately.

##### Arguments

- `block_id` - The identifier of the block to read.
- `buf` - The buffer in the memory which the block is read into.
- `resp` - A mutable reference to a variable provided by the caller which contains the status of the requests. The caller can safely read the variable only after the request is ready.

##### Usage

It will submit request to the virtio block device and return a token identifying the position of the first Descriptor in the chain. If there are not enough Descriptors to allocate, then it returns Error::BufferTooSmall.

After the request is ready, `resp` will be updated and the caller can get the status of the request(e.g. succeed or failed) through it. However, the caller must not spin on `resp` to wait for it to change. A safe way is to read it after the same token as this method returns is fetched through VirtIOBlk::pop_used(), which means that the request has been ready.

##### Safety

`buf` is still borrowed by the underlying virtio block device even if this method returns. Thus, it is the caller’s responsibility to guarantee that `buf` is not accessed before the request is completed in order to avoid data races.

source

#### pub fn write_block(&mut self, block_id: usize, buf: &[ u8 ]) -> Result

Write a block.

source

#### pub unsafe fn write_block_nb(
&mut self,
block_id: usize,
buf: &[ u8 ],
resp: &mut BlkResp
) -> Result<u16>

##### Arguments

- `block_id` - The identifier of the block to write.
- `buf` - The buffer in the memory containing the data to write to the block.
- `resp` - A mutable reference to a variable provided by the caller which contains the status of the requests. The caller can safely read the variable only after the request is ready.

##### Usage

See also VirtIOBlk::read_block_nb().

##### Safety

See also VirtIOBlk::read_block_nb().

source

#### pub fn pop_used(&mut self) -> Result<u16>

During an interrupt, it fetches a token of a completed request from the used ring and return it. If all completed requests have already been fetched, return Err(Error::NotReady).

source

#### pub fn virt_queue_size(&self) -> u16

Return size of its VirtQueue. It can be used to tell the caller how many channels he should monitor on.

## Auto Trait Implementations

### impl<'a> RefUnwindSafe for VirtIOBlk<'a>

### impl<'a> Send for VirtIOBlk<'a>

### impl<'a> Sync for VirtIOBlk<'a>

### impl<'a> Unpin for VirtIOBlk<'a>

### impl<'a> !UnwindSafe for VirtIOBlk<'a>

## Blanket Implementations

source

### impl<T> Any for T where
T: 'static + ?Sized,

source

#### fn type_id(&self) -> TypeId

Gets the `TypeId` of `self`. Read more

source

### impl<T> Borrow<T> for T where
T: ?Sized,

const: unstable · source

#### fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more

source

### impl<T> BorrowMut<T> for T where
T: ?Sized,

const: unstable · source

#### fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more

source

### impl<T> From<T> for T

const: unstable · source

#### fn from(t: T) -> T

Returns the argument unchanged.

source

### impl<T, U> Into<U> for T where
U: From<T>,

const: unstable · source

#### fn into(self) -> U

Calls `U::from(self)`.

That is, this conversion is whatever the implementation of `From<T> for U` chooses to do.

source

### impl<T, U> TryFrom<U> for T where
U: Into<T>,

#### type Error = Infallible

The type returned in the event of a conversion error.

const: unstable · source

#### fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.

source

### impl<T, U> TryInto<U> for T where
U: TryFrom<T>,

#### type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

const: unstable · source

#### fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.