]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/datastore.rs
datastore: gc: comment exclusive process lock
[proxmox-backup.git] / src / backup / datastore.rs
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;
6
7 use anyhow::{bail, format_err, Error};
8 use lazy_static::lazy_static;
9 use serde_json::Value;
10
11 use proxmox::tools::fs::{replace_file, CreateOptions};
12
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};
18 use super::index::*;
19 use super::{DataBlob, ArchiveType, archive_type};
20 use crate::config::datastore;
21 use crate::server::WorkerTask;
22 use crate::tools;
23 use crate::tools::format::HumanByte;
24 use crate::tools::fs::{lock_dir_noblock, DirLockGuard};
25 use crate::api2::types::{GarbageCollectionStatus, Userid};
26
27 lazy_static! {
28 static ref DATASTORE_MAP: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new());
29 }
30
31 /// Datastore Management
32 ///
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>,
39 }
40
41 impl DataStore {
42
43 pub fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
44
45 let (config, _digest) = datastore::config()?;
46 let config: datastore::DataStoreConfig = config.lookup("datastore", name)?;
47
48 let mut map = DATASTORE_MAP.lock().unwrap();
49
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());
54 }
55 }
56
57 let datastore = DataStore::open(name)?;
58
59 let datastore = Arc::new(datastore);
60 map.insert(name.to_string(), datastore.clone());
61
62 Ok(datastore)
63 }
64
65 pub fn open(store_name: &str) -> Result<Self, Error> {
66
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))?;
70
71 let path = store_config["path"].as_str().unwrap();
72
73 Self::open_with_path(store_name, Path::new(path))
74 }
75
76 pub fn open_with_path(store_name: &str, path: &Path) -> Result<Self, Error> {
77 let chunk_store = ChunkStore::open(store_name, path)?;
78
79 let gc_status = GarbageCollectionStatus::default();
80
81 Ok(Self {
82 chunk_store: Arc::new(chunk_store),
83 gc_mutex: Mutex::new(false),
84 last_gc_status: Mutex::new(gc_status),
85 })
86 }
87
88 pub fn get_chunk_iterator(
89 &self,
90 ) -> Result<
91 impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize, bool)>,
92 Error
93 > {
94 self.chunk_store.get_chunk_iterator()
95 }
96
97 pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> {
98
99 let index = FixedIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?;
100
101 Ok(index)
102 }
103
104 pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> {
105
106 let full_path = self.chunk_store.relative_path(filename.as_ref());
107
108 let index = FixedIndexReader::open(&full_path)?;
109
110 Ok(index)
111 }
112
113 pub fn create_dynamic_writer<P: AsRef<Path>>(
114 &self, filename: P,
115 ) -> Result<DynamicIndexWriter, Error> {
116
117 let index = DynamicIndexWriter::create(
118 self.chunk_store.clone(), filename.as_ref())?;
119
120 Ok(index)
121 }
122
123 pub fn open_dynamic_reader<P: AsRef<Path>>(&self, filename: P) -> Result<DynamicIndexReader, Error> {
124
125 let full_path = self.chunk_store.relative_path(filename.as_ref());
126
127 let index = DynamicIndexReader::open(&full_path)?;
128
129 Ok(index)
130 }
131
132 pub fn open_index<P>(&self, filename: P) -> Result<Box<dyn IndexFile + Send>, Error>
133 where
134 P: AsRef<Path>,
135 {
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),
142 };
143 Ok(out)
144 }
145
146 pub fn name(&self) -> &str {
147 self.chunk_store.name()
148 }
149
150 pub fn base_path(&self) -> PathBuf {
151 self.chunk_store.base_path()
152 }
153
154 /// Cleanup a backup directory
155 ///
156 /// Removes all files not mentioned in the manifest.
157 pub fn cleanup_backup_dir(&self, backup_dir: &BackupDir, manifest: &BackupManifest
158 ) -> Result<(), Error> {
159
160 let mut full_path = self.base_path();
161 full_path.push(backup_dir.relative_path());
162
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()); });
167
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; }
172 }
173 let file_name = item.file_name().to_bytes();
174 if file_name == b"." || file_name == b".." { continue; };
175
176 if let Ok(name) = std::str::from_utf8(file_name) {
177 if wanted_files.contains(name) { continue; }
178 }
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) };
182 }
183 }
184
185 Ok(())
186 }
187
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());
192 full_path
193 }
194
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());
199 full_path
200 }
201
202 /// Remove a complete backup group including all snapshots
203 pub fn remove_backup_group(&self, backup_group: &BackupGroup) -> Result<(), Error> {
204
205 let full_path = self.group_path(backup_group);
206
207 let _guard = tools::fs::lock_dir_noblock(&full_path, "backup group", "possible running backup")?;
208
209 log::info!("removing backup group {:?}", full_path);
210 std::fs::remove_dir_all(&full_path)
211 .map_err(|err| {
212 format_err!(
213 "removing backup group {:?} failed - {}",
214 full_path,
215 err,
216 )
217 })?;
218
219 Ok(())
220 }
221
222 /// Remove a backup directory including all content
223 pub fn remove_backup_dir(&self, backup_dir: &BackupDir, force: bool) -> Result<(), Error> {
224
225 let full_path = self.snapshot_path(backup_dir);
226
227 let _guard;
228 if !force {
229 _guard = lock_dir_noblock(&full_path, "snapshot", "possibly running or used as base")?;
230 }
231
232 log::info!("removing backup snapshot {:?}", full_path);
233 std::fs::remove_dir_all(&full_path)
234 .map_err(|err| {
235 format_err!(
236 "removing backup snapshot {:?} failed - {}",
237 full_path,
238 err,
239 )
240 })?;
241
242 Ok(())
243 }
244
245 /// Returns the time of the last successful backup
246 ///
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());
252
253 if group_path.exists() {
254 backup_group.last_successful_backup(&base_path)
255 } else {
256 Ok(None)
257 }
258 }
259
260 /// Returns the backup owner.
261 ///
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
269 }
270
271 /// Set the backup owner.
272 pub fn set_owner(
273 &self,
274 backup_group: &BackupGroup,
275 userid: &Userid,
276 force: bool,
277 ) -> Result<(), Error> {
278 let mut path = self.base_path();
279 path.push(backup_group.group_path());
280 path.push("owner");
281
282 let mut open_options = std::fs::OpenOptions::new();
283 open_options.write(true);
284 open_options.truncate(true);
285
286 if force {
287 open_options.create(true);
288 } else {
289 open_options.create_new(true);
290 }
291
292 let mut file = open_options.open(&path)
293 .map_err(|err| format_err!("unable to create owner file {:?} - {}", path, err))?;
294
295 write!(file, "{}\n", userid)
296 .map_err(|err| format_err!("unable to write owner file {:?} - {}", path, err))?;
297
298 Ok(())
299 }
300
301 /// Create (if it does not already exists) and lock a backup group
302 ///
303 /// And set the owner to 'userid'. If the group already exists, it returns the
304 /// current owner (instead of setting the owner).
305 ///
306 /// This also acquires an exclusive lock on the directory and returns the lock guard.
307 pub fn create_locked_backup_group(
308 &self,
309 backup_group: &BackupGroup,
310 userid: &Userid,
311 ) -> Result<(Userid, DirLockGuard), Error> {
312 // create intermediate path first:
313 let base_path = self.base_path();
314
315 let mut full_path = base_path.clone();
316 full_path.push(backup_group.backup_type());
317 std::fs::create_dir_all(&full_path)?;
318
319 full_path.push(backup_group.backup_id());
320
321 // create the last component now
322 match std::fs::create_dir(&full_path) {
323 Ok(_) => {
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
327 Ok((owner, guard))
328 }
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
332 Ok((owner, guard))
333 }
334 Err(err) => bail!("unable to create backup group {:?} - {}", full_path, err),
335 }
336 }
337
338 /// Creates a new backup snapshot inside a BackupGroup
339 ///
340 /// The BackupGroup directory needs to exist.
341 pub fn create_locked_backup_dir(&self, backup_dir: &BackupDir)
342 -> Result<(PathBuf, bool, DirLockGuard), Error>
343 {
344 let relative_path = backup_dir.relative_path();
345 let mut full_path = self.base_path();
346 full_path.push(&relative_path);
347
348 let lock = ||
349 lock_dir_noblock(&full_path, "snapshot", "internal error - tried creating snapshot that's already in use");
350
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())
355 }
356 }
357
358 pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
359 let base = self.base_path();
360
361 let mut list = vec![];
362
363 use walkdir::WalkDir;
364
365 let walker = WalkDir::new(&base).same_file_system(true).into_iter();
366
367 // make sure we skip .chunks (and other hidden files to keep it simple)
368 fn is_hidden(entry: &walkdir::DirEntry) -> bool {
369 entry.file_name()
370 .to_str()
371 .map(|s| s.starts_with("."))
372 .unwrap_or(false)
373 }
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(""));
377 match inner.kind() {
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())
383 }
384 },
385 _ => bail!("unexpected error on datastore traversal: {} - {}", inner, path.display()),
386 }
387 }
388 Ok(())
389 };
390 for entry in walker.filter_entry(|e| !is_hidden(e)) {
391 let path = match entry {
392 Ok(entry) => entry.into_path(),
393 Err(err) => {
394 handle_entry_err(err)?;
395 continue
396 },
397 };
398 if let Ok(archive_type) = archive_type(&path) {
399 if archive_type == ArchiveType::FixedIndex || archive_type == ArchiveType::DynamicIndex {
400 list.push(path);
401 }
402 }
403 }
404
405 Ok(list)
406 }
407
408 // mark chunks used by ``index`` as used
409 fn index_mark_used_chunks<I: IndexFile>(
410 &self,
411 index: I,
412 file_name: &Path, // only used for error reporting
413 status: &mut GarbageCollectionStatus,
414 worker: &WorkerTask,
415 ) -> Result<(), Error> {
416
417 status.index_file_count += 1;
418 status.index_data_bytes += index.index_bytes();
419
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));
427 }
428 }
429 Ok(())
430 }
431
432 fn mark_used_chunks(&self, status: &mut GarbageCollectionStatus, worker: &WorkerTask) -> Result<(), Error> {
433
434 let image_list = self.list_images()?;
435
436 let image_count = image_list.len();
437
438 let mut done = 0;
439
440 let mut last_percentage: usize = 0;
441
442 for path in image_list {
443
444 worker.fail_on_abort()?;
445 tools::fail_on_shutdown()?;
446
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)?;
454 }
455 }
456 done += 1;
457
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;
463 }
464 }
465
466 Ok(())
467 }
468
469 pub fn last_gc_status(&self) -> GarbageCollectionStatus {
470 self.last_gc_status.lock().unwrap().clone()
471 }
472
473 pub fn garbage_collection_running(&self) -> bool {
474 if let Ok(_) = self.gc_mutex.try_lock() { false } else { true }
475 }
476
477 pub fn garbage_collection(&self, worker: &WorkerTask) -> Result<(), Error> {
478
479 if let Ok(ref mut _mutex) = self.gc_mutex.try_lock() {
480
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()?;
485
486 let phase1_start_time = unsafe { libc::time(std::ptr::null_mut()) };
487 let oldest_writer = self.chunk_store.oldest_writer().unwrap_or(phase1_start_time);
488
489 let mut gc_status = GarbageCollectionStatus::default();
490 gc_status.upid = Some(worker.to_string());
491
492 worker.log("Start GC phase1 (mark used chunks)");
493
494 self.mark_used_chunks(&mut gc_status, &worker)?;
495
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)?;
498
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));
503 }
504 if gc_status.removed_bad > 0 {
505 worker.log(&format!("Removed bad files: {}", gc_status.removed_bad));
506 }
507
508 worker.log(&format!("Original data usage: {}", HumanByte::from(gc_status.index_data_bytes)));
509
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));
513 }
514
515 worker.log(&format!("On-Disk chunks: {}", gc_status.disk_chunks));
516
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)));
520 }
521
522 *self.last_gc_status.lock().unwrap() = gc_status;
523
524 } else {
525 bail!("Start GC failed - (already running/locked)");
526 }
527
528 Ok(())
529 }
530
531 pub fn try_shared_chunk_store_lock(&self) -> Result<tools::ProcessLockSharedGuard, Error> {
532 self.chunk_store.try_shared_lock()
533 }
534
535 pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) {
536 self.chunk_store.chunk_path(digest)
537 }
538
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)
541 }
542
543 pub fn insert_chunk(
544 &self,
545 chunk: &DataBlob,
546 digest: &[u8; 32],
547 ) -> Result<(bool, u64), Error> {
548 self.chunk_store.insert_chunk(chunk, digest)
549 }
550
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());
554 path.push(filename);
555
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))
560 }
561
562
563 pub fn load_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
564
565 let (chunk_path, digest_str) = self.chunk_store.chunk_path(digest);
566
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 '{}' - {}",
572 self.name(),
573 digest_str,
574 err,
575 ))
576 }
577
578 pub fn load_manifest(
579 &self,
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))
586 }
587
588 pub fn load_manifest_json(
589 &self,
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[..])?;
596 Ok(manifest)
597 }
598
599 pub fn store_manifest(
600 &self,
601 backup_dir: &BackupDir,
602 manifest: Value,
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();
607
608 let mut path = self.base_path();
609 path.push(backup_dir.relative_path());
610 path.push(MANIFEST_BLOB_NAME);
611
612 replace_file(&path, raw_data, CreateOptions::new())?;
613
614 Ok(())
615 }
616 }