2018-12-31 16:30:08 +00:00
|
|
|
use std::fs::File;
|
2020-06-23 10:09:49 +00:00
|
|
|
use std::io::{self, BufWriter, Seek, SeekFrom, Write};
|
2020-03-23 14:03:18 +00:00
|
|
|
use std::ops::Range;
|
2018-12-31 16:30:08 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-08-13 10:59:03 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-06-23 10:09:49 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2020-06-24 09:57:12 +00:00
|
|
|
use std::task::Context;
|
2020-06-23 10:09:49 +00:00
|
|
|
use std::pin::Pin;
|
2019-08-13 10:59:03 +00:00
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, format_err, Error};
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-07-01 08:44:03 +00:00
|
|
|
use proxmox::tools::io::ReadExt;
|
2019-11-14 09:18:31 +00:00
|
|
|
use proxmox::tools::uuid::Uuid;
|
2020-06-12 11:57:56 +00:00
|
|
|
use proxmox::tools::mmap::Mmap;
|
2020-06-24 09:57:12 +00:00
|
|
|
use pxar::accessor::{MaybeReady, ReadAt, ReadAtOperation};
|
2019-05-22 13:02:16 +00:00
|
|
|
|
2019-08-13 10:59:03 +00:00
|
|
|
use super::chunk_stat::ChunkStat;
|
|
|
|
use super::chunk_store::ChunkStore;
|
2020-03-23 14:03:18 +00:00
|
|
|
use super::index::ChunkReadInfo;
|
2019-08-13 10:59:03 +00:00
|
|
|
use super::read_chunk::ReadChunk;
|
2019-11-14 09:18:31 +00:00
|
|
|
use super::Chunker;
|
|
|
|
use super::IndexFile;
|
2019-10-06 08:31:06 +00:00
|
|
|
use super::{DataBlob, DataChunkBuilder};
|
2020-09-12 13:10:47 +00:00
|
|
|
use crate::tools;
|
2019-06-13 09:47:23 +00:00
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
/// Header format definition for dynamic index files (`.dixd`)
|
2018-12-31 16:30:08 +00:00
|
|
|
#[repr(C)]
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct DynamicIndexHeader {
|
2019-06-14 12:58:37 +00:00
|
|
|
pub magic: [u8; 8],
|
2018-12-31 16:30:08 +00:00
|
|
|
pub uuid: [u8; 16],
|
2020-09-12 13:10:47 +00:00
|
|
|
pub ctime: i64,
|
2019-06-14 06:57:52 +00:00
|
|
|
/// Sha256 over the index ``SHA256(offset1||digest1||offset2||digest2||...)``
|
|
|
|
pub index_csum: [u8; 32],
|
2019-08-23 09:38:16 +00:00
|
|
|
reserved: [u8; 4032], // overall size is one page (4096 bytes)
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
2020-01-21 11:28:01 +00:00
|
|
|
proxmox::static_assert_size!(DynamicIndexHeader, 4096);
|
2019-08-23 09:38:16 +00:00
|
|
|
// TODO: Once non-Copy unions are stabilized, use:
|
|
|
|
// union DynamicIndexHeader {
|
|
|
|
// reserved: [u8; 4096],
|
|
|
|
// pub data: DynamicIndexHeaderData,
|
|
|
|
// }
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2020-08-17 09:50:32 +00:00
|
|
|
impl DynamicIndexHeader {
|
|
|
|
/// Convenience method to allocate a zero-initialized header struct.
|
|
|
|
pub fn zeroed() -> Box<Self> {
|
|
|
|
unsafe {
|
|
|
|
Box::from_raw(std::alloc::alloc_zeroed(std::alloc::Layout::new::<Self>()) as *mut Self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
unsafe {
|
|
|
|
std::slice::from_raw_parts(
|
|
|
|
self as *const Self as *const u8,
|
|
|
|
std::mem::size_of::<Self>(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 11:57:56 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct DynamicEntry {
|
2020-06-15 07:19:35 +00:00
|
|
|
end_le: u64,
|
2020-06-12 11:57:56 +00:00
|
|
|
digest: [u8; 32],
|
|
|
|
}
|
|
|
|
|
2020-06-15 07:19:35 +00:00
|
|
|
impl DynamicEntry {
|
|
|
|
#[inline]
|
|
|
|
pub fn end(&self) -> u64 {
|
|
|
|
u64::from_le(self.end_le)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct DynamicIndexReader {
|
2019-01-18 15:50:15 +00:00
|
|
|
_file: File,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub size: usize,
|
2020-06-12 11:57:56 +00:00
|
|
|
index: Mmap<DynamicEntry>,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
2020-09-12 13:10:47 +00:00
|
|
|
pub ctime: i64,
|
2019-06-14 06:57:52 +00:00
|
|
|
pub index_csum: [u8; 32],
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl DynamicIndexReader {
|
2019-06-28 14:35:00 +00:00
|
|
|
pub fn open(path: &Path) -> Result<Self, Error> {
|
2019-07-04 06:17:30 +00:00
|
|
|
File::open(path)
|
|
|
|
.map_err(Error::from)
|
2020-06-12 11:57:56 +00:00
|
|
|
.and_then(Self::new)
|
2019-07-04 06:17:30 +00:00
|
|
|
.map_err(|err| format_err!("Unable to open dynamic index {:?} - {}", path, err))
|
2019-06-28 14:35:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(mut file: std::fs::File) -> Result<Self, Error> {
|
2019-11-14 09:18:31 +00:00
|
|
|
if let Err(err) =
|
|
|
|
nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock)
|
|
|
|
{
|
2019-07-04 06:17:30 +00:00
|
|
|
bail!("unable to get shared lock - {}", err);
|
|
|
|
}
|
|
|
|
|
2020-06-12 11:57:56 +00:00
|
|
|
// FIXME: This is NOT OUR job! Check the callers of this method and remove this!
|
2019-06-28 14:35:00 +00:00
|
|
|
file.seek(SeekFrom::Start(0))?;
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
let header_size = std::mem::size_of::<DynamicIndexHeader>();
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-09-11 12:50:45 +00:00
|
|
|
let header: Box<DynamicIndexHeader> = unsafe { file.read_host_value_boxed()? };
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-06-14 12:58:37 +00:00
|
|
|
if header.magic != super::DYNAMIC_SIZED_CHUNK_INDEX_1_0 {
|
2019-06-28 14:35:00 +00:00
|
|
|
bail!("got unknown magic number");
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
let ctime = proxmox::tools::time::epoch_i64();
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
let rawfd = file.as_raw_fd();
|
|
|
|
|
2019-06-28 14:35:00 +00:00
|
|
|
let stat = nix::sys::stat::fstat(rawfd)?;
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
let size = stat.st_size as usize;
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let index_size = size - header_size;
|
2020-06-12 11:57:56 +00:00
|
|
|
let index_count = index_size / 40;
|
|
|
|
if index_count * 40 != index_size {
|
2019-06-28 14:35:00 +00:00
|
|
|
bail!("got unexpected file size");
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 11:57:56 +00:00
|
|
|
let index = unsafe {
|
|
|
|
Mmap::map_fd(
|
|
|
|
rawfd,
|
|
|
|
header_size as u64,
|
|
|
|
index_count,
|
2019-11-14 09:18:31 +00:00
|
|
|
nix::sys::mman::ProtFlags::PROT_READ,
|
|
|
|
nix::sys::mman::MapFlags::MAP_PRIVATE,
|
2020-06-12 11:57:56 +00:00
|
|
|
)?
|
|
|
|
};
|
2019-01-02 13:27:04 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
2019-01-18 15:50:15 +00:00
|
|
|
_file: file,
|
2019-01-02 13:27:04 +00:00
|
|
|
size,
|
2020-06-12 11:57:56 +00:00
|
|
|
index,
|
2019-01-02 13:27:04 +00:00
|
|
|
ctime,
|
|
|
|
uuid: header.uuid,
|
2019-06-14 06:57:52 +00:00
|
|
|
index_csum: header.index_csum,
|
2019-01-02 13:27:04 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-05 13:47:56 +00:00
|
|
|
#[inline]
|
2019-10-25 16:03:55 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2019-01-05 13:47:56 +00:00
|
|
|
fn chunk_end(&self, pos: usize) -> u64 {
|
2020-06-12 11:57:56 +00:00
|
|
|
if pos >= self.index.len() {
|
2019-01-05 13:47:56 +00:00
|
|
|
panic!("chunk index out of range");
|
|
|
|
}
|
2020-06-15 07:19:35 +00:00
|
|
|
self.index[pos].end()
|
2019-01-05 13:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-13 09:47:23 +00:00
|
|
|
fn chunk_digest(&self, pos: usize) -> &[u8; 32] {
|
2020-06-12 11:57:56 +00:00
|
|
|
if pos >= self.index.len() {
|
2019-01-05 13:47:56 +00:00
|
|
|
panic!("chunk index out of range");
|
|
|
|
}
|
2020-06-12 11:57:56 +00:00
|
|
|
&self.index[pos].digest
|
2019-01-05 13:47:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 11:57:56 +00:00
|
|
|
// TODO: can we use std::slice::binary_search with Mmap now?
|
2019-01-05 13:47:56 +00:00
|
|
|
fn binary_search(
|
|
|
|
&self,
|
|
|
|
start_idx: usize,
|
|
|
|
start: u64,
|
|
|
|
end_idx: usize,
|
|
|
|
end: u64,
|
2019-11-14 09:18:31 +00:00
|
|
|
offset: u64,
|
2019-01-05 13:47:56 +00:00
|
|
|
) -> Result<usize, Error> {
|
|
|
|
if (offset >= end) || (offset < start) {
|
|
|
|
bail!("offset out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
if end_idx == start_idx {
|
|
|
|
return Ok(start_idx); // found
|
|
|
|
}
|
2019-11-14 09:18:31 +00:00
|
|
|
let middle_idx = (start_idx + end_idx) / 2;
|
2019-01-05 13:47:56 +00:00
|
|
|
let middle_end = self.chunk_end(middle_idx);
|
|
|
|
|
|
|
|
if offset < middle_end {
|
2019-10-26 09:36:01 +00:00
|
|
|
self.binary_search(start_idx, start, middle_idx, middle_end, offset)
|
2019-01-05 13:47:56 +00:00
|
|
|
} else {
|
2019-10-26 09:36:01 +00:00
|
|
|
self.binary_search(middle_idx + 1, middle_end, end_idx, end, offset)
|
2019-01-05 13:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 13:32:34 +00:00
|
|
|
impl IndexFile for DynamicIndexReader {
|
|
|
|
fn index_count(&self) -> usize {
|
2020-06-12 11:57:56 +00:00
|
|
|
self.index.len()
|
2019-02-27 13:32:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn index_digest(&self, pos: usize) -> Option<&[u8; 32]> {
|
2020-06-12 11:57:56 +00:00
|
|
|
if pos >= self.index.len() {
|
2019-02-27 13:32:34 +00:00
|
|
|
None
|
|
|
|
} else {
|
2019-11-14 09:18:31 +00:00
|
|
|
Some(unsafe { std::mem::transmute(self.chunk_digest(pos).as_ptr()) })
|
2019-02-27 13:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-04 05:57:43 +00:00
|
|
|
|
|
|
|
fn index_bytes(&self) -> u64 {
|
2020-06-12 11:57:56 +00:00
|
|
|
if self.index.is_empty() {
|
2019-07-04 05:57:43 +00:00
|
|
|
0
|
|
|
|
} else {
|
2020-06-12 11:57:56 +00:00
|
|
|
self.chunk_end(self.index.len() - 1)
|
2019-07-04 05:57:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-26 06:14:45 +00:00
|
|
|
|
2020-06-29 10:44:45 +00:00
|
|
|
fn compute_csum(&self) -> ([u8; 32], u64) {
|
|
|
|
let mut csum = openssl::sha::Sha256::new();
|
|
|
|
let mut chunk_end = 0;
|
|
|
|
for pos in 0..self.index_count() {
|
|
|
|
let info = self.chunk_info(pos).unwrap();
|
|
|
|
chunk_end = info.range.end;
|
|
|
|
csum.update(&chunk_end.to_le_bytes());
|
|
|
|
csum.update(&info.digest);
|
|
|
|
}
|
|
|
|
let csum = csum.finish();
|
|
|
|
(csum, chunk_end)
|
|
|
|
}
|
|
|
|
|
2020-06-26 06:14:45 +00:00
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
fn chunk_info(&self, pos: usize) -> Option<ChunkReadInfo> {
|
|
|
|
if pos >= self.index.len() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let start = if pos == 0 { 0 } else { self.index[pos - 1].end() };
|
|
|
|
|
|
|
|
let end = self.index[pos].end();
|
|
|
|
|
|
|
|
Some(ChunkReadInfo {
|
|
|
|
range: start..end,
|
|
|
|
digest: self.index[pos].digest.clone(),
|
|
|
|
})
|
|
|
|
}
|
2020-07-22 13:56:21 +00:00
|
|
|
|
|
|
|
fn chunk_from_offset(&self, offset: u64) -> Option<(usize, u64)> {
|
|
|
|
let end_idx = self.index.len() - 1;
|
|
|
|
let end = self.chunk_end(end_idx);
|
|
|
|
let found_idx = self.binary_search(0, 0, end_idx, end, offset);
|
|
|
|
let found_idx = match found_idx {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => return None
|
|
|
|
};
|
|
|
|
|
|
|
|
let found_start = if found_idx == 0 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
self.chunk_end(found_idx - 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
Some((found_idx, offset - found_start))
|
|
|
|
}
|
2019-02-27 13:32:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
struct CachedChunk {
|
|
|
|
range: Range<u64>,
|
|
|
|
data: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CachedChunk {
|
|
|
|
/// Perform sanity checks on the range and data size:
|
|
|
|
pub fn new(range: Range<u64>, data: Vec<u8>) -> Result<Self, Error> {
|
|
|
|
if data.len() as u64 != range.end - range.start {
|
|
|
|
bail!(
|
|
|
|
"read chunk with wrong size ({} != {})",
|
|
|
|
data.len(),
|
|
|
|
range.end - range.start,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(Self { range, data })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:35:00 +00:00
|
|
|
pub struct BufferedDynamicReader<S> {
|
|
|
|
store: S,
|
2019-02-12 11:05:33 +00:00
|
|
|
index: DynamicIndexReader,
|
2019-01-05 13:47:56 +00:00
|
|
|
archive_size: u64,
|
|
|
|
read_buffer: Vec<u8>,
|
|
|
|
buffered_chunk_idx: usize,
|
|
|
|
buffered_chunk_start: u64,
|
|
|
|
read_offset: u64,
|
2020-03-23 14:03:18 +00:00
|
|
|
lru_cache: crate::tools::lru_cache::LruCache<usize, CachedChunk>,
|
2020-02-27 14:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ChunkCacher<'a, S> {
|
|
|
|
store: &'a mut S,
|
|
|
|
index: &'a DynamicIndexReader,
|
|
|
|
}
|
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
impl<'a, S: ReadChunk> crate::tools::lru_cache::Cacher<usize, CachedChunk> for ChunkCacher<'a, S> {
|
|
|
|
fn fetch(&mut self, index: usize) -> Result<Option<CachedChunk>, Error> {
|
2020-06-26 06:14:45 +00:00
|
|
|
let info = match self.index.chunk_info(index) {
|
|
|
|
Some(info) => info,
|
|
|
|
None => bail!("chunk index out of range"),
|
|
|
|
};
|
2020-03-23 14:03:18 +00:00
|
|
|
let range = info.range;
|
|
|
|
let data = self.store.read_chunk(&info.digest)?;
|
|
|
|
CachedChunk::new(range, data).map(Some)
|
2020-02-27 14:56:28 +00:00
|
|
|
}
|
2019-01-02 13:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
impl<S: ReadChunk> BufferedDynamicReader<S> {
|
2019-06-28 14:35:00 +00:00
|
|
|
pub fn new(index: DynamicIndexReader, store: S) -> Self {
|
2020-06-12 11:57:56 +00:00
|
|
|
let archive_size = index.index_bytes();
|
2019-01-05 13:47:56 +00:00
|
|
|
Self {
|
2019-06-28 14:35:00 +00:00
|
|
|
store,
|
2019-09-11 10:06:59 +00:00
|
|
|
index,
|
|
|
|
archive_size,
|
2019-11-14 09:18:31 +00:00
|
|
|
read_buffer: Vec::with_capacity(1024 * 1024),
|
2019-01-05 13:47:56 +00:00
|
|
|
buffered_chunk_idx: 0,
|
|
|
|
buffered_chunk_start: 0,
|
|
|
|
read_offset: 0,
|
2020-02-27 14:56:28 +00:00
|
|
|
lru_cache: crate::tools::lru_cache::LruCache::new(32),
|
2019-01-05 13:47:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
pub fn archive_size(&self) -> u64 {
|
|
|
|
self.archive_size
|
|
|
|
}
|
2019-01-05 13:47:56 +00:00
|
|
|
|
2019-01-05 16:28:20 +00:00
|
|
|
fn buffer_chunk(&mut self, idx: usize) -> Result<(), Error> {
|
2020-03-23 14:03:18 +00:00
|
|
|
//let (start, end, data) = self.lru_cache.access(
|
|
|
|
let cached_chunk = self.lru_cache.access(
|
2020-02-27 14:56:28 +00:00
|
|
|
idx,
|
|
|
|
&mut ChunkCacher {
|
|
|
|
store: &mut self.store,
|
|
|
|
index: &self.index,
|
|
|
|
},
|
|
|
|
)?.ok_or_else(|| format_err!("chunk not found by cacher"))?;
|
|
|
|
|
|
|
|
// fixme: avoid copy
|
2019-06-13 09:47:23 +00:00
|
|
|
self.read_buffer.clear();
|
2020-03-23 14:03:18 +00:00
|
|
|
self.read_buffer.extend_from_slice(&cached_chunk.data);
|
2019-01-05 16:28:20 +00:00
|
|
|
|
|
|
|
self.buffered_chunk_idx = idx;
|
2019-06-28 14:35:00 +00:00
|
|
|
|
2020-03-23 14:03:18 +00:00
|
|
|
self.buffered_chunk_start = cached_chunk.range.start;
|
2019-01-05 16:28:20 +00:00
|
|
|
//println!("BUFFER {} {}", self.buffered_chunk_start, end);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
impl<S: ReadChunk> crate::tools::BufferedRead for BufferedDynamicReader<S> {
|
2019-01-05 16:28:20 +00:00
|
|
|
fn buffered_read(&mut self, offset: u64) -> Result<&[u8], Error> {
|
2019-11-14 09:18:31 +00:00
|
|
|
if offset == self.archive_size {
|
|
|
|
return Ok(&self.read_buffer[0..0]);
|
|
|
|
}
|
2019-01-06 08:17:28 +00:00
|
|
|
|
2019-01-05 13:47:56 +00:00
|
|
|
let buffer_len = self.read_buffer.len();
|
2019-01-20 08:39:32 +00:00
|
|
|
let index = &self.index;
|
2019-01-05 13:47:56 +00:00
|
|
|
|
|
|
|
// optimization for sequential read
|
2019-11-14 09:18:31 +00:00
|
|
|
if buffer_len > 0
|
2020-06-12 11:57:56 +00:00
|
|
|
&& ((self.buffered_chunk_idx + 1) < index.index.len())
|
2019-11-14 09:18:31 +00:00
|
|
|
&& (offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
2019-01-05 13:47:56 +00:00
|
|
|
{
|
|
|
|
let next_idx = self.buffered_chunk_idx + 1;
|
|
|
|
let next_end = index.chunk_end(next_idx);
|
|
|
|
if offset < next_end {
|
2019-01-10 10:19:54 +00:00
|
|
|
self.buffer_chunk(next_idx)?;
|
2019-01-05 13:47:56 +00:00
|
|
|
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
|
|
|
return Ok(&self.read_buffer[buffer_offset..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
if (buffer_len == 0)
|
|
|
|
|| (offset < self.buffered_chunk_start)
|
|
|
|
|| (offset >= (self.buffered_chunk_start + (self.read_buffer.len() as u64)))
|
2019-01-05 13:47:56 +00:00
|
|
|
{
|
2020-06-12 11:57:56 +00:00
|
|
|
let end_idx = index.index.len() - 1;
|
2019-01-05 13:47:56 +00:00
|
|
|
let end = index.chunk_end(end_idx);
|
|
|
|
let idx = index.binary_search(0, 0, end_idx, end, offset)?;
|
2019-01-10 10:19:54 +00:00
|
|
|
self.buffer_chunk(idx)?;
|
2019-11-14 09:18:31 +00:00
|
|
|
}
|
2019-01-05 13:47:56 +00:00
|
|
|
|
|
|
|
let buffer_offset = (offset - self.buffered_chunk_start) as usize;
|
|
|
|
Ok(&self.read_buffer[buffer_offset..])
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 13:27:04 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
impl<S: ReadChunk> std::io::Read for BufferedDynamicReader<S> {
|
2019-01-06 09:04:45 +00:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
|
2019-02-28 08:17:04 +00:00
|
|
|
use crate::tools::BufferedRead;
|
2019-11-14 09:18:31 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2019-01-06 09:04:45 +00:00
|
|
|
|
|
|
|
let data = match self.buffered_read(self.read_offset) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())),
|
|
|
|
};
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let n = if data.len() > buf.len() {
|
|
|
|
buf.len()
|
|
|
|
} else {
|
|
|
|
data.len()
|
|
|
|
};
|
2019-01-06 09:04:45 +00:00
|
|
|
|
2020-06-18 11:55:19 +00:00
|
|
|
buf[0..n].copy_from_slice(&data[0..n]);
|
2019-01-06 09:04:45 +00:00
|
|
|
|
|
|
|
self.read_offset += n as u64;
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(n)
|
2019-01-06 09:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
impl<S: ReadChunk> std::io::Seek for BufferedDynamicReader<S> {
|
2019-06-28 14:35:00 +00:00
|
|
|
fn seek(&mut self, pos: SeekFrom) -> Result<u64, std::io::Error> {
|
2019-01-06 08:35:39 +00:00
|
|
|
let new_offset = match pos {
|
2019-11-14 09:18:31 +00:00
|
|
|
SeekFrom::Start(start_offset) => start_offset as i64,
|
|
|
|
SeekFrom::End(end_offset) => (self.archive_size as i64) + end_offset,
|
2019-01-06 08:35:39 +00:00
|
|
|
SeekFrom::Current(offset) => (self.read_offset as i64) + offset,
|
|
|
|
};
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
use std::io::{Error, ErrorKind};
|
2019-01-06 08:35:39 +00:00
|
|
|
if (new_offset < 0) || (new_offset > (self.archive_size as i64)) {
|
|
|
|
return Err(Error::new(
|
|
|
|
ErrorKind::Other,
|
2019-11-14 09:18:31 +00:00
|
|
|
format!(
|
|
|
|
"seek is out of range {} ([0..{}])",
|
|
|
|
new_offset, self.archive_size
|
|
|
|
),
|
|
|
|
));
|
2019-01-06 08:35:39 +00:00
|
|
|
}
|
|
|
|
self.read_offset = new_offset as u64;
|
|
|
|
|
|
|
|
Ok(self.read_offset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:09:49 +00:00
|
|
|
/// This is a workaround until we have cleaned up the chunk/reader/... infrastructure for better
|
|
|
|
/// async use!
|
|
|
|
///
|
|
|
|
/// Ideally BufferedDynamicReader gets replaced so the LruCache maps to `BroadcastFuture<Chunk>`,
|
|
|
|
/// so that we can properly access it from multiple threads simultaneously while not issuing
|
|
|
|
/// duplicate simultaneous reads over http.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct LocalDynamicReadAt<R: ReadChunk> {
|
|
|
|
inner: Arc<Mutex<BufferedDynamicReader<R>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<R: ReadChunk> LocalDynamicReadAt<R> {
|
|
|
|
pub fn new(inner: BufferedDynamicReader<R>) -> Self {
|
|
|
|
Self {
|
|
|
|
inner: Arc::new(Mutex::new(inner)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:57:12 +00:00
|
|
|
impl<R: ReadChunk> ReadAt for LocalDynamicReadAt<R> {
|
|
|
|
fn start_read_at<'a>(
|
|
|
|
self: Pin<&'a Self>,
|
2020-06-23 10:09:49 +00:00
|
|
|
_cx: &mut Context,
|
2020-06-24 09:57:12 +00:00
|
|
|
buf: &'a mut [u8],
|
2020-06-23 10:09:49 +00:00
|
|
|
offset: u64,
|
2020-06-24 09:57:12 +00:00
|
|
|
) -> MaybeReady<io::Result<usize>, ReadAtOperation<'a>> {
|
2020-06-23 10:09:49 +00:00
|
|
|
use std::io::Read;
|
2020-06-24 09:57:12 +00:00
|
|
|
MaybeReady::Ready(tokio::task::block_in_place(move || {
|
2020-06-23 10:09:49 +00:00
|
|
|
let mut reader = self.inner.lock().unwrap();
|
|
|
|
reader.seek(SeekFrom::Start(offset))?;
|
2020-06-24 09:57:12 +00:00
|
|
|
Ok(reader.read(buf)?)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_complete<'a>(
|
|
|
|
self: Pin<&'a Self>,
|
|
|
|
_op: ReadAtOperation<'a>,
|
|
|
|
) -> MaybeReady<io::Result<usize>, ReadAtOperation<'a>> {
|
|
|
|
panic!("LocalDynamicReadAt::start_read_at returned Pending");
|
2020-06-23 10:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-29 06:49:57 +00:00
|
|
|
/// Create dynamic index files (`.dixd`)
|
2019-02-12 11:05:33 +00:00
|
|
|
pub struct DynamicIndexWriter {
|
2019-01-15 10:38:26 +00:00
|
|
|
store: Arc<ChunkStore>,
|
2019-03-22 08:42:15 +00:00
|
|
|
_lock: tools::ProcessLockSharedGuard,
|
2019-01-02 11:54:40 +00:00
|
|
|
writer: BufWriter<File>,
|
2019-01-02 10:02:56 +00:00
|
|
|
closed: bool,
|
2018-12-31 16:30:08 +00:00
|
|
|
filename: PathBuf,
|
|
|
|
tmp_filename: PathBuf,
|
2019-06-14 06:57:52 +00:00
|
|
|
csum: Option<openssl::sha::Sha256>,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
2020-09-12 13:10:47 +00:00
|
|
|
pub ctime: i64,
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl Drop for DynamicIndexWriter {
|
2019-01-15 10:38:26 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:05:33 +00:00
|
|
|
impl DynamicIndexWriter {
|
2019-05-29 06:49:57 +00:00
|
|
|
pub fn create(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
|
2019-03-22 08:42:15 +00:00
|
|
|
let shared_lock = store.try_shared_lock()?;
|
|
|
|
|
2018-12-31 16:30:08 +00:00
|
|
|
let full_path = store.relative_path(path);
|
|
|
|
let mut tmp_path = full_path.clone();
|
2019-02-12 11:05:33 +00:00
|
|
|
tmp_path.set_extension("tmp_didx");
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let file = std::fs::OpenOptions::new()
|
2019-11-14 09:18:31 +00:00
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
2018-12-31 16:30:08 +00:00
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.open(&tmp_path)?;
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let mut writer = BufWriter::with_capacity(1024 * 1024, file);
|
2019-01-02 11:54:40 +00:00
|
|
|
|
2020-09-12 13:10:47 +00:00
|
|
|
let ctime = proxmox::tools::time::epoch_i64();
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let uuid = Uuid::generate();
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2020-08-17 09:50:32 +00:00
|
|
|
let mut header = DynamicIndexHeader::zeroed();
|
2019-06-14 12:58:37 +00:00
|
|
|
header.magic = super::DYNAMIC_SIZED_CHUNK_INDEX_1_0;
|
2020-09-12 13:10:47 +00:00
|
|
|
header.ctime = i64::to_le(ctime);
|
2018-12-31 16:30:08 +00:00
|
|
|
header.uuid = *uuid.as_bytes();
|
2020-08-17 09:50:32 +00:00
|
|
|
// header.index_csum = [0u8; 32];
|
|
|
|
writer.write_all(header.as_bytes())?;
|
2018-12-31 16:30:08 +00:00
|
|
|
|
2019-06-14 06:57:52 +00:00
|
|
|
let csum = Some(openssl::sha::Sha256::new());
|
|
|
|
|
2018-12-31 16:30:08 +00:00
|
|
|
Ok(Self {
|
|
|
|
store,
|
2019-03-22 08:42:15 +00:00
|
|
|
_lock: shared_lock,
|
2019-09-11 10:06:59 +00:00
|
|
|
writer,
|
2019-01-02 10:02:56 +00:00
|
|
|
closed: false,
|
2018-12-31 16:30:08 +00:00
|
|
|
filename: full_path,
|
|
|
|
tmp_filename: tmp_path,
|
|
|
|
ctime,
|
|
|
|
uuid: *uuid.as_bytes(),
|
2019-06-14 06:57:52 +00:00
|
|
|
csum,
|
2018-12-31 16:30:08 +00:00
|
|
|
})
|
|
|
|
}
|
2019-01-02 10:02:56 +00:00
|
|
|
|
2019-06-13 09:47:23 +00:00
|
|
|
// fixme: use add_chunk instead?
|
2019-10-06 08:31:06 +00:00
|
|
|
pub fn insert_chunk(&self, chunk: &DataBlob, digest: &[u8; 32]) -> Result<(bool, u64), Error> {
|
|
|
|
self.store.insert_chunk(chunk, digest)
|
2019-05-29 06:49:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
pub fn close(&mut self) -> Result<[u8; 32], Error> {
|
2019-01-02 10:02:56 +00:00
|
|
|
if self.closed {
|
2019-11-14 09:18:31 +00:00
|
|
|
bail!(
|
|
|
|
"cannot close already closed archive index file {:?}",
|
|
|
|
self.filename
|
|
|
|
);
|
2019-01-02 10:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.closed = true;
|
|
|
|
|
2019-01-02 11:54:40 +00:00
|
|
|
self.writer.flush()?;
|
2019-01-02 10:02:56 +00:00
|
|
|
|
2020-01-21 11:28:01 +00:00
|
|
|
let csum_offset = proxmox::offsetof!(DynamicIndexHeader, index_csum);
|
2019-06-28 14:35:00 +00:00
|
|
|
self.writer.seek(SeekFrom::Start(csum_offset as u64))?;
|
2019-06-14 06:57:52 +00:00
|
|
|
|
|
|
|
let csum = self.csum.take().unwrap();
|
|
|
|
let index_csum = csum.finish();
|
|
|
|
|
|
|
|
self.writer.write_all(&index_csum)?;
|
|
|
|
self.writer.flush()?;
|
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
|
|
|
|
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:57:52 +00:00
|
|
|
Ok(index_csum)
|
2019-01-02 10:02:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 06:49:57 +00:00
|
|
|
// fixme: rename to add_digest
|
|
|
|
pub fn add_chunk(&mut self, offset: u64, digest: &[u8; 32]) -> Result<(), Error> {
|
|
|
|
if self.closed {
|
2019-11-14 09:18:31 +00:00
|
|
|
bail!(
|
|
|
|
"cannot write to closed dynamic index file {:?}",
|
|
|
|
self.filename
|
|
|
|
);
|
2019-05-29 06:49:57 +00:00
|
|
|
}
|
2019-06-14 06:57:52 +00:00
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
let offset_le: &[u8; 8] = unsafe { &std::mem::transmute::<u64, [u8; 8]>(offset.to_le()) };
|
2019-06-14 06:57:52 +00:00
|
|
|
|
|
|
|
if let Some(ref mut csum) = self.csum {
|
|
|
|
csum.update(offset_le);
|
|
|
|
csum.update(digest);
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:22:41 +00:00
|
|
|
self.writer.write_all(offset_le)?;
|
|
|
|
self.writer.write_all(digest)?;
|
2019-05-29 06:49:57 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writer which splits a binary stream into dynamic sized chunks
|
|
|
|
///
|
|
|
|
/// And store the resulting chunk list into the index file.
|
|
|
|
pub struct DynamicChunkWriter {
|
|
|
|
index: DynamicIndexWriter,
|
|
|
|
closed: bool,
|
|
|
|
chunker: Chunker,
|
|
|
|
stat: ChunkStat,
|
|
|
|
chunk_offset: usize,
|
|
|
|
last_chunk: usize,
|
|
|
|
chunk_buffer: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DynamicChunkWriter {
|
|
|
|
pub fn new(index: DynamicIndexWriter, chunk_size: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
index,
|
|
|
|
closed: false,
|
|
|
|
chunker: Chunker::new(chunk_size),
|
|
|
|
stat: ChunkStat::new(0),
|
|
|
|
chunk_offset: 0,
|
|
|
|
last_chunk: 0,
|
2019-11-14 09:18:31 +00:00
|
|
|
chunk_buffer: Vec::with_capacity(chunk_size * 4),
|
2019-05-29 06:49:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
pub fn stat(&self) -> &ChunkStat {
|
|
|
|
&self.stat
|
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
pub fn close(&mut self) -> Result<(), Error> {
|
2019-05-29 06:49:57 +00:00
|
|
|
if self.closed {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.closed = true;
|
|
|
|
|
|
|
|
self.write_chunk_buffer()?;
|
|
|
|
|
|
|
|
self.index.close()?;
|
|
|
|
|
|
|
|
self.stat.size = self.chunk_offset as u64;
|
|
|
|
|
|
|
|
// add size of index file
|
2019-11-14 09:18:31 +00:00
|
|
|
self.stat.size +=
|
|
|
|
(self.stat.chunk_count * 40 + std::mem::size_of::<DynamicIndexHeader>()) as u64;
|
2019-05-29 06:49:57 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_chunk_buffer(&mut self) -> Result<(), Error> {
|
2019-01-02 10:02:56 +00:00
|
|
|
let chunk_size = self.chunk_buffer.len();
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
if chunk_size == 0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2019-01-02 10:02:56 +00:00
|
|
|
|
|
|
|
let expected_chunk_size = self.chunk_offset - self.last_chunk;
|
|
|
|
if expected_chunk_size != self.chunk_buffer.len() {
|
2019-05-29 06:49:57 +00:00
|
|
|
bail!("wrong chunk size {} != {}", expected_chunk_size, chunk_size);
|
2019-01-02 10:02:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.chunk_count += 1;
|
2019-02-19 14:19:12 +00:00
|
|
|
|
2019-01-02 10:02:56 +00:00
|
|
|
self.last_chunk = self.chunk_offset;
|
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
let (chunk, digest) = DataChunkBuilder::new(&self.chunk_buffer)
|
2019-06-13 09:47:23 +00:00
|
|
|
.compress(true)
|
|
|
|
.build()?;
|
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
match self.index.insert_chunk(&chunk, &digest) {
|
2019-06-13 09:47:23 +00:00
|
|
|
Ok((is_duplicate, compressed_size)) => {
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.compressed_size += compressed_size;
|
2019-02-25 10:36:05 +00:00
|
|
|
if is_duplicate {
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.duplicate_chunks += 1;
|
2019-02-25 10:36:05 +00:00
|
|
|
} else {
|
2019-02-25 11:52:10 +00:00
|
|
|
self.stat.disk_size += compressed_size;
|
2019-02-25 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 09:18:31 +00:00
|
|
|
println!(
|
|
|
|
"ADD CHUNK {:016x} {} {}% {} {}",
|
|
|
|
self.chunk_offset,
|
|
|
|
chunk_size,
|
|
|
|
(compressed_size * 100) / (chunk_size as u64),
|
|
|
|
is_duplicate,
|
|
|
|
proxmox::tools::digest_to_hex(&digest)
|
|
|
|
);
|
2019-05-29 06:49:57 +00:00
|
|
|
self.index.add_chunk(self.chunk_offset as u64, &digest)?;
|
2019-01-02 10:02:56 +00:00
|
|
|
self.chunk_buffer.truncate(0);
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(())
|
2019-01-02 10:02:56 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
self.chunk_buffer.truncate(0);
|
2019-10-26 09:36:01 +00:00
|
|
|
Err(err)
|
2019-01-02 10:02:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 06:49:57 +00:00
|
|
|
impl Write for DynamicChunkWriter {
|
2018-12-31 16:30:08 +00:00
|
|
|
fn write(&mut self, data: &[u8]) -> std::result::Result<usize, std::io::Error> {
|
|
|
|
let chunker = &mut self.chunker;
|
|
|
|
|
|
|
|
let pos = chunker.scan(data);
|
|
|
|
|
|
|
|
if pos > 0 {
|
2019-10-06 08:31:06 +00:00
|
|
|
self.chunk_buffer.extend_from_slice(&data[0..pos]);
|
2018-12-31 16:30:08 +00:00
|
|
|
self.chunk_offset += pos;
|
|
|
|
|
2019-05-29 06:49:57 +00:00
|
|
|
if let Err(err) = self.write_chunk_buffer() {
|
2019-11-14 09:18:31 +00:00
|
|
|
return Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
err.to_string(),
|
|
|
|
));
|
2019-05-29 06:49:57 +00:00
|
|
|
}
|
2019-01-02 10:02:56 +00:00
|
|
|
Ok(pos)
|
2018-12-31 16:30:08 +00:00
|
|
|
} else {
|
|
|
|
self.chunk_offset += data.len();
|
2019-10-06 08:31:06 +00:00
|
|
|
self.chunk_buffer.extend_from_slice(data);
|
2019-01-02 10:02:56 +00:00
|
|
|
Ok(data.len())
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
|
2019-11-14 09:18:31 +00:00
|
|
|
Err(std::io::Error::new(
|
|
|
|
std::io::ErrorKind::Other,
|
|
|
|
"please use close() instead of flush()",
|
|
|
|
))
|
2018-12-31 16:30:08 +00:00
|
|
|
}
|
|
|
|
}
|