mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
603b5e862c
This is a pre-requisite for the bug fix in the following commit. Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de> On-behalf-of: SAP philipp.schuster@sap.com
203 lines
6.6 KiB
Rust
203 lines
6.6 KiB
Rust
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
use std::io::{self, BufWriter, Seek, SeekFrom, Write};
|
|
use std::mem::size_of;
|
|
use std::os::fd::{AsRawFd, RawFd};
|
|
|
|
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
|
use vmm_sys_util::write_zeroes::WriteZeroes;
|
|
|
|
use super::RawFile;
|
|
|
|
/// A qcow file. Allows reading/writing clusters and appending clusters.
|
|
#[derive(Debug)]
|
|
pub struct QcowRawFile {
|
|
file: RawFile,
|
|
cluster_size: u64,
|
|
cluster_mask: u64,
|
|
}
|
|
|
|
impl QcowRawFile {
|
|
/// Creates a `QcowRawFile` from the given `File`, `None` is returned if `cluster_size` is not
|
|
/// a power of two.
|
|
pub fn from(file: RawFile, cluster_size: u64) -> Option<Self> {
|
|
if !cluster_size.is_power_of_two() {
|
|
return None;
|
|
}
|
|
Some(QcowRawFile {
|
|
file,
|
|
cluster_size,
|
|
cluster_mask: cluster_size - 1,
|
|
})
|
|
}
|
|
|
|
/// Reads `count` 64 bit offsets and returns them as a vector.
|
|
/// `mask` optionally `&`s out some of the bits on the file.
|
|
pub fn read_pointer_table(
|
|
&mut self,
|
|
offset: u64,
|
|
count: u64,
|
|
mask: Option<u64>,
|
|
) -> io::Result<Vec<u64>> {
|
|
let mut table = vec![0; count as usize];
|
|
self.file.seek(SeekFrom::Start(offset))?;
|
|
self.file.read_u64_into::<BigEndian>(&mut table)?;
|
|
if let Some(m) = mask {
|
|
for ptr in &mut table {
|
|
*ptr &= m;
|
|
}
|
|
}
|
|
Ok(table)
|
|
}
|
|
|
|
/// Reads a cluster's worth of 64 bit offsets and returns them as a vector.
|
|
/// `mask` optionally `&`s out some of the bits on the file.
|
|
pub fn read_pointer_cluster(&mut self, offset: u64, mask: Option<u64>) -> io::Result<Vec<u64>> {
|
|
let count = self.cluster_size / size_of::<u64>() as u64;
|
|
self.read_pointer_table(offset, count, mask)
|
|
}
|
|
|
|
/// Internal helper for creating a buffered writer for pointer tables.
|
|
#[inline]
|
|
fn setup_pointer_table_writer<T>(
|
|
&mut self,
|
|
offset: u64,
|
|
entries: &impl Iterator<Item = T>,
|
|
) -> io::Result<BufWriter<RawFile>> {
|
|
self.file.seek(SeekFrom::Start(offset))?;
|
|
let my_file = self.file.try_clone()?;
|
|
let capacity = entries.size_hint().0 * size_of::<u64>();
|
|
Ok(BufWriter::with_capacity(capacity, my_file))
|
|
}
|
|
|
|
/// Writes a pointer table to `offset` in the file.
|
|
/// Entries are computed on-the-fly by the callback.
|
|
pub fn write_pointer_table<'a, T: Copy + 'a>(
|
|
&mut self,
|
|
offset: u64,
|
|
entries: impl Iterator<Item = &'a T>,
|
|
mut f: impl FnMut(&mut QcowRawFile, T) -> io::Result<u64>,
|
|
) -> io::Result<()> {
|
|
let mut buffer = self.setup_pointer_table_writer(offset, &entries)?;
|
|
|
|
for addr in entries {
|
|
let entry = f(self, *addr)?;
|
|
buffer.write_u64::<BigEndian>(entry)?;
|
|
}
|
|
buffer.flush()?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Writes a pointer table directly without transforming values.
|
|
pub fn write_pointer_table_direct<'a>(
|
|
&mut self,
|
|
offset: u64,
|
|
entries: impl Iterator<Item = &'a u64>,
|
|
) -> io::Result<()> {
|
|
let mut buffer = self.setup_pointer_table_writer(offset, &entries)?;
|
|
|
|
for &entry in entries {
|
|
buffer.write_u64::<BigEndian>(entry)?;
|
|
}
|
|
buffer.flush()?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Read a refcount block from the file and returns a Vec containing the block.
|
|
/// Always returns a cluster's worth of data.
|
|
pub fn read_refcount_block(&mut self, offset: u64) -> io::Result<Vec<u16>> {
|
|
let count = self.cluster_size / size_of::<u16>() as u64;
|
|
let mut table = vec![0; count as usize];
|
|
self.file.seek(SeekFrom::Start(offset))?;
|
|
self.file.read_u16_into::<BigEndian>(&mut table)?;
|
|
Ok(table)
|
|
}
|
|
|
|
/// Writes a refcount block to the file.
|
|
pub fn write_refcount_block(&mut self, offset: u64, table: &[u16]) -> io::Result<()> {
|
|
self.file.seek(SeekFrom::Start(offset))?;
|
|
let mut buffer = BufWriter::with_capacity(std::mem::size_of_val(table), &mut self.file);
|
|
|
|
for count in table {
|
|
buffer.write_u16::<BigEndian>(*count)?;
|
|
}
|
|
buffer.flush()?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Allocates a new cluster at the end of the current file, return the address.
|
|
pub fn add_cluster_end(&mut self, max_valid_cluster_offset: u64) -> io::Result<Option<u64>> {
|
|
// Determine where the new end of the file should be and set_len, which
|
|
// translates to truncate(2).
|
|
let file_end: u64 = self.file.seek(SeekFrom::End(0))?;
|
|
let new_cluster_address: u64 = (file_end + self.cluster_size - 1) & !self.cluster_mask;
|
|
|
|
if new_cluster_address > max_valid_cluster_offset {
|
|
return Ok(None);
|
|
}
|
|
|
|
self.file.set_len(new_cluster_address + self.cluster_size)?;
|
|
|
|
Ok(Some(new_cluster_address))
|
|
}
|
|
|
|
/// Returns a mutable reference to the underlying file.
|
|
pub fn file_mut(&mut self) -> &mut RawFile {
|
|
&mut self.file
|
|
}
|
|
|
|
/// Returns the size of the file's clusters.
|
|
pub fn cluster_size(&self) -> u64 {
|
|
self.cluster_size
|
|
}
|
|
|
|
/// Returns the offset of `address` within a cluster.
|
|
pub fn cluster_offset(&self, address: u64) -> u64 {
|
|
address & self.cluster_mask
|
|
}
|
|
|
|
/// Returns the base address of the cluster containing `address`.
|
|
pub fn cluster_address(&self, address: u64) -> u64 {
|
|
address & !self.cluster_mask
|
|
}
|
|
|
|
/// Zeros out a cluster in the file.
|
|
pub fn zero_cluster(&mut self, address: u64) -> io::Result<()> {
|
|
let cluster_size = self.cluster_size as usize;
|
|
self.file.seek(SeekFrom::Start(address))?;
|
|
self.file.write_zeroes(cluster_size)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Writes
|
|
pub fn write_cluster(&mut self, address: u64, data: &[u8]) -> io::Result<()> {
|
|
let cluster_size = self.cluster_size as usize;
|
|
self.file.seek(SeekFrom::Start(address))?;
|
|
self.file.write_all(&data[0..cluster_size])
|
|
}
|
|
|
|
pub fn physical_size(&self) -> Result<u64, std::io::Error> {
|
|
self.file.metadata().map(|m| m.len())
|
|
}
|
|
}
|
|
|
|
impl Clone for QcowRawFile {
|
|
fn clone(&self) -> Self {
|
|
QcowRawFile {
|
|
file: self.file.try_clone().expect("QcowRawFile cloning failed"),
|
|
cluster_size: self.cluster_size,
|
|
cluster_mask: self.cluster_mask,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AsRawFd for QcowRawFile {
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
self.file.as_raw_fd()
|
|
}
|
|
}
|