Skip to main content

imago/qcow2/
io_func.rs

1//! Special I/O functions.
2//!
3//! Most of I/O should be implemented in the generic
4//! [`imago::format::access`](crate::format::access) module, but some I/O needs to be done directly
5//! by image drivers (like handling compression).
6
7use super::*;
8use crate::io_buffers::IoBuffer;
9
10#[maybe_async]
11impl<S: Storage, F: WrappedFormat<S>> Qcow2<S, F> {
12    /// Read the special range at `offset`.
13    ///
14    /// Currently, the only special range we have are compressed clusters.
15    pub(super) async fn do_readv_special(
16        &self,
17        mut bufv: IoVectorMut<'_>,
18        mut offset: GuestOffset,
19    ) -> io::Result<()> {
20        let mut saved_l2_table: Option<Arc<L2Table>> = None;
21        let cb = self.header.cluster_bits();
22
23        // Do everything cluster by cluster.
24        while !bufv.is_empty() {
25            let l2_table = if let Some(saved) = saved_l2_table.as_ref() {
26                saved
27            } else {
28                let new_l2 = self
29                    .get_l2(offset, false)
30                    .await?
31                    .ok_or(io::ErrorKind::Other)?;
32                saved_l2_table.get_or_insert(new_l2)
33            };
34
35            let chunk_length = offset.remaining_in_cluster(cb);
36            let (chunk, remainder) = bufv.split_at(chunk_length);
37            bufv = remainder;
38
39            let mut bounce_buffer_and_chunk = None;
40            let need_bounce_buffer = chunk.buffer_count() != 1
41                || offset.in_cluster_offset(cb) != 0
42                || chunk.len() != self.header.cluster_size() as u64;
43
44            let slice = if need_bounce_buffer {
45                let bounce_buffer = IoBuffer::new(self.header.cluster_size(), 1)?;
46                bounce_buffer_and_chunk = Some((bounce_buffer, chunk));
47                bounce_buffer_and_chunk.as_mut().unwrap().0.as_mut()
48            } else {
49                chunk.into_inner().pop().unwrap().into()
50            };
51
52            let guest_cluster = offset.cluster(cb);
53            match l2_table.get_mapping(guest_cluster)? {
54                L2Mapping::Compressed {
55                    host_offset,
56                    length,
57                } => {
58                    self.read_compressed_cluster(slice.into_slice(), host_offset, length)
59                        .await?;
60                }
61
62                _ => return Err(io::ErrorKind::Other.into()),
63            }
64
65            if let Some((bounce_buffer, mut chunk)) = bounce_buffer_and_chunk {
66                let ofs = offset.in_cluster_offset(cb);
67                let end = ofs + chunk.len() as usize;
68                chunk.copy_from_slice(bounce_buffer.as_ref_range(ofs..end).into_slice());
69            }
70
71            let next_cluster = if let Some(next) = guest_cluster.next_in_l2(cb) {
72                next
73            } else {
74                saved_l2_table.take();
75                guest_cluster.first_in_next_l2(cb)
76            };
77            offset = next_cluster.offset(cb);
78        }
79
80        Ok(())
81    }
82}