2018-12-15 13:51:05 +00:00
|
|
|
use failure::*;
|
|
|
|
|
2019-01-25 09:58:28 +00:00
|
|
|
use crate::tools;
|
2018-12-15 13:51:05 +00:00
|
|
|
use super::chunk_store::*;
|
|
|
|
|
2019-01-15 11:36:16 +00:00
|
|
|
use std::sync::Arc;
|
2018-12-16 13:44:44 +00:00
|
|
|
use std::io::{Read, Write};
|
2018-12-15 13:51:05 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::os::unix::io::AsRawFd;
|
2018-12-15 16:05:49 +00:00
|
|
|
use uuid::Uuid;
|
2018-12-16 13:44:44 +00:00
|
|
|
use chrono::{Local, TimeZone};
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
/// Header format definition for fixed index files (`.fixd`)
|
2018-12-15 16:05:49 +00:00
|
|
|
#[repr(C)]
|
2019-02-12 10:50:45 +00:00
|
|
|
pub struct FixedIndexHeader {
|
2019-02-12 13:13:31 +00:00
|
|
|
/// The string `PROXMOX-FIDX`
|
2018-12-15 16:05:49 +00:00
|
|
|
pub magic: [u8; 12],
|
|
|
|
pub version: u32,
|
|
|
|
pub uuid: [u8; 16],
|
2018-12-16 10:48:03 +00:00
|
|
|
pub ctime: u64,
|
2018-12-15 16:05:49 +00:00
|
|
|
pub size: u64,
|
2018-12-16 13:44:44 +00:00
|
|
|
pub chunk_size: u64,
|
2019-01-02 11:56:04 +00:00
|
|
|
reserved: [u8; 4040], // overall size is one page (4096 bytes)
|
2018-12-15 16:05:49 +00:00
|
|
|
}
|
2018-12-15 13:51:05 +00:00
|
|
|
|
|
|
|
// split image into fixed size chunks
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub struct FixedIndexReader {
|
2019-01-15 11:36:16 +00:00
|
|
|
store: Arc<ChunkStore>,
|
2018-12-16 13:44:44 +00:00
|
|
|
filename: PathBuf,
|
|
|
|
chunk_size: usize,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub size: usize,
|
2018-12-16 13:44:44 +00:00
|
|
|
index: *mut u8,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
|
|
|
pub ctime: u64,
|
2018-12-16 13:44:44 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl Drop for FixedIndexReader {
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Err(err) = self.unmap() {
|
|
|
|
eprintln!("Unable to unmap file {:?} - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl FixedIndexReader {
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-01-15 11:36:16 +00:00
|
|
|
pub fn open(store: Arc<ChunkStore>, path: &Path) -> Result<Self, Error> {
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
let full_path = store.relative_path(path);
|
|
|
|
|
|
|
|
let mut file = std::fs::File::open(&full_path)?;
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let header_size = std::mem::size_of::<FixedIndexHeader>();
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
// todo: use static assertion when available in rust
|
2019-01-02 12:13:13 +00:00
|
|
|
if header_size != 4096 { bail!("got unexpected header size for {:?}", path); }
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
let mut buffer = vec![0u8; header_size];
|
|
|
|
file.read_exact(&mut buffer)?;
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) };
|
2018-12-16 13:44:44 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
if header.magic != *b"PROXMOX-FIDX" {
|
2019-01-02 12:13:13 +00:00
|
|
|
bail!("got unknown magic number for {:?}", path);
|
|
|
|
}
|
|
|
|
|
2019-01-04 07:45:04 +00:00
|
|
|
let version = u32::from_le(header.version);
|
2019-01-02 12:13:13 +00:00
|
|
|
if version != 1 {
|
|
|
|
bail!("got unsupported version number ({})", version);
|
|
|
|
}
|
|
|
|
|
2019-01-04 07:45:04 +00:00
|
|
|
let size = u64::from_le(header.size) as usize;
|
|
|
|
let ctime = u64::from_le(header.ctime);
|
|
|
|
let chunk_size = u64::from_le(header.chunk_size) as usize;
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
let index_size = ((size + chunk_size - 1)/chunk_size)*32;
|
|
|
|
|
2019-01-02 17:14:02 +00:00
|
|
|
let rawfd = file.as_raw_fd();
|
|
|
|
|
|
|
|
let stat = match nix::sys::stat::fstat(rawfd) {
|
|
|
|
Ok(stat) => stat,
|
|
|
|
Err(err) => bail!("fstat {:?} failed - {}", path, err),
|
|
|
|
};
|
|
|
|
|
2019-01-11 07:41:33 +00:00
|
|
|
let expected_index_size = (stat.st_size as usize) - header_size;
|
2019-01-02 17:14:02 +00:00
|
|
|
if index_size != expected_index_size {
|
|
|
|
bail!("got unexpected file size for {:?} ({} != {})",
|
|
|
|
path, index_size, expected_index_size);
|
|
|
|
}
|
2018-12-16 13:44:44 +00:00
|
|
|
|
|
|
|
let data = unsafe { nix::sys::mman::mmap(
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
index_size,
|
|
|
|
nix::sys::mman::ProtFlags::PROT_READ,
|
|
|
|
nix::sys::mman::MapFlags::MAP_PRIVATE,
|
|
|
|
file.as_raw_fd(),
|
|
|
|
header_size as i64) }? as *mut u8;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
store,
|
|
|
|
filename: full_path,
|
|
|
|
chunk_size,
|
|
|
|
size,
|
|
|
|
index: data,
|
|
|
|
ctime,
|
|
|
|
uuid: header.uuid,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unmap(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if self.index == std::ptr::null_mut() { return Ok(()); }
|
|
|
|
|
|
|
|
let index_size = ((self.size + self.chunk_size - 1)/self.chunk_size)*32;
|
|
|
|
|
|
|
|
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } {
|
|
|
|
bail!("unmap file {:?} failed - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.index = std::ptr::null_mut();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
pub fn mark_used_chunks(&self, status: &mut GarbageCollectionStatus) -> Result<(), Error> {
|
2018-12-18 10:06:03 +00:00
|
|
|
|
|
|
|
if self.index == std::ptr::null_mut() { bail!("detected closed index file."); }
|
|
|
|
|
|
|
|
let index_count = (self.size + self.chunk_size - 1)/self.chunk_size;
|
|
|
|
|
2018-12-22 15:58:16 +00:00
|
|
|
status.used_bytes += index_count * self.chunk_size;
|
|
|
|
status.used_chunks += index_count;
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
for pos in 0..index_count {
|
|
|
|
|
|
|
|
let digest = unsafe { std::slice::from_raw_parts_mut(self.index.add(pos*32), 32) };
|
|
|
|
if let Err(err) = self.store.touch_chunk(digest) {
|
|
|
|
bail!("unable to access chunk {}, required by {:?} - {}",
|
2019-01-25 09:58:28 +00:00
|
|
|
tools::digest_to_hex(digest), self.filename, err);
|
2018-12-18 10:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-16 13:44:44 +00:00
|
|
|
pub fn print_info(&self) {
|
|
|
|
println!("Filename: {:?}", self.filename);
|
|
|
|
println!("Size: {}", self.size);
|
|
|
|
println!("ChunkSize: {}", self.chunk_size);
|
|
|
|
println!("CTime: {}", Local.timestamp(self.ctime as i64, 0).format("%c"));
|
|
|
|
println!("UUID: {:?}", self.uuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
pub struct FixedIndexWriter {
|
2019-01-15 11:36:16 +00:00
|
|
|
store: Arc<ChunkStore>,
|
2018-12-16 12:39:21 +00:00
|
|
|
filename: PathBuf,
|
|
|
|
tmp_filename: PathBuf,
|
2018-12-15 13:51:05 +00:00
|
|
|
chunk_size: usize,
|
2019-01-02 11:53:49 +00:00
|
|
|
duplicate_chunks: usize,
|
2018-12-15 13:51:05 +00:00
|
|
|
size: usize,
|
|
|
|
index: *mut u8,
|
2019-01-30 17:25:37 +00:00
|
|
|
pub uuid: [u8; 16],
|
|
|
|
pub ctime: u64,
|
2018-12-15 13:51:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl Drop for FixedIndexWriter {
|
2018-12-16 12:39:21 +00:00
|
|
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = std::fs::remove_file(&self.tmp_filename); // ignore errors
|
|
|
|
if let Err(err) = self.unmap() {
|
2018-12-16 12:43:19 +00:00
|
|
|
eprintln!("Unable to unmap file {:?} - {}", self.tmp_filename, err);
|
2018-12-16 12:39:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
impl FixedIndexWriter {
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-01-15 11:36:16 +00:00
|
|
|
pub fn create(store: Arc<ChunkStore>, path: &Path, size: usize, chunk_size: usize) -> Result<Self, Error> {
|
2018-12-15 13:51:05 +00:00
|
|
|
|
|
|
|
let full_path = store.relative_path(path);
|
2018-12-16 12:39:21 +00:00
|
|
|
let mut tmp_path = full_path.clone();
|
2019-02-12 10:50:45 +00:00
|
|
|
tmp_path.set_extension("tmp_fidx");
|
2018-12-15 13:51:05 +00:00
|
|
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
2018-12-15 16:05:49 +00:00
|
|
|
.create(true).truncate(true)
|
2018-12-15 13:51:05 +00:00
|
|
|
.read(true)
|
|
|
|
.write(true)
|
2018-12-16 12:39:21 +00:00
|
|
|
.open(&tmp_path)?;
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
let header_size = std::mem::size_of::<FixedIndexHeader>();
|
2018-12-15 16:05:49 +00:00
|
|
|
|
|
|
|
// todo: use static assertion when available in rust
|
|
|
|
if header_size != 4096 { panic!("got unexpected header size"); }
|
|
|
|
|
|
|
|
let ctime = std::time::SystemTime::now().duration_since(
|
2018-12-16 10:48:03 +00:00
|
|
|
std::time::SystemTime::UNIX_EPOCH)?.as_secs();
|
2018-12-15 16:05:49 +00:00
|
|
|
|
|
|
|
let uuid = Uuid::new_v4();
|
|
|
|
|
2018-12-16 12:43:19 +00:00
|
|
|
let buffer = vec![0u8; header_size];
|
2019-02-12 10:50:45 +00:00
|
|
|
let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) };
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2019-02-12 10:50:45 +00:00
|
|
|
header.magic = *b"PROXMOX-FIDX";
|
2019-01-04 07:45:04 +00:00
|
|
|
header.version = u32::to_le(1);
|
|
|
|
header.ctime = u64::to_le(ctime);
|
|
|
|
header.size = u64::to_le(size as u64);
|
|
|
|
header.chunk_size = u64::to_le(chunk_size as u64);
|
2018-12-15 16:05:49 +00:00
|
|
|
header.uuid = *uuid.as_bytes();
|
|
|
|
|
2018-12-16 10:48:03 +00:00
|
|
|
file.write_all(&buffer)?;
|
2018-12-15 16:05:49 +00:00
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
let index_size = ((size + chunk_size - 1)/chunk_size)*32;
|
2018-12-15 16:05:49 +00:00
|
|
|
nix::unistd::ftruncate(file.as_raw_fd(), (header_size + index_size) as i64)?;
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
let data = unsafe { nix::sys::mman::mmap(
|
|
|
|
std::ptr::null_mut(),
|
|
|
|
index_size,
|
|
|
|
nix::sys::mman::ProtFlags::PROT_READ | nix::sys::mman::ProtFlags::PROT_WRITE,
|
|
|
|
nix::sys::mman::MapFlags::MAP_SHARED,
|
|
|
|
file.as_raw_fd(),
|
2018-12-15 16:05:49 +00:00
|
|
|
header_size as i64) }? as *mut u8;
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
store,
|
2018-12-16 12:39:21 +00:00
|
|
|
filename: full_path,
|
|
|
|
tmp_filename: tmp_path,
|
2018-12-15 13:51:05 +00:00
|
|
|
chunk_size,
|
2019-01-02 11:53:49 +00:00
|
|
|
duplicate_chunks: 0,
|
2018-12-15 13:51:05 +00:00
|
|
|
size,
|
|
|
|
index: data,
|
2018-12-15 16:05:49 +00:00
|
|
|
ctime,
|
|
|
|
uuid: *uuid.as_bytes(),
|
2018-12-15 13:51:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-16 12:39:21 +00:00
|
|
|
fn unmap(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if self.index == std::ptr::null_mut() { return Ok(()); }
|
|
|
|
|
|
|
|
let index_size = ((self.size + self.chunk_size - 1)/self.chunk_size)*32;
|
|
|
|
|
|
|
|
if let Err(err) = unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) } {
|
2018-12-16 12:43:19 +00:00
|
|
|
bail!("unmap file {:?} failed - {}", self.tmp_filename, err);
|
2018-12-16 12:39:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.index = std::ptr::null_mut();
|
|
|
|
|
2019-01-02 11:53:49 +00:00
|
|
|
println!("Original size: {} Compressed size: {} Deduplicated size: {}",
|
|
|
|
self.size, self.size, self.size - (self.duplicate_chunks*self.chunk_size));
|
|
|
|
|
2018-12-16 12:39:21 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if self.index == std::ptr::null_mut() { bail!("cannot close already closed index file."); }
|
|
|
|
|
|
|
|
self.unmap()?;
|
|
|
|
|
|
|
|
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
|
|
|
|
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
// Note: We want to add data out of order, so do not assume and order here.
|
|
|
|
pub fn add_chunk(&mut self, pos: usize, chunk: &[u8]) -> Result<(), Error> {
|
|
|
|
|
2018-12-16 12:39:21 +00:00
|
|
|
if self.index == std::ptr::null_mut() { bail!("cannot write to closed index file."); }
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
let end = pos + chunk.len();
|
|
|
|
|
|
|
|
if end > self.size {
|
|
|
|
bail!("write chunk data exceeds size ({} >= {})", end, self.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// last chunk can be smaller
|
|
|
|
if ((end != self.size) && (chunk.len() != self.chunk_size)) ||
|
|
|
|
(chunk.len() > self.chunk_size) || (chunk.len() == 0) {
|
|
|
|
bail!("got chunk with wrong length ({} != {}", chunk.len(), self.chunk_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos >= self.size { bail!("add chunk after end ({} >= {})", pos, self.size); }
|
|
|
|
|
|
|
|
if pos & (self.chunk_size-1) != 0 { bail!("add unaligned chunk (pos = {})", pos); }
|
|
|
|
|
|
|
|
|
|
|
|
let (is_duplicate, digest) = self.store.insert_chunk(chunk)?;
|
|
|
|
|
2019-01-25 09:58:28 +00:00
|
|
|
println!("ADD CHUNK {} {} {} {}", pos, chunk.len(), is_duplicate, tools::digest_to_hex(&digest));
|
2018-12-15 13:51:05 +00:00
|
|
|
|
2019-01-02 11:53:49 +00:00
|
|
|
if is_duplicate { self.duplicate_chunks += 1; }
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
let index_pos = (pos/self.chunk_size)*32;
|
|
|
|
unsafe {
|
|
|
|
let dst = self.index.add(index_pos);
|
|
|
|
dst.copy_from_nonoverlapping(digest.as_ptr(), 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|