1 use std
::collections
::{HashSet, HashMap}
;
2 use std
::io
::{self, Write}
;
3 use std
::path
::{Path, PathBuf}
;
4 use std
::sync
::{Arc, Mutex}
;
5 use std
::convert
::TryFrom
;
7 use anyhow
::{bail, format_err, Error}
;
8 use lazy_static
::lazy_static
;
11 use proxmox
::tools
::fs
::{replace_file, CreateOptions}
;
13 use super::backup_info
::{BackupGroup, BackupDir}
;
14 use super::chunk_store
::ChunkStore
;
15 use super::dynamic_index
::{DynamicIndexReader, DynamicIndexWriter}
;
16 use super::fixed_index
::{FixedIndexReader, FixedIndexWriter}
;
17 use super::manifest
::{MANIFEST_BLOB_NAME, CLIENT_LOG_BLOB_NAME, BackupManifest}
;
19 use super::{DataBlob, ArchiveType, archive_type}
;
20 use crate::config
::datastore
;
21 use crate::server
::WorkerTask
;
23 use crate::tools
::format
::HumanByte
;
24 use crate::tools
::fs
::{lock_dir_noblock, DirLockGuard}
;
25 use crate::api2
::types
::{GarbageCollectionStatus, Userid}
;
28 static ref DATASTORE_MAP
: Mutex
<HashMap
<String
, Arc
<DataStore
>>> = Mutex
::new(HashMap
::new());
31 /// Datastore Management
33 /// A Datastore can store severals backups, and provides the
34 /// management interface for backup.
35 pub struct DataStore
{
36 chunk_store
: Arc
<ChunkStore
>,
37 gc_mutex
: Mutex
<bool
>,
38 last_gc_status
: Mutex
<GarbageCollectionStatus
>,
43 pub fn lookup_datastore(name
: &str) -> Result
<Arc
<DataStore
>, Error
> {
45 let (config
, _digest
) = datastore
::config()?
;
46 let config
: datastore
::DataStoreConfig
= config
.lookup("datastore", name
)?
;
48 let mut map
= DATASTORE_MAP
.lock().unwrap();
50 if let Some(datastore
) = map
.get(name
) {
51 // Compare Config - if changed, create new Datastore object!
52 if datastore
.chunk_store
.base
== PathBuf
::from(&config
.path
) {
53 return Ok(datastore
.clone());
57 let datastore
= DataStore
::open(name
)?
;
59 let datastore
= Arc
::new(datastore
);
60 map
.insert(name
.to_string(), datastore
.clone());
65 pub fn open(store_name
: &str) -> Result
<Self, Error
> {
67 let (config
, _digest
) = datastore
::config()?
;
68 let (_
, store_config
) = config
.sections
.get(store_name
)
69 .ok_or(format_err
!("no such datastore '{}'", store_name
))?
;
71 let path
= store_config
["path"].as_str().unwrap();
73 Self::open_with_path(store_name
, Path
::new(path
))
76 pub fn open_with_path(store_name
: &str, path
: &Path
) -> Result
<Self, Error
> {
77 let chunk_store
= ChunkStore
::open(store_name
, path
)?
;
79 let gc_status
= GarbageCollectionStatus
::default();
82 chunk_store
: Arc
::new(chunk_store
),
83 gc_mutex
: Mutex
::new(false),
84 last_gc_status
: Mutex
::new(gc_status
),
88 pub fn get_chunk_iterator(
91 impl Iterator
<Item
= (Result
<tools
::fs
::ReadDirEntry
, Error
>, usize, bool
)>,
94 self.chunk_store
.get_chunk_iterator()
97 pub fn create_fixed_writer
<P
: AsRef
<Path
>>(&self, filename
: P
, size
: usize, chunk_size
: usize) -> Result
<FixedIndexWriter
, Error
> {
99 let index
= FixedIndexWriter
::create(self.chunk_store
.clone(), filename
.as_ref(), size
, chunk_size
)?
;
104 pub fn open_fixed_reader
<P
: AsRef
<Path
>>(&self, filename
: P
) -> Result
<FixedIndexReader
, Error
> {
106 let full_path
= self.chunk_store
.relative_path(filename
.as_ref());
108 let index
= FixedIndexReader
::open(&full_path
)?
;
113 pub fn create_dynamic_writer
<P
: AsRef
<Path
>>(
115 ) -> Result
<DynamicIndexWriter
, Error
> {
117 let index
= DynamicIndexWriter
::create(
118 self.chunk_store
.clone(), filename
.as_ref())?
;
123 pub fn open_dynamic_reader
<P
: AsRef
<Path
>>(&self, filename
: P
) -> Result
<DynamicIndexReader
, Error
> {
125 let full_path
= self.chunk_store
.relative_path(filename
.as_ref());
127 let index
= DynamicIndexReader
::open(&full_path
)?
;
132 pub fn open_index
<P
>(&self, filename
: P
) -> Result
<Box
<dyn IndexFile
+ Send
>, Error
>
136 let filename
= filename
.as_ref();
137 let out
: Box
<dyn IndexFile
+ Send
> =
138 match archive_type(filename
)?
{
139 ArchiveType
::DynamicIndex
=> Box
::new(self.open_dynamic_reader(filename
)?
),
140 ArchiveType
::FixedIndex
=> Box
::new(self.open_fixed_reader(filename
)?
),
141 _
=> bail
!("cannot open index file of unknown type: {:?}", filename
),
146 pub fn name(&self) -> &str {
147 self.chunk_store
.name()
150 pub fn base_path(&self) -> PathBuf
{
151 self.chunk_store
.base_path()
154 /// Cleanup a backup directory
156 /// Removes all files not mentioned in the manifest.
157 pub fn cleanup_backup_dir(&self, backup_dir
: &BackupDir
, manifest
: &BackupManifest
158 ) -> Result
<(), Error
> {
160 let mut full_path
= self.base_path();
161 full_path
.push(backup_dir
.relative_path());
163 let mut wanted_files
= HashSet
::new();
164 wanted_files
.insert(MANIFEST_BLOB_NAME
.to_string());
165 wanted_files
.insert(CLIENT_LOG_BLOB_NAME
.to_string());
166 manifest
.files().iter().for_each(|item
| { wanted_files.insert(item.filename.clone()); }
);
168 for item
in tools
::fs
::read_subdir(libc
::AT_FDCWD
, &full_path
)?
{
169 if let Ok(item
) = item
{
170 if let Some(file_type
) = item
.file_type() {
171 if file_type
!= nix
::dir
::Type
::File { continue; }
173 let file_name
= item
.file_name().to_bytes();
174 if file_name
== b
"." || file_name
== b
".." { continue; }
;
176 if let Ok(name
) = std
::str::from_utf8(file_name
) {
177 if wanted_files
.contains(name
) { continue; }
179 println
!("remove unused file {:?}", item
.file_name());
180 let dirfd
= item
.parent_fd();
181 let _res
= unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) }
;
188 /// Returns the absolute path for a backup_group
189 pub fn group_path(&self, backup_group
: &BackupGroup
) -> PathBuf
{
190 let mut full_path
= self.base_path();
191 full_path
.push(backup_group
.group_path());
195 /// Returns the absolute path for backup_dir
196 pub fn snapshot_path(&self, backup_dir
: &BackupDir
) -> PathBuf
{
197 let mut full_path
= self.base_path();
198 full_path
.push(backup_dir
.relative_path());
202 /// Remove a complete backup group including all snapshots
203 pub fn remove_backup_group(&self, backup_group
: &BackupGroup
) -> Result
<(), Error
> {
205 let full_path
= self.group_path(backup_group
);
207 let _guard
= tools
::fs
::lock_dir_noblock(&full_path
, "backup group", "possible running backup")?
;
209 log
::info
!("removing backup group {:?}", full_path
);
210 std
::fs
::remove_dir_all(&full_path
)
213 "removing backup group {:?} failed - {}",
222 /// Remove a backup directory including all content
223 pub fn remove_backup_dir(&self, backup_dir
: &BackupDir
, force
: bool
) -> Result
<(), Error
> {
225 let full_path
= self.snapshot_path(backup_dir
);
229 _guard
= lock_dir_noblock(&full_path
, "snapshot", "possibly running or used as base")?
;
232 log
::info
!("removing backup snapshot {:?}", full_path
);
233 std
::fs
::remove_dir_all(&full_path
)
236 "removing backup snapshot {:?} failed - {}",
245 /// Returns the time of the last successful backup
247 /// Or None if there is no backup in the group (or the group dir does not exist).
248 pub fn last_successful_backup(&self, backup_group
: &BackupGroup
) -> Result
<Option
<i64>, Error
> {
249 let base_path
= self.base_path();
250 let mut group_path
= base_path
.clone();
251 group_path
.push(backup_group
.group_path());
253 if group_path
.exists() {
254 backup_group
.last_successful_backup(&base_path
)
260 /// Returns the backup owner.
262 /// The backup owner is the user who first created the backup group.
263 pub fn get_owner(&self, backup_group
: &BackupGroup
) -> Result
<Userid
, Error
> {
264 let mut full_path
= self.base_path();
265 full_path
.push(backup_group
.group_path());
266 full_path
.push("owner");
267 let owner
= proxmox
::tools
::fs
::file_read_firstline(full_path
)?
;
268 Ok(owner
.trim_end().parse()?
) // remove trailing newline
271 /// Set the backup owner.
274 backup_group
: &BackupGroup
,
277 ) -> Result
<(), Error
> {
278 let mut path
= self.base_path();
279 path
.push(backup_group
.group_path());
282 let mut open_options
= std
::fs
::OpenOptions
::new();
283 open_options
.write(true);
284 open_options
.truncate(true);
287 open_options
.create(true);
289 open_options
.create_new(true);
292 let mut file
= open_options
.open(&path
)
293 .map_err(|err
| format_err
!("unable to create owner file {:?} - {}", path
, err
))?
;
295 write
!(file
, "{}\n", userid
)
296 .map_err(|err
| format_err
!("unable to write owner file {:?} - {}", path
, err
))?
;
301 /// Create (if it does not already exists) and lock a backup group
303 /// And set the owner to 'userid'. If the group already exists, it returns the
304 /// current owner (instead of setting the owner).
306 /// This also acquires an exclusive lock on the directory and returns the lock guard.
307 pub fn create_locked_backup_group(
309 backup_group
: &BackupGroup
,
311 ) -> Result
<(Userid
, DirLockGuard
), Error
> {
312 // create intermediate path first:
313 let base_path
= self.base_path();
315 let mut full_path
= base_path
.clone();
316 full_path
.push(backup_group
.backup_type());
317 std
::fs
::create_dir_all(&full_path
)?
;
319 full_path
.push(backup_group
.backup_id());
321 // create the last component now
322 match std
::fs
::create_dir(&full_path
) {
324 let guard
= lock_dir_noblock(&full_path
, "backup group", "another backup is already running")?
;
325 self.set_owner(backup_group
, userid
, false)?
;
326 let owner
= self.get_owner(backup_group
)?
; // just to be sure
329 Err(ref err
) if err
.kind() == io
::ErrorKind
::AlreadyExists
=> {
330 let guard
= lock_dir_noblock(&full_path
, "backup group", "another backup is already running")?
;
331 let owner
= self.get_owner(backup_group
)?
; // just to be sure
334 Err(err
) => bail
!("unable to create backup group {:?} - {}", full_path
, err
),
338 /// Creates a new backup snapshot inside a BackupGroup
340 /// The BackupGroup directory needs to exist.
341 pub fn create_locked_backup_dir(&self, backup_dir
: &BackupDir
)
342 -> Result
<(PathBuf
, bool
, DirLockGuard
), Error
>
344 let relative_path
= backup_dir
.relative_path();
345 let mut full_path
= self.base_path();
346 full_path
.push(&relative_path
);
349 lock_dir_noblock(&full_path
, "snapshot", "internal error - tried creating snapshot that's already in use");
351 match std
::fs
::create_dir(&full_path
) {
352 Ok(_
) => Ok((relative_path
, true, lock()?
)),
353 Err(ref e
) if e
.kind() == io
::ErrorKind
::AlreadyExists
=> Ok((relative_path
, false, lock()?
)),
354 Err(e
) => Err(e
.into())
358 pub fn list_images(&self) -> Result
<Vec
<PathBuf
>, Error
> {
359 let base
= self.base_path();
361 let mut list
= vec
![];
363 use walkdir
::WalkDir
;
365 let walker
= WalkDir
::new(&base
).same_file_system(true).into_iter();
367 // make sure we skip .chunks (and other hidden files to keep it simple)
368 fn is_hidden(entry
: &walkdir
::DirEntry
) -> bool
{
371 .map(|s
| s
.starts_with("."))
374 let handle_entry_err
= |err
: walkdir
::Error
| {
375 if let Some(inner
) = err
.io_error() {
376 let path
= err
.path().unwrap_or(Path
::new(""));
378 io
::ErrorKind
::PermissionDenied
=> {
379 // only allow to skip ext4 fsck directory, avoid GC if, for example,
380 // a user got file permissions wrong on datastore rsync to new server
381 if err
.depth() > 1 || !path
.ends_with("lost+found") {
382 bail
!("cannot continue garbage-collection safely, permission denied on: {}", path
.display())
385 _
=> bail
!("unexpected error on datastore traversal: {} - {}", inner
, path
.display()),
390 for entry
in walker
.filter_entry(|e
| !is_hidden(e
)) {
391 let path
= match entry
{
392 Ok(entry
) => entry
.into_path(),
394 handle_entry_err(err
)?
;
398 if let Ok(archive_type
) = archive_type(&path
) {
399 if archive_type
== ArchiveType
::FixedIndex
|| archive_type
== ArchiveType
::DynamicIndex
{
408 // mark chunks used by ``index`` as used
409 fn index_mark_used_chunks
<I
: IndexFile
>(
412 file_name
: &Path
, // only used for error reporting
413 status
: &mut GarbageCollectionStatus
,
415 ) -> Result
<(), Error
> {
417 status
.index_file_count
+= 1;
418 status
.index_data_bytes
+= index
.index_bytes();
420 for pos
in 0..index
.index_count() {
421 worker
.fail_on_abort()?
;
422 tools
::fail_on_shutdown()?
;
423 let digest
= index
.index_digest(pos
).unwrap();
424 if let Err(err
) = self.chunk_store
.touch_chunk(digest
) {
425 worker
.warn(&format
!("warning: unable to access chunk {}, required by {:?} - {}",
426 proxmox
::tools
::digest_to_hex(digest
), file_name
, err
));
432 fn mark_used_chunks(&self, status
: &mut GarbageCollectionStatus
, worker
: &WorkerTask
) -> Result
<(), Error
> {
434 let image_list
= self.list_images()?
;
436 let image_count
= image_list
.len();
440 let mut last_percentage
: usize = 0;
442 for path
in image_list
{
444 worker
.fail_on_abort()?
;
445 tools
::fail_on_shutdown()?
;
447 if let Ok(archive_type
) = archive_type(&path
) {
448 if archive_type
== ArchiveType
::FixedIndex
{
449 let index
= self.open_fixed_reader(&path
)?
;
450 self.index_mark_used_chunks(index
, &path
, status
, worker
)?
;
451 } else if archive_type
== ArchiveType
::DynamicIndex
{
452 let index
= self.open_dynamic_reader(&path
)?
;
453 self.index_mark_used_chunks(index
, &path
, status
, worker
)?
;
458 let percentage
= done
*100/image_count
;
459 if percentage
> last_percentage
{
460 worker
.log(format
!("percentage done: phase1 {}% ({} of {} index files)",
461 percentage
, done
, image_count
));
462 last_percentage
= percentage
;
469 pub fn last_gc_status(&self) -> GarbageCollectionStatus
{
470 self.last_gc_status
.lock().unwrap().clone()
473 pub fn garbage_collection_running(&self) -> bool
{
474 if let Ok(_
) = self.gc_mutex
.try_lock() { false }
else { true }
477 pub fn garbage_collection(&self, worker
: &WorkerTask
) -> Result
<(), Error
> {
479 if let Ok(ref mut _mutex
) = self.gc_mutex
.try_lock() {
481 // avoids that we run GC if an old daemon process has still a
482 // running backup writer, which is not save as we have no "oldest
483 // writer" information and thus no safe atime cutoff
484 let _exclusive_lock
= self.chunk_store
.try_exclusive_lock()?
;
486 let phase1_start_time
= proxmox
::tools
::time
::epoch_i64();
487 let oldest_writer
= self.chunk_store
.oldest_writer().unwrap_or(phase1_start_time
);
489 let mut gc_status
= GarbageCollectionStatus
::default();
490 gc_status
.upid
= Some(worker
.to_string());
492 worker
.log("Start GC phase1 (mark used chunks)");
494 self.mark_used_chunks(&mut gc_status
, &worker
)?
;
496 worker
.log("Start GC phase2 (sweep unused chunks)");
497 self.chunk_store
.sweep_unused_chunks(oldest_writer
, phase1_start_time
, &mut gc_status
, &worker
)?
;
499 worker
.log(&format
!("Removed garbage: {}", HumanByte
::from(gc_status
.removed_bytes
)));
500 worker
.log(&format
!("Removed chunks: {}", gc_status
.removed_chunks
));
501 if gc_status
.pending_bytes
> 0 {
502 worker
.log(&format
!("Pending removals: {} (in {} chunks)", HumanByte
::from(gc_status
.pending_bytes
), gc_status
.pending_chunks
));
504 if gc_status
.removed_bad
> 0 {
505 worker
.log(&format
!("Removed bad files: {}", gc_status
.removed_bad
));
508 worker
.log(&format
!("Original data usage: {}", HumanByte
::from(gc_status
.index_data_bytes
)));
510 if gc_status
.index_data_bytes
> 0 {
511 let comp_per
= (gc_status
.disk_bytes
as f64 * 100.)/gc_status
.index_data_bytes
as f64;
512 worker
.log(&format
!("On-Disk usage: {} ({:.2}%)", HumanByte
::from(gc_status
.disk_bytes
), comp_per
));
515 worker
.log(&format
!("On-Disk chunks: {}", gc_status
.disk_chunks
));
517 if gc_status
.disk_chunks
> 0 {
518 let avg_chunk
= gc_status
.disk_bytes
/(gc_status
.disk_chunks
as u64);
519 worker
.log(&format
!("Average chunk size: {}", HumanByte
::from(avg_chunk
)));
522 *self.last_gc_status
.lock().unwrap() = gc_status
;
525 bail
!("Start GC failed - (already running/locked)");
531 pub fn try_shared_chunk_store_lock(&self) -> Result
<tools
::ProcessLockSharedGuard
, Error
> {
532 self.chunk_store
.try_shared_lock()
535 pub fn chunk_path(&self, digest
:&[u8; 32]) -> (PathBuf
, String
) {
536 self.chunk_store
.chunk_path(digest
)
539 pub fn cond_touch_chunk(&self, digest
: &[u8; 32], fail_if_not_exist
: bool
) -> Result
<bool
, Error
> {
540 self.chunk_store
.cond_touch_chunk(digest
, fail_if_not_exist
)
547 ) -> Result
<(bool
, u64), Error
> {
548 self.chunk_store
.insert_chunk(chunk
, digest
)
551 pub fn load_blob(&self, backup_dir
: &BackupDir
, filename
: &str) -> Result
<DataBlob
, Error
> {
552 let mut path
= self.base_path();
553 path
.push(backup_dir
.relative_path());
556 proxmox
::try_block
!({
557 let mut file
= std
::fs
::File
::open(&path
)?
;
558 DataBlob
::load_from_reader(&mut file
)
559 }).map_err(|err
| format_err
!("unable to load blob '{:?}' - {}", path
, err
))
563 pub fn load_chunk(&self, digest
: &[u8; 32]) -> Result
<DataBlob
, Error
> {
565 let (chunk_path
, digest_str
) = self.chunk_store
.chunk_path(digest
);
567 proxmox
::try_block
!({
568 let mut file
= std
::fs
::File
::open(&chunk_path
)?
;
569 DataBlob
::load_from_reader(&mut file
)
570 }).map_err(|err
| format_err
!(
571 "store '{}', unable to load chunk '{}' - {}",
578 pub fn load_manifest(
580 backup_dir
: &BackupDir
,
581 ) -> Result
<(BackupManifest
, u64), Error
> {
582 let blob
= self.load_blob(backup_dir
, MANIFEST_BLOB_NAME
)?
;
583 let raw_size
= blob
.raw_size();
584 let manifest
= BackupManifest
::try_from(blob
)?
;
585 Ok((manifest
, raw_size
))
588 pub fn load_manifest_json(
590 backup_dir
: &BackupDir
,
591 ) -> Result
<Value
, Error
> {
592 let blob
= self.load_blob(backup_dir
, MANIFEST_BLOB_NAME
)?
;
593 // no expected digest available
594 let manifest_data
= blob
.decode(None
, None
)?
;
595 let manifest
: Value
= serde_json
::from_slice(&manifest_data
[..])?
;
599 pub fn store_manifest(
601 backup_dir
: &BackupDir
,
603 ) -> Result
<(), Error
> {
604 let manifest
= serde_json
::to_string_pretty(&manifest
)?
;
605 let blob
= DataBlob
::encode(manifest
.as_bytes(), None
, true)?
;
606 let raw_data
= blob
.raw_data();
608 let mut path
= self.base_path();
609 path
.push(backup_dir
.relative_path());
610 path
.push(MANIFEST_BLOB_NAME
);
612 replace_file(&path
, raw_data
, CreateOptions
::new())?
;