proxmox-backup/src/backup/fixed_index.rs

385 lines
12 KiB
Rust
Raw Normal View History

use failure::*;
use crate::tools;
use super::IndexFile;
use super::chunk_stat::*;
use super::chunk_store::*;
use std::sync::Arc;
2018-12-16 13:44:44 +00:00
use std::io::{Read, Write};
use std::fs::File;
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};
use super::ChunkInfo;
/// Header format definition for fixed index files (`.fidx`)
2018-12-15 16:05:49 +00:00
#[repr(C)]
pub struct FixedIndexHeader {
pub magic: [u8; 8],
2018-12-15 16:05:49 +00:00
pub uuid: [u8; 16],
2018-12-16 10:48:03 +00:00
pub ctime: u64,
/// Sha256 over the index ``SHA256(digest1||digest2||...)``
pub index_csum: [u8; 32],
pub size: u64,
pub chunk_size: u64,
reserved: [u8; 4016], // overall size is one page (4096 bytes)
2018-12-15 16:05:49 +00:00
}
// split image into fixed size chunks
pub struct FixedIndexReader {
store: Arc<ChunkStore>,
_file: File,
2018-12-16 13:44:44 +00:00
filename: PathBuf,
pub chunk_size: usize,
2019-01-30 17:25:37 +00:00
pub size: usize,
index_length: 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,
pub index_csum: [u8; 32],
2018-12-16 13:44:44 +00:00
}
// `index` is mmap()ed which cannot be thread-local so should be sendable
unsafe impl Send for FixedIndexReader {}
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);
}
}
}
impl FixedIndexReader {
2018-12-16 13:44:44 +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 = File::open(&full_path)
.map_err(|err| format_err!("Unable to open fixed index {:?} - {}", full_path, err))?;
2018-12-16 13:44:44 +00:00
2019-03-27 09:11:14 +00:00
if let Err(err) = nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockSharedNonblock) {
bail!("unable to get shared lock on {:?} - {}", full_path, err);
}
let header_size = std::mem::size_of::<FixedIndexHeader>();
2018-12-16 13:44:44 +00:00
// todo: use static assertion when available in rust
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)?;
let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) };
2018-12-16 13:44:44 +00:00
if header.magic != super::FIXED_SIZED_CHUNK_INDEX_1_0 {
bail!("got unknown magic number for {:?}", path);
}
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_length = (size + chunk_size - 1)/chunk_size;
let index_size = index_length*32;
2018-12-16 13:44:44 +00:00
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,
_file: file,
2018-12-16 13:44:44 +00:00
chunk_size,
size,
index_length,
2018-12-16 13:44:44 +00:00
index: data,
ctime,
uuid: header.uuid,
index_csum: header.index_csum,
2018-12-16 13:44:44 +00:00
})
}
fn unmap(&mut self) -> Result<(), Error> {
if self.index == std::ptr::null_mut() { return Ok(()); }
let index_size = self.index_length*32;
2018-12-16 13:44:44 +00:00
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(())
}
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."); }
status.used_bytes += self.index_length * self.chunk_size;
status.used_chunks += self.index_length;
2018-12-18 10:06:03 +00:00
for pos in 0..self.index_length {
2018-12-18 10:06:03 +00:00
2019-04-01 10:13:02 +00:00
tools::fail_on_shutdown()?;
let digest = self.index_digest(pos).unwrap();
2018-12-18 10:06:03 +00:00
if let Err(err) = self.store.touch_chunk(digest) {
bail!("unable to access chunk {}, required by {:?} - {}",
proxmox::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);
}
}
impl IndexFile for FixedIndexReader {
fn index_count(&self) -> usize {
self.index_length
}
fn index_digest(&self, pos: usize) -> Option<&[u8; 32]> {
if pos >= self.index_length {
None
} else {
Some(unsafe { std::mem::transmute(self.index.add(pos*32)) })
}
}
}
pub struct FixedIndexWriter {
store: Arc<ChunkStore>,
file: File,
_lock: tools::ProcessLockSharedGuard,
filename: PathBuf,
tmp_filename: PathBuf,
chunk_size: usize,
size: usize,
index_length: usize,
index: *mut u8,
2019-01-30 17:25:37 +00:00
pub uuid: [u8; 16],
pub ctime: u64,
}
// `index` is mmap()ed which cannot be thread-local so should be sendable
unsafe impl Send for FixedIndexWriter {}
impl Drop for FixedIndexWriter {
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);
}
}
}
impl FixedIndexWriter {
pub fn create(store: Arc<ChunkStore>, path: &Path, size: usize, chunk_size: usize) -> Result<Self, Error> {
let shared_lock = store.try_shared_lock()?;
let full_path = store.relative_path(path);
let mut tmp_path = full_path.clone();
tmp_path.set_extension("tmp_fidx");
let mut file = std::fs::OpenOptions::new()
2018-12-15 16:05:49 +00:00
.create(true).truncate(true)
.read(true)
.write(true)
.open(&tmp_path)?;
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];
let header = unsafe { &mut * (buffer.as_ptr() as *mut FixedIndexHeader) };
2018-12-15 16:05:49 +00:00
header.magic = super::FIXED_SIZED_CHUNK_INDEX_1_0;
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();
header.index_csum = [0u8; 32];
2018-12-16 10:48:03 +00:00
file.write_all(&buffer)?;
2018-12-15 16:05:49 +00:00
let index_length = (size + chunk_size - 1)/chunk_size;
let index_size = index_length*32;
2018-12-15 16:05:49 +00:00
nix::unistd::ftruncate(file.as_raw_fd(), (header_size + index_size) as i64)?;
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;
Ok(Self {
store,
file,
_lock: shared_lock,
filename: full_path,
tmp_filename: tmp_path,
chunk_size,
size,
index_length,
index: data,
2018-12-15 16:05:49 +00:00
ctime,
uuid: *uuid.as_bytes(),
})
}
pub fn index_length(&self) -> usize {
self.index_length
}
fn unmap(&mut self) -> Result<(), Error> {
if self.index == std::ptr::null_mut() { return Ok(()); }
let index_size = self.index_length*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);
}
self.index = std::ptr::null_mut();
Ok(())
}
pub fn close(&mut self) -> Result<[u8; 32], Error> {
if self.index == std::ptr::null_mut() { bail!("cannot close already closed index file."); }
let index_size = self.index_length*32;
let data = unsafe { std::slice::from_raw_parts(self.index, index_size) };
let index_csum = openssl::sha::sha256(data);
self.unmap()?;
use std::io::Seek;
let csum_offset = proxmox::tools::offsetof!(FixedIndexHeader, index_csum);
self.file.seek(std::io::SeekFrom::Start(csum_offset as u64))?;
self.file.write_all(&index_csum)?;
self.file.flush()?;
if let Err(err) = std::fs::rename(&self.tmp_filename, &self.filename) {
bail!("Atomic rename file {:?} failed - {}", self.filename, err);
}
Ok(index_csum)
}
// Note: We want to add data out of order, so do not assume any order here.
pub fn add_chunk(&mut self, chunk_info: &ChunkInfo, stat: &mut ChunkStat) -> Result<(), Error> {
let chunk_len = chunk_info.chunk_len as usize;
let end = chunk_info.offset as usize;
if end < chunk_len {
bail!("got chunk with small offset ({} < {}", end, chunk_len);
}
let pos = end - 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.chunk_size-1) != 0 { bail!("add unaligned chunk (pos = {})", pos); }
if (end as u64) != chunk_info.offset {
bail!("got chunk with wrong offset ({} != {}", end, chunk_info.offset);
}
let (is_duplicate, compressed_size) = self.store.insert_chunk(&chunk_info.chunk)?;
stat.chunk_count += 1;
stat.compressed_size += compressed_size;
let digest = chunk_info.chunk.digest();
println!("ADD CHUNK {} {} {}% {} {}", pos, chunk_len,
(compressed_size*100)/(chunk_len as u64), is_duplicate, proxmox::tools::digest_to_hex(digest));
if is_duplicate {
stat.duplicate_chunks += 1;
} else {
stat.disk_size += compressed_size;
}
self.add_digest(pos / self.chunk_size, digest)
}
pub fn add_digest(&mut self, index: usize, digest: &[u8; 32]) -> Result<(), Error> {
if index >= self.index_length {
bail!("add digest failed - index out of range ({} >= {})", index, self.index_length);
}
if self.index == std::ptr::null_mut() { bail!("cannot write to closed index file."); }
let index_pos = index*32;
unsafe {
let dst = self.index.add(index_pos);
dst.copy_from_nonoverlapping(digest.as_ptr(), 32);
}
Ok(())
}
}