use std::io;
use libc::EINVAL;
use remain::sorted;
use thiserror::Error;
use crate::qcow::qcow_raw_file::QcowRawFile;
use crate::qcow::vec_cache::CacheMap;
use crate::qcow::vec_cache::Cacheable;
use crate::qcow::vec_cache::VecCache;
#[sorted]
#[derive(Error, Debug)]
pub enum Error {
#[error("failed to write a refblock from the cache to disk: {0}")]
EvictingRefCounts(io::Error),
#[error("address requested is not within the range of the disk")]
InvalidIndex,
#[error("cluster with addr={0} needs to be read")]
NeedCluster(u64),
#[error("new cluster needs to be allocated for refcounts")]
NeedNewCluster,
#[error("failed to read the file into the refcount cache: {0}")]
ReadingRefCounts(io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub struct RefCount {
ref_table: VecCache<u64>,
refcount_table_offset: u64,
refblock_cache: CacheMap<VecCache<u16>>,
refcount_block_entries: u64, cluster_size: u64,
max_valid_cluster_offset: u64,
}
impl RefCount {
pub fn new(
raw_file: &mut QcowRawFile,
refcount_table_offset: u64,
refcount_table_entries: u64,
refcount_block_entries: u64,
cluster_size: u64,
) -> io::Result<RefCount> {
let ref_table = VecCache::from_vec(raw_file.read_pointer_table(
refcount_table_offset,
refcount_table_entries,
None,
)?);
let max_valid_cluster_index = (ref_table.len() as u64) * refcount_block_entries - 1;
let max_valid_cluster_offset = max_valid_cluster_index * cluster_size;
Ok(RefCount {
ref_table,
refcount_table_offset,
refblock_cache: CacheMap::new(50),
refcount_block_entries,
cluster_size,
max_valid_cluster_offset,
})
}
pub fn refcounts_per_block(&self) -> u64 {
self.refcount_block_entries
}
pub fn max_valid_cluster_offset(&self) -> u64 {
self.max_valid_cluster_offset
}
pub fn set_cluster_refcount(
&mut self,
raw_file: &mut QcowRawFile,
cluster_address: u64,
refcount: u16,
mut new_cluster: Option<(u64, VecCache<u16>)>,
) -> Result<Option<u64>> {
let (table_index, block_index) = self.get_refcount_index(cluster_address);
let block_addr_disk = *self.ref_table.get(table_index).ok_or(Error::InvalidIndex)?;
if !self.refblock_cache.contains_key(&table_index) {
if let Some((addr, table)) = new_cluster.take() {
self.ref_table[table_index] = addr;
let ref_table = &self.ref_table;
self.refblock_cache
.insert(table_index, table, |index, evicted| {
raw_file.write_refcount_block(ref_table[index], evicted.get_values())
})
.map_err(Error::EvictingRefCounts)?;
} else {
if block_addr_disk == 0 {
return Err(Error::NeedNewCluster);
}
return Err(Error::NeedCluster(block_addr_disk));
}
}
let dropped_cluster = if !self.refblock_cache.get(&table_index).unwrap().dirty() {
if let Some((addr, _)) = new_cluster.take() {
self.ref_table[table_index] = addr;
Some(block_addr_disk)
} else {
return Err(Error::NeedNewCluster);
}
} else {
None
};
self.refblock_cache.get_mut(&table_index).unwrap()[block_index] = refcount;
Ok(dropped_cluster)
}
pub fn flush_blocks(&mut self, raw_file: &mut QcowRawFile) -> io::Result<()> {
for (table_index, block) in self.refblock_cache.iter_mut().filter(|(_k, v)| v.dirty()) {
let addr = self.ref_table[*table_index];
if addr != 0 {
raw_file.write_refcount_block(addr, block.get_values())?;
} else {
return Err(std::io::Error::from_raw_os_error(EINVAL));
}
block.mark_clean();
}
Ok(())
}
pub fn flush_table(&mut self, raw_file: &mut QcowRawFile) -> io::Result<bool> {
if self.ref_table.dirty() {
raw_file.write_pointer_table(
self.refcount_table_offset,
self.ref_table.get_values(),
0,
)?;
self.ref_table.mark_clean();
Ok(true)
} else {
Ok(false)
}
}
pub fn get_cluster_refcount(
&mut self,
raw_file: &mut QcowRawFile,
address: u64,
) -> Result<u16> {
let (table_index, block_index) = self.get_refcount_index(address);
let block_addr_disk = *self.ref_table.get(table_index).ok_or(Error::InvalidIndex)?;
if block_addr_disk == 0 {
return Ok(0);
}
if !self.refblock_cache.contains_key(&table_index) {
let table = VecCache::from_vec(
raw_file
.read_refcount_block(block_addr_disk)
.map_err(Error::ReadingRefCounts)?,
);
let ref_table = &self.ref_table;
self.refblock_cache
.insert(table_index, table, |index, evicted| {
raw_file.write_refcount_block(ref_table[index], evicted.get_values())
})
.map_err(Error::EvictingRefCounts)?;
}
Ok(self.refblock_cache.get(&table_index).unwrap()[block_index])
}
fn get_refcount_index(&self, address: u64) -> (usize, usize) {
let block_index = (address / self.cluster_size) % self.refcount_block_entries;
let refcount_table_index = (address / self.cluster_size) / self.refcount_block_entries;
(refcount_table_index as usize, block_index as usize)
}
}