3 use std
::os
::unix
::io
::AsRawFd
;
4 use std
::path
::{Path, PathBuf}
;
6 use std
::io
::{Seek, SeekFrom}
;
8 use anyhow
::{bail, format_err, Error}
;
10 use pbs_tools
::process_locker
::ProcessLockSharedGuard
;
12 use proxmox_io
::ReadExt
;
13 use proxmox_uuid
::Uuid
;
15 use crate::chunk_stat
::ChunkStat
;
16 use crate::chunk_store
::ChunkStore
;
17 use crate::data_blob
::ChunkInfo
;
18 use crate::file_formats
;
19 use crate::index
::{ChunkReadInfo, IndexFile}
;
21 /// Header format definition for fixed index files (`.fidx`)
23 pub struct FixedIndexHeader
{
27 /// Sha256 over the index ``SHA256(digest1||digest2||...)``
28 pub index_csum
: [u8; 32],
31 reserved
: [u8; 4016], // overall size is one page (4096 bytes)
33 proxmox_lang
::static_assert_size
!(FixedIndexHeader
, 4096);
35 // split image into fixed size chunks
37 pub struct FixedIndexReader
{
39 pub chunk_size
: usize,
45 pub index_csum
: [u8; 32],
48 // `index` is mmap()ed which cannot be thread-local so should be sendable
49 unsafe impl Send
for FixedIndexReader {}
50 unsafe impl Sync
for FixedIndexReader {}
52 impl Drop
for FixedIndexReader
{
54 if let Err(err
) = self.unmap() {
55 eprintln
!("Unable to unmap file - {}", err
);
60 impl FixedIndexReader
{
61 pub fn open(path
: &Path
) -> Result
<Self, Error
> {
65 .map_err(|err
| format_err
!("Unable to open fixed index {:?} - {}", path
, err
))
68 pub fn new(mut file
: std
::fs
::File
) -> Result
<Self, Error
> {
69 file
.seek(SeekFrom
::Start(0))?
;
71 let header_size
= std
::mem
::size_of
::<FixedIndexHeader
>();
73 let stat
= match nix
::sys
::stat
::fstat(file
.as_raw_fd()) {
75 Err(err
) => bail
!("fstat failed - {}", err
),
78 let size
= stat
.st_size
as usize;
80 if size
< header_size
{
81 bail
!("index too small ({})", stat
.st_size
);
84 let header
: Box
<FixedIndexHeader
> = unsafe { file.read_host_value_boxed()? }
;
86 if header
.magic
!= file_formats
::FIXED_SIZED_CHUNK_INDEX_1_0
{
87 bail
!("got unknown magic number");
90 let size
= u64::from_le(header
.size
);
91 let ctime
= i64::from_le(header
.ctime
);
92 let chunk_size
= u64::from_le(header
.chunk_size
);
94 let index_length
= ((size
+ chunk_size
- 1) / chunk_size
) as usize;
95 let index_size
= index_length
* 32;
97 let expected_index_size
= (stat
.st_size
as usize) - header_size
;
98 if index_size
!= expected_index_size
{
100 "got unexpected file size ({} != {})",
107 nix
::sys
::mman
::mmap(
108 std
::ptr
::null_mut(),
110 nix
::sys
::mman
::ProtFlags
::PROT_READ
,
111 nix
::sys
::mman
::MapFlags
::MAP_PRIVATE
,
119 chunk_size
: chunk_size
as usize,
125 index_csum
: header
.index_csum
,
129 fn unmap(&mut self) -> Result
<(), Error
> {
130 if self.index
.is_null() {
134 let index_size
= self.index_length
* 32;
137 unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) }
139 bail
!("unmap file failed - {}", err
);
142 self.index
= std
::ptr
::null_mut();
147 pub fn print_info(&self) {
148 println
!("Size: {}", self.size
);
149 println
!("ChunkSize: {}", self.chunk_size
);
151 let mut ctime_str
= self.ctime
.to_string();
152 if let Ok(s
) = proxmox_time
::strftime_local("%c", self.ctime
) {
156 println
!("CTime: {}", ctime_str
);
157 println
!("UUID: {:?}", self.uuid
);
161 impl IndexFile
for FixedIndexReader
{
162 fn index_count(&self) -> usize {
166 fn index_digest(&self, pos
: usize) -> Option
<&[u8; 32]> {
167 if pos
>= self.index_length
{
170 Some(unsafe { &*(self.index.add(pos * 32) as *const [u8; 32]) }
)
174 fn index_bytes(&self) -> u64 {
178 fn chunk_info(&self, pos
: usize) -> Option
<ChunkReadInfo
> {
179 if pos
>= self.index_length
{
183 let start
= (pos
* self.chunk_size
) as u64;
184 let mut end
= start
+ self.chunk_size
as u64;
190 let digest
= self.index_digest(pos
).unwrap();
197 fn index_ctime(&self) -> i64 {
201 fn index_size(&self) -> usize {
205 fn compute_csum(&self) -> ([u8; 32], u64) {
206 let mut csum
= openssl
::sha
::Sha256
::new();
207 let mut chunk_end
= 0;
208 for pos
in 0..self.index_count() {
209 let info
= self.chunk_info(pos
).unwrap();
210 chunk_end
= info
.range
.end
;
211 csum
.update(&info
.digest
);
213 let csum
= csum
.finish();
218 fn chunk_from_offset(&self, offset
: u64) -> Option
<(usize, u64)> {
219 if offset
>= self.size
{
224 (offset
/ self.chunk_size
as u64) as usize,
225 offset
& (self.chunk_size
- 1) as u64, // fast modulo, valid for 2^x chunk_size
230 pub struct FixedIndexWriter
{
231 store
: Arc
<ChunkStore
>,
233 _lock
: ProcessLockSharedGuard
,
235 tmp_filename
: PathBuf
,
244 // `index` is mmap()ed which cannot be thread-local so should be sendable
245 unsafe impl Send
for FixedIndexWriter {}
247 impl Drop
for FixedIndexWriter
{
249 let _
= std
::fs
::remove_file(&self.tmp_filename
); // ignore errors
250 if let Err(err
) = self.unmap() {
251 eprintln
!("Unable to unmap file {:?} - {}", self.tmp_filename
, err
);
256 impl FixedIndexWriter
{
257 #[allow(clippy::cast_ptr_alignment)]
259 store
: Arc
<ChunkStore
>,
263 ) -> Result
<Self, Error
> {
264 let shared_lock
= store
.try_shared_lock()?
;
266 let full_path
= store
.relative_path(path
);
267 let mut tmp_path
= full_path
.clone();
268 tmp_path
.set_extension("tmp_fidx");
270 let mut file
= std
::fs
::OpenOptions
::new()
277 let header_size
= std
::mem
::size_of
::<FixedIndexHeader
>();
279 // todo: use static assertion when available in rust
280 if header_size
!= 4096 {
281 panic
!("got unexpected header size");
284 let ctime
= proxmox_time
::epoch_i64();
286 let uuid
= Uuid
::generate();
288 let buffer
= vec
![0u8; header_size
];
289 let header
= unsafe { &mut *(buffer.as_ptr() as *mut FixedIndexHeader) }
;
291 header
.magic
= file_formats
::FIXED_SIZED_CHUNK_INDEX_1_0
;
292 header
.ctime
= i64::to_le(ctime
);
293 header
.size
= u64::to_le(size
as u64);
294 header
.chunk_size
= u64::to_le(chunk_size
as u64);
295 header
.uuid
= *uuid
.as_bytes();
297 header
.index_csum
= [0u8; 32];
299 file
.write_all(&buffer
)?
;
301 let index_length
= (size
+ chunk_size
- 1) / chunk_size
;
302 let index_size
= index_length
* 32;
303 nix
::unistd
::ftruncate(file
.as_raw_fd(), (header_size
+ index_size
) as i64)?
;
306 nix
::sys
::mman
::mmap(
307 std
::ptr
::null_mut(),
309 nix
::sys
::mman
::ProtFlags
::PROT_READ
| nix
::sys
::mman
::ProtFlags
::PROT_WRITE
,
310 nix
::sys
::mman
::MapFlags
::MAP_SHARED
,
321 tmp_filename
: tmp_path
,
327 uuid
: *uuid
.as_bytes(),
331 pub fn index_length(&self) -> usize {
335 fn unmap(&mut self) -> Result
<(), Error
> {
336 if self.index
.is_null() {
340 let index_size
= self.index_length
* 32;
343 unsafe { nix::sys::mman::munmap(self.index as *mut std::ffi::c_void, index_size) }
345 bail
!("unmap file {:?} failed - {}", self.tmp_filename
, err
);
348 self.index
= std
::ptr
::null_mut();
353 pub fn close(&mut self) -> Result
<[u8; 32], Error
> {
354 if self.index
.is_null() {
355 bail
!("cannot close already closed index file.");
358 let index_size
= self.index_length
* 32;
359 let data
= unsafe { std::slice::from_raw_parts(self.index, index_size) }
;
360 let index_csum
= openssl
::sha
::sha256(data
);
364 let csum_offset
= proxmox_lang
::offsetof!(FixedIndexHeader
, index_csum
);
365 self.file
.seek(SeekFrom
::Start(csum_offset
as u64))?
;
366 self.file
.write_all(&index_csum
)?
;
369 if let Err(err
) = std
::fs
::rename(&self.tmp_filename
, &self.filename
) {
370 bail
!("Atomic rename file {:?} failed - {}", self.filename
, err
);
376 pub fn check_chunk_alignment(&self, offset
: usize, chunk_len
: usize) -> Result
<usize, Error
> {
377 if offset
< chunk_len
{
378 bail
!("got chunk with small offset ({} < {}", offset
, chunk_len
);
381 let pos
= offset
- chunk_len
;
383 if offset
> self.size
{
384 bail
!("chunk data exceeds size ({} >= {})", offset
, self.size
);
387 // last chunk can be smaller
388 if ((offset
!= self.size
) && (chunk_len
!= self.chunk_size
))
389 || (chunk_len
> self.chunk_size
)
393 "chunk with unexpected length ({} != {}",
399 if pos
& (self.chunk_size
- 1) != 0 {
400 bail
!("got unaligned chunk (pos = {})", pos
);
403 Ok(pos
/ self.chunk_size
)
406 // Note: We want to add data out of order, so do not assume any order here.
407 pub fn add_chunk(&mut self, chunk_info
: &ChunkInfo
, stat
: &mut ChunkStat
) -> Result
<(), Error
> {
408 let chunk_len
= chunk_info
.chunk_len
as usize;
409 let offset
= chunk_info
.offset
as usize; // end of chunk
411 let idx
= self.check_chunk_alignment(offset
, chunk_len
)?
;
413 let (is_duplicate
, compressed_size
) = self
415 .insert_chunk(&chunk_info
.chunk
, &chunk_info
.digest
)?
;
417 stat
.chunk_count
+= 1;
418 stat
.compressed_size
+= compressed_size
;
420 let digest
= &chunk_info
.digest
;
423 "ADD CHUNK {} {} {}% {} {}",
426 (compressed_size
* 100) / (chunk_len
as u64),
428 proxmox
::tools
::digest_to_hex(digest
)
432 stat
.duplicate_chunks
+= 1;
434 stat
.disk_size
+= compressed_size
;
437 self.add_digest(idx
, digest
)
440 pub fn add_digest(&mut self, index
: usize, digest
: &[u8; 32]) -> Result
<(), Error
> {
441 if index
>= self.index_length
{
443 "add digest failed - index out of range ({} >= {})",
449 if self.index
.is_null() {
450 bail
!("cannot write to closed index file.");
453 let index_pos
= index
* 32;
455 let dst
= self.index
.add(index_pos
);
456 dst
.copy_from_nonoverlapping(digest
.as_ptr(), 32);
462 pub fn clone_data_from(&mut self, reader
: &FixedIndexReader
) -> Result
<(), Error
> {
463 if self.index_length
!= reader
.index_count() {
464 bail
!("clone_data_from failed - index sizes not equal");
467 for i
in 0..self.index_length
{
468 self.add_digest(i
, reader
.index_digest(i
).unwrap())?
;