]>
Commit | Line | Data |
---|---|---|
7759eef5 | 1 | use std::collections::{HashSet, HashMap}; |
54552dda | 2 | use std::io::{self, Write}; |
367f002e WB |
3 | use std::path::{Path, PathBuf}; |
4 | use std::sync::{Arc, Mutex}; | |
60f9a6ea | 5 | use std::convert::TryFrom; |
cb4b721c | 6 | use std::str::FromStr; |
1a374fcf SR |
7 | use std::time::Duration; |
8 | use std::fs::File; | |
367f002e | 9 | |
f7d4e4b5 | 10 | use anyhow::{bail, format_err, Error}; |
2c32fdde | 11 | use lazy_static::lazy_static; |
e4439025 | 12 | |
b683fd58 | 13 | use proxmox::tools::fs::{replace_file, file_read_optional_string, CreateOptions, open_file_locked}; |
529de6c7 | 14 | |
6d6b4e72 | 15 | use super::backup_info::{BackupGroup, BackupDir}; |
a92830dc | 16 | use super::chunk_store::ChunkStore; |
367f002e WB |
17 | use super::dynamic_index::{DynamicIndexReader, DynamicIndexWriter}; |
18 | use super::fixed_index::{FixedIndexReader, FixedIndexWriter}; | |
1a374fcf | 19 | use super::manifest::{MANIFEST_BLOB_NAME, MANIFEST_LOCK_NAME, CLIENT_LOG_BLOB_NAME, BackupManifest}; |
5de2bced | 20 | use super::index::*; |
1e8da0a7 | 21 | use super::{DataBlob, ArchiveType, archive_type}; |
115d927c | 22 | use crate::config::datastore::{self, DataStoreConfig}; |
f6b1d1cc | 23 | use crate::task::TaskState; |
367f002e | 24 | use crate::tools; |
49a92084 | 25 | use crate::tools::format::HumanByte; |
e4342585 | 26 | use crate::tools::fs::{lock_dir_noblock, DirLockGuard}; |
e6dc35ac | 27 | use crate::api2::types::{Authid, GarbageCollectionStatus}; |
f6b1d1cc | 28 | use crate::server::UPID; |
529de6c7 | 29 | |
367f002e WB |
30 | lazy_static! { |
31 | static ref DATASTORE_MAP: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new()); | |
b3483782 | 32 | } |
ff3d3100 | 33 | |
e5064ba6 DM |
34 | /// Datastore Management |
35 | /// | |
36 | /// A Datastore can store severals backups, and provides the | |
37 | /// management interface for backup. | |
529de6c7 | 38 | pub struct DataStore { |
1629d2ad | 39 | chunk_store: Arc<ChunkStore>, |
64e53b28 | 40 | gc_mutex: Mutex<bool>, |
f2b99c34 | 41 | last_gc_status: Mutex<GarbageCollectionStatus>, |
0698f78d | 42 | verify_new: bool, |
529de6c7 DM |
43 | } |
44 | ||
45 | impl DataStore { | |
46 | ||
2c32fdde DM |
47 | pub fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> { |
48 | ||
d0187a51 DM |
49 | let (config, _digest) = datastore::config()?; |
50 | let config: datastore::DataStoreConfig = config.lookup("datastore", name)?; | |
df729017 | 51 | let path = PathBuf::from(&config.path); |
2c32fdde | 52 | |
515688d1 | 53 | let mut map = DATASTORE_MAP.lock().unwrap(); |
2c32fdde DM |
54 | |
55 | if let Some(datastore) = map.get(name) { | |
56 | // Compare Config - if changed, create new Datastore object! | |
0698f78d SR |
57 | if datastore.chunk_store.base == path && |
58 | datastore.verify_new == config.verify_new.unwrap_or(false) | |
59 | { | |
2c32fdde DM |
60 | return Ok(datastore.clone()); |
61 | } | |
62 | } | |
63 | ||
df729017 | 64 | let datastore = DataStore::open_with_path(name, &path, config)?; |
f0a61124 DM |
65 | |
66 | let datastore = Arc::new(datastore); | |
67 | map.insert(name.to_string(), datastore.clone()); | |
2c32fdde | 68 | |
f0a61124 | 69 | Ok(datastore) |
2c32fdde DM |
70 | } |
71 | ||
0698f78d | 72 | fn open_with_path(store_name: &str, path: &Path, config: DataStoreConfig) -> Result<Self, Error> { |
277fc5a3 | 73 | let chunk_store = ChunkStore::open(store_name, path)?; |
529de6c7 | 74 | |
b683fd58 DC |
75 | let mut gc_status_path = chunk_store.base_path(); |
76 | gc_status_path.push(".gc-status"); | |
77 | ||
78 | let gc_status = if let Some(state) = file_read_optional_string(gc_status_path)? { | |
79 | match serde_json::from_str(&state) { | |
80 | Ok(state) => state, | |
81 | Err(err) => { | |
82 | eprintln!("error reading gc-status: {}", err); | |
83 | GarbageCollectionStatus::default() | |
84 | } | |
85 | } | |
86 | } else { | |
87 | GarbageCollectionStatus::default() | |
88 | }; | |
f2b99c34 | 89 | |
529de6c7 | 90 | Ok(Self { |
1629d2ad | 91 | chunk_store: Arc::new(chunk_store), |
64e53b28 | 92 | gc_mutex: Mutex::new(false), |
f2b99c34 | 93 | last_gc_status: Mutex::new(gc_status), |
0698f78d | 94 | verify_new: config.verify_new.unwrap_or(false), |
529de6c7 DM |
95 | }) |
96 | } | |
97 | ||
d59397e6 WB |
98 | pub fn get_chunk_iterator( |
99 | &self, | |
100 | ) -> Result< | |
a9767cf7 | 101 | impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize, bool)>, |
d59397e6 WB |
102 | Error |
103 | > { | |
a5736098 | 104 | self.chunk_store.get_chunk_iterator() |
d59397e6 WB |
105 | } |
106 | ||
91a905b6 | 107 | pub fn create_fixed_writer<P: AsRef<Path>>(&self, filename: P, size: usize, chunk_size: usize) -> Result<FixedIndexWriter, Error> { |
529de6c7 | 108 | |
91a905b6 | 109 | let index = FixedIndexWriter::create(self.chunk_store.clone(), filename.as_ref(), size, chunk_size)?; |
529de6c7 DM |
110 | |
111 | Ok(index) | |
112 | } | |
113 | ||
91a905b6 | 114 | pub fn open_fixed_reader<P: AsRef<Path>>(&self, filename: P) -> Result<FixedIndexReader, Error> { |
529de6c7 | 115 | |
a7c72ad9 DM |
116 | let full_path = self.chunk_store.relative_path(filename.as_ref()); |
117 | ||
118 | let index = FixedIndexReader::open(&full_path)?; | |
529de6c7 DM |
119 | |
120 | Ok(index) | |
121 | } | |
3d5c11e5 | 122 | |
93d5d779 | 123 | pub fn create_dynamic_writer<P: AsRef<Path>>( |
0433db19 | 124 | &self, filename: P, |
93d5d779 | 125 | ) -> Result<DynamicIndexWriter, Error> { |
0433db19 | 126 | |
93d5d779 | 127 | let index = DynamicIndexWriter::create( |
976595e1 | 128 | self.chunk_store.clone(), filename.as_ref())?; |
0433db19 DM |
129 | |
130 | Ok(index) | |
131 | } | |
ff3d3100 | 132 | |
93d5d779 | 133 | pub fn open_dynamic_reader<P: AsRef<Path>>(&self, filename: P) -> Result<DynamicIndexReader, Error> { |
77703d95 | 134 | |
d48a9955 DM |
135 | let full_path = self.chunk_store.relative_path(filename.as_ref()); |
136 | ||
137 | let index = DynamicIndexReader::open(&full_path)?; | |
77703d95 DM |
138 | |
139 | Ok(index) | |
140 | } | |
141 | ||
5de2bced WB |
142 | pub fn open_index<P>(&self, filename: P) -> Result<Box<dyn IndexFile + Send>, Error> |
143 | where | |
144 | P: AsRef<Path>, | |
145 | { | |
146 | let filename = filename.as_ref(); | |
147 | let out: Box<dyn IndexFile + Send> = | |
1e8da0a7 DM |
148 | match archive_type(filename)? { |
149 | ArchiveType::DynamicIndex => Box::new(self.open_dynamic_reader(filename)?), | |
150 | ArchiveType::FixedIndex => Box::new(self.open_fixed_reader(filename)?), | |
5de2bced WB |
151 | _ => bail!("cannot open index file of unknown type: {:?}", filename), |
152 | }; | |
153 | Ok(out) | |
154 | } | |
155 | ||
60f9a6ea DM |
156 | pub fn name(&self) -> &str { |
157 | self.chunk_store.name() | |
158 | } | |
159 | ||
ff3d3100 DM |
160 | pub fn base_path(&self) -> PathBuf { |
161 | self.chunk_store.base_path() | |
162 | } | |
163 | ||
c47e294e | 164 | /// Cleanup a backup directory |
7759eef5 DM |
165 | /// |
166 | /// Removes all files not mentioned in the manifest. | |
167 | pub fn cleanup_backup_dir(&self, backup_dir: &BackupDir, manifest: &BackupManifest | |
168 | ) -> Result<(), Error> { | |
169 | ||
170 | let mut full_path = self.base_path(); | |
171 | full_path.push(backup_dir.relative_path()); | |
172 | ||
173 | let mut wanted_files = HashSet::new(); | |
174 | wanted_files.insert(MANIFEST_BLOB_NAME.to_string()); | |
1610c45a | 175 | wanted_files.insert(CLIENT_LOG_BLOB_NAME.to_string()); |
7759eef5 DM |
176 | manifest.files().iter().for_each(|item| { wanted_files.insert(item.filename.clone()); }); |
177 | ||
178 | for item in tools::fs::read_subdir(libc::AT_FDCWD, &full_path)? { | |
179 | if let Ok(item) = item { | |
180 | if let Some(file_type) = item.file_type() { | |
181 | if file_type != nix::dir::Type::File { continue; } | |
182 | } | |
183 | let file_name = item.file_name().to_bytes(); | |
184 | if file_name == b"." || file_name == b".." { continue; }; | |
185 | ||
186 | if let Ok(name) = std::str::from_utf8(file_name) { | |
187 | if wanted_files.contains(name) { continue; } | |
188 | } | |
189 | println!("remove unused file {:?}", item.file_name()); | |
190 | let dirfd = item.parent_fd(); | |
191 | let _res = unsafe { libc::unlinkat(dirfd, item.file_name().as_ptr(), 0) }; | |
192 | } | |
193 | } | |
194 | ||
195 | Ok(()) | |
196 | } | |
4b4eba0b | 197 | |
41b373ec DM |
198 | /// Returns the absolute path for a backup_group |
199 | pub fn group_path(&self, backup_group: &BackupGroup) -> PathBuf { | |
4b4eba0b DM |
200 | let mut full_path = self.base_path(); |
201 | full_path.push(backup_group.group_path()); | |
41b373ec DM |
202 | full_path |
203 | } | |
204 | ||
205 | /// Returns the absolute path for backup_dir | |
206 | pub fn snapshot_path(&self, backup_dir: &BackupDir) -> PathBuf { | |
207 | let mut full_path = self.base_path(); | |
208 | full_path.push(backup_dir.relative_path()); | |
209 | full_path | |
210 | } | |
211 | ||
212 | /// Remove a complete backup group including all snapshots | |
6abce6c2 | 213 | pub fn remove_backup_group(&self, backup_group: &BackupGroup) -> Result<(), Error> { |
41b373ec DM |
214 | |
215 | let full_path = self.group_path(backup_group); | |
4b4eba0b | 216 | |
6d6b4e72 | 217 | let _guard = tools::fs::lock_dir_noblock(&full_path, "backup group", "possible running backup")?; |
c9756b40 | 218 | |
4b4eba0b | 219 | log::info!("removing backup group {:?}", full_path); |
4c0ae82e SR |
220 | |
221 | // remove all individual backup dirs first to ensure nothing is using them | |
222 | for snap in backup_group.list_backups(&self.base_path())? { | |
223 | self.remove_backup_dir(&snap.backup_dir, false)?; | |
224 | } | |
225 | ||
226 | // no snapshots left, we can now safely remove the empty folder | |
6abce6c2 | 227 | std::fs::remove_dir_all(&full_path) |
8a1d68c8 DM |
228 | .map_err(|err| { |
229 | format_err!( | |
4c0ae82e | 230 | "removing backup group directory {:?} failed - {}", |
8a1d68c8 DM |
231 | full_path, |
232 | err, | |
233 | ) | |
234 | })?; | |
4b4eba0b DM |
235 | |
236 | Ok(()) | |
237 | } | |
238 | ||
8f579717 | 239 | /// Remove a backup directory including all content |
c9756b40 | 240 | pub fn remove_backup_dir(&self, backup_dir: &BackupDir, force: bool) -> Result<(), Error> { |
8f579717 | 241 | |
41b373ec | 242 | let full_path = self.snapshot_path(backup_dir); |
8f579717 | 243 | |
1a374fcf | 244 | let (_guard, _manifest_guard); |
c9756b40 | 245 | if !force { |
238a872d | 246 | _guard = lock_dir_noblock(&full_path, "snapshot", "possibly running or in use")?; |
6bd0a00c | 247 | _manifest_guard = self.lock_manifest(backup_dir)?; |
c9756b40 SR |
248 | } |
249 | ||
8a1d68c8 | 250 | log::info!("removing backup snapshot {:?}", full_path); |
6abce6c2 | 251 | std::fs::remove_dir_all(&full_path) |
8a1d68c8 DM |
252 | .map_err(|err| { |
253 | format_err!( | |
254 | "removing backup snapshot {:?} failed - {}", | |
255 | full_path, | |
256 | err, | |
257 | ) | |
258 | })?; | |
8f579717 | 259 | |
179145dc DC |
260 | // the manifest does not exists anymore, we do not need to keep the lock |
261 | if let Ok(path) = self.manifest_lock_path(backup_dir) { | |
262 | // ignore errors | |
263 | let _ = std::fs::remove_file(path); | |
264 | } | |
265 | ||
8f579717 DM |
266 | Ok(()) |
267 | } | |
268 | ||
41b373ec DM |
269 | /// Returns the time of the last successful backup |
270 | /// | |
271 | /// Or None if there is no backup in the group (or the group dir does not exist). | |
6a7be83e | 272 | pub fn last_successful_backup(&self, backup_group: &BackupGroup) -> Result<Option<i64>, Error> { |
41b373ec DM |
273 | let base_path = self.base_path(); |
274 | let mut group_path = base_path.clone(); | |
275 | group_path.push(backup_group.group_path()); | |
276 | ||
277 | if group_path.exists() { | |
278 | backup_group.last_successful_backup(&base_path) | |
279 | } else { | |
280 | Ok(None) | |
281 | } | |
282 | } | |
283 | ||
54552dda DM |
284 | /// Returns the backup owner. |
285 | /// | |
e6dc35ac FG |
286 | /// The backup owner is the entity who first created the backup group. |
287 | pub fn get_owner(&self, backup_group: &BackupGroup) -> Result<Authid, Error> { | |
54552dda DM |
288 | let mut full_path = self.base_path(); |
289 | full_path.push(backup_group.group_path()); | |
290 | full_path.push("owner"); | |
291 | let owner = proxmox::tools::fs::file_read_firstline(full_path)?; | |
e7cb4dc5 | 292 | Ok(owner.trim_end().parse()?) // remove trailing newline |
54552dda DM |
293 | } |
294 | ||
295 | /// Set the backup owner. | |
e7cb4dc5 WB |
296 | pub fn set_owner( |
297 | &self, | |
298 | backup_group: &BackupGroup, | |
e6dc35ac | 299 | auth_id: &Authid, |
e7cb4dc5 WB |
300 | force: bool, |
301 | ) -> Result<(), Error> { | |
54552dda DM |
302 | let mut path = self.base_path(); |
303 | path.push(backup_group.group_path()); | |
304 | path.push("owner"); | |
305 | ||
306 | let mut open_options = std::fs::OpenOptions::new(); | |
307 | open_options.write(true); | |
308 | open_options.truncate(true); | |
309 | ||
310 | if force { | |
311 | open_options.create(true); | |
312 | } else { | |
313 | open_options.create_new(true); | |
314 | } | |
315 | ||
316 | let mut file = open_options.open(&path) | |
317 | .map_err(|err| format_err!("unable to create owner file {:?} - {}", path, err))?; | |
318 | ||
e6dc35ac | 319 | writeln!(file, "{}", auth_id) |
54552dda DM |
320 | .map_err(|err| format_err!("unable to write owner file {:?} - {}", path, err))?; |
321 | ||
322 | Ok(()) | |
323 | } | |
324 | ||
1fc82c41 | 325 | /// Create (if it does not already exists) and lock a backup group |
54552dda DM |
326 | /// |
327 | /// And set the owner to 'userid'. If the group already exists, it returns the | |
328 | /// current owner (instead of setting the owner). | |
1fc82c41 | 329 | /// |
1ffe0301 | 330 | /// This also acquires an exclusive lock on the directory and returns the lock guard. |
e7cb4dc5 WB |
331 | pub fn create_locked_backup_group( |
332 | &self, | |
333 | backup_group: &BackupGroup, | |
e6dc35ac FG |
334 | auth_id: &Authid, |
335 | ) -> Result<(Authid, DirLockGuard), Error> { | |
8731e40a | 336 | // create intermediate path first: |
44288184 | 337 | let mut full_path = self.base_path(); |
54552dda | 338 | full_path.push(backup_group.backup_type()); |
8731e40a WB |
339 | std::fs::create_dir_all(&full_path)?; |
340 | ||
54552dda DM |
341 | full_path.push(backup_group.backup_id()); |
342 | ||
343 | // create the last component now | |
344 | match std::fs::create_dir(&full_path) { | |
345 | Ok(_) => { | |
e4342585 | 346 | let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?; |
e6dc35ac | 347 | self.set_owner(backup_group, auth_id, false)?; |
54552dda | 348 | let owner = self.get_owner(backup_group)?; // just to be sure |
1fc82c41 | 349 | Ok((owner, guard)) |
54552dda DM |
350 | } |
351 | Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => { | |
e4342585 | 352 | let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?; |
54552dda | 353 | let owner = self.get_owner(backup_group)?; // just to be sure |
1fc82c41 | 354 | Ok((owner, guard)) |
54552dda DM |
355 | } |
356 | Err(err) => bail!("unable to create backup group {:?} - {}", full_path, err), | |
357 | } | |
358 | } | |
359 | ||
360 | /// Creates a new backup snapshot inside a BackupGroup | |
361 | /// | |
362 | /// The BackupGroup directory needs to exist. | |
f23f7543 SR |
363 | pub fn create_locked_backup_dir(&self, backup_dir: &BackupDir) |
364 | -> Result<(PathBuf, bool, DirLockGuard), Error> | |
365 | { | |
b3483782 DM |
366 | let relative_path = backup_dir.relative_path(); |
367 | let mut full_path = self.base_path(); | |
368 | full_path.push(&relative_path); | |
ff3d3100 | 369 | |
f23f7543 SR |
370 | let lock = || |
371 | lock_dir_noblock(&full_path, "snapshot", "internal error - tried creating snapshot that's already in use"); | |
372 | ||
8731e40a | 373 | match std::fs::create_dir(&full_path) { |
f23f7543 SR |
374 | Ok(_) => Ok((relative_path, true, lock()?)), |
375 | Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok((relative_path, false, lock()?)), | |
376 | Err(e) => Err(e.into()) | |
8731e40a | 377 | } |
ff3d3100 DM |
378 | } |
379 | ||
3d5c11e5 | 380 | pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> { |
ff3d3100 | 381 | let base = self.base_path(); |
3d5c11e5 DM |
382 | |
383 | let mut list = vec![]; | |
384 | ||
95cea65b DM |
385 | use walkdir::WalkDir; |
386 | ||
84466003 | 387 | let walker = WalkDir::new(&base).into_iter(); |
95cea65b DM |
388 | |
389 | // make sure we skip .chunks (and other hidden files to keep it simple) | |
390 | fn is_hidden(entry: &walkdir::DirEntry) -> bool { | |
391 | entry.file_name() | |
392 | .to_str() | |
393 | .map(|s| s.starts_with(".")) | |
394 | .unwrap_or(false) | |
395 | } | |
c3b090ac TL |
396 | let handle_entry_err = |err: walkdir::Error| { |
397 | if let Some(inner) = err.io_error() { | |
398 | let path = err.path().unwrap_or(Path::new("")); | |
399 | match inner.kind() { | |
400 | io::ErrorKind::PermissionDenied => { | |
401 | // only allow to skip ext4 fsck directory, avoid GC if, for example, | |
402 | // a user got file permissions wrong on datastore rsync to new server | |
403 | if err.depth() > 1 || !path.ends_with("lost+found") { | |
404 | bail!("cannot continue garbage-collection safely, permission denied on: {}", path.display()) | |
405 | } | |
406 | }, | |
407 | _ => bail!("unexpected error on datastore traversal: {} - {}", inner, path.display()), | |
408 | } | |
409 | } | |
410 | Ok(()) | |
411 | }; | |
95cea65b | 412 | for entry in walker.filter_entry(|e| !is_hidden(e)) { |
c3b090ac TL |
413 | let path = match entry { |
414 | Ok(entry) => entry.into_path(), | |
415 | Err(err) => { | |
416 | handle_entry_err(err)?; | |
417 | continue | |
418 | }, | |
419 | }; | |
1e8da0a7 DM |
420 | if let Ok(archive_type) = archive_type(&path) { |
421 | if archive_type == ArchiveType::FixedIndex || archive_type == ArchiveType::DynamicIndex { | |
95cea65b | 422 | list.push(path); |
3d5c11e5 DM |
423 | } |
424 | } | |
425 | } | |
426 | ||
427 | Ok(list) | |
428 | } | |
429 | ||
a660978c DM |
430 | // mark chunks used by ``index`` as used |
431 | fn index_mark_used_chunks<I: IndexFile>( | |
432 | &self, | |
433 | index: I, | |
434 | file_name: &Path, // only used for error reporting | |
435 | status: &mut GarbageCollectionStatus, | |
f6b1d1cc | 436 | worker: &dyn TaskState, |
a660978c DM |
437 | ) -> Result<(), Error> { |
438 | ||
439 | status.index_file_count += 1; | |
440 | status.index_data_bytes += index.index_bytes(); | |
441 | ||
442 | for pos in 0..index.index_count() { | |
f6b1d1cc | 443 | worker.check_abort()?; |
a660978c DM |
444 | tools::fail_on_shutdown()?; |
445 | let digest = index.index_digest(pos).unwrap(); | |
446 | if let Err(err) = self.chunk_store.touch_chunk(digest) { | |
f6b1d1cc WB |
447 | crate::task_warn!( |
448 | worker, | |
449 | "warning: unable to access chunk {}, required by {:?} - {}", | |
450 | proxmox::tools::digest_to_hex(digest), | |
451 | file_name, | |
452 | err, | |
453 | ); | |
fd192564 SR |
454 | |
455 | // touch any corresponding .bad files to keep them around, meaning if a chunk is | |
456 | // rewritten correctly they will be removed automatically, as well as if no index | |
457 | // file requires the chunk anymore (won't get to this loop then) | |
458 | for i in 0..=9 { | |
459 | let bad_ext = format!("{}.bad", i); | |
460 | let mut bad_path = PathBuf::new(); | |
461 | bad_path.push(self.chunk_path(digest).0); | |
462 | bad_path.set_extension(bad_ext); | |
463 | self.chunk_store.cond_touch_path(&bad_path, false)?; | |
464 | } | |
a660978c DM |
465 | } |
466 | } | |
467 | Ok(()) | |
468 | } | |
469 | ||
f6b1d1cc WB |
470 | fn mark_used_chunks( |
471 | &self, | |
472 | status: &mut GarbageCollectionStatus, | |
473 | worker: &dyn TaskState, | |
474 | ) -> Result<(), Error> { | |
3d5c11e5 DM |
475 | |
476 | let image_list = self.list_images()?; | |
8317873c DM |
477 | let image_count = image_list.len(); |
478 | ||
479 | let mut done = 0; | |
8317873c DM |
480 | let mut last_percentage: usize = 0; |
481 | ||
cb4b721c FG |
482 | let mut strange_paths_count: u64 = 0; |
483 | ||
788d82d9 | 484 | for img in image_list { |
92da93b2 | 485 | |
f6b1d1cc | 486 | worker.check_abort()?; |
92da93b2 DM |
487 | tools::fail_on_shutdown()?; |
488 | ||
cb4b721c FG |
489 | if let Some(backup_dir_path) = img.parent() { |
490 | let backup_dir_path = backup_dir_path.strip_prefix(self.base_path())?; | |
491 | if let Some(backup_dir_str) = backup_dir_path.to_str() { | |
492 | if BackupDir::from_str(backup_dir_str).is_err() { | |
493 | strange_paths_count += 1; | |
494 | } | |
495 | } | |
496 | } | |
497 | ||
efcac39d | 498 | match std::fs::File::open(&img) { |
e0762002 | 499 | Ok(file) => { |
788d82d9 | 500 | if let Ok(archive_type) = archive_type(&img) { |
e0762002 | 501 | if archive_type == ArchiveType::FixedIndex { |
788d82d9 | 502 | let index = FixedIndexReader::new(file).map_err(|e| { |
efcac39d | 503 | format_err!("can't read index '{}' - {}", img.to_string_lossy(), e) |
2f0b9235 | 504 | })?; |
788d82d9 | 505 | self.index_mark_used_chunks(index, &img, status, worker)?; |
e0762002 | 506 | } else if archive_type == ArchiveType::DynamicIndex { |
788d82d9 | 507 | let index = DynamicIndexReader::new(file).map_err(|e| { |
efcac39d | 508 | format_err!("can't read index '{}' - {}", img.to_string_lossy(), e) |
2f0b9235 | 509 | })?; |
788d82d9 | 510 | self.index_mark_used_chunks(index, &img, status, worker)?; |
e0762002 DM |
511 | } |
512 | } | |
513 | } | |
788d82d9 | 514 | Err(err) if err.kind() == io::ErrorKind::NotFound => (), // ignore vanished files |
efcac39d | 515 | Err(err) => bail!("can't open index {} - {}", img.to_string_lossy(), err), |
77703d95 | 516 | } |
8317873c DM |
517 | done += 1; |
518 | ||
519 | let percentage = done*100/image_count; | |
520 | if percentage > last_percentage { | |
f6b1d1cc WB |
521 | crate::task_log!( |
522 | worker, | |
7956877f | 523 | "marked {}% ({} of {} index files)", |
f6b1d1cc WB |
524 | percentage, |
525 | done, | |
526 | image_count, | |
527 | ); | |
8317873c DM |
528 | last_percentage = percentage; |
529 | } | |
3d5c11e5 DM |
530 | } |
531 | ||
cb4b721c FG |
532 | if strange_paths_count > 0 { |
533 | crate::task_log!( | |
534 | worker, | |
535 | "found (and marked) {} index files outside of expected directory scheme", | |
536 | strange_paths_count, | |
537 | ); | |
538 | } | |
539 | ||
540 | ||
3d5c11e5 | 541 | Ok(()) |
f2b99c34 DM |
542 | } |
543 | ||
544 | pub fn last_gc_status(&self) -> GarbageCollectionStatus { | |
545 | self.last_gc_status.lock().unwrap().clone() | |
546 | } | |
3d5c11e5 | 547 | |
8545480a DM |
548 | pub fn garbage_collection_running(&self) -> bool { |
549 | if let Ok(_) = self.gc_mutex.try_lock() { false } else { true } | |
550 | } | |
551 | ||
f6b1d1cc | 552 | pub fn garbage_collection(&self, worker: &dyn TaskState, upid: &UPID) -> Result<(), Error> { |
3d5c11e5 | 553 | |
a198d74f | 554 | if let Ok(ref mut _mutex) = self.gc_mutex.try_lock() { |
e95950e4 | 555 | |
c6772c92 TL |
556 | // avoids that we run GC if an old daemon process has still a |
557 | // running backup writer, which is not save as we have no "oldest | |
558 | // writer" information and thus no safe atime cutoff | |
43b13033 DM |
559 | let _exclusive_lock = self.chunk_store.try_exclusive_lock()?; |
560 | ||
823867f5 | 561 | let phase1_start_time = proxmox::tools::time::epoch_i64(); |
49a92084 | 562 | let oldest_writer = self.chunk_store.oldest_writer().unwrap_or(phase1_start_time); |
11861a48 | 563 | |
64e53b28 | 564 | let mut gc_status = GarbageCollectionStatus::default(); |
f6b1d1cc WB |
565 | gc_status.upid = Some(upid.to_string()); |
566 | ||
567 | crate::task_log!(worker, "Start GC phase1 (mark used chunks)"); | |
568 | ||
569 | self.mark_used_chunks(&mut gc_status, worker)?; | |
570 | ||
571 | crate::task_log!(worker, "Start GC phase2 (sweep unused chunks)"); | |
572 | self.chunk_store.sweep_unused_chunks( | |
573 | oldest_writer, | |
574 | phase1_start_time, | |
575 | &mut gc_status, | |
576 | worker, | |
577 | )?; | |
578 | ||
579 | crate::task_log!( | |
580 | worker, | |
581 | "Removed garbage: {}", | |
582 | HumanByte::from(gc_status.removed_bytes), | |
583 | ); | |
584 | crate::task_log!(worker, "Removed chunks: {}", gc_status.removed_chunks); | |
cf459b19 | 585 | if gc_status.pending_bytes > 0 { |
f6b1d1cc WB |
586 | crate::task_log!( |
587 | worker, | |
588 | "Pending removals: {} (in {} chunks)", | |
589 | HumanByte::from(gc_status.pending_bytes), | |
590 | gc_status.pending_chunks, | |
591 | ); | |
cf459b19 | 592 | } |
a9767cf7 | 593 | if gc_status.removed_bad > 0 { |
b6563f48 | 594 | crate::task_log!(worker, "Removed bad chunks: {}", gc_status.removed_bad); |
a9767cf7 | 595 | } |
cf459b19 | 596 | |
b4fb2623 | 597 | if gc_status.still_bad > 0 { |
b6563f48 | 598 | crate::task_log!(worker, "Leftover bad chunks: {}", gc_status.still_bad); |
b4fb2623 DM |
599 | } |
600 | ||
f6b1d1cc WB |
601 | crate::task_log!( |
602 | worker, | |
603 | "Original data usage: {}", | |
604 | HumanByte::from(gc_status.index_data_bytes), | |
605 | ); | |
868c5852 DM |
606 | |
607 | if gc_status.index_data_bytes > 0 { | |
49a92084 | 608 | let comp_per = (gc_status.disk_bytes as f64 * 100.)/gc_status.index_data_bytes as f64; |
f6b1d1cc WB |
609 | crate::task_log!( |
610 | worker, | |
611 | "On-Disk usage: {} ({:.2}%)", | |
612 | HumanByte::from(gc_status.disk_bytes), | |
613 | comp_per, | |
614 | ); | |
868c5852 DM |
615 | } |
616 | ||
f6b1d1cc | 617 | crate::task_log!(worker, "On-Disk chunks: {}", gc_status.disk_chunks); |
868c5852 | 618 | |
d6373f35 DM |
619 | let deduplication_factor = if gc_status.disk_bytes > 0 { |
620 | (gc_status.index_data_bytes as f64)/(gc_status.disk_bytes as f64) | |
621 | } else { | |
622 | 1.0 | |
623 | }; | |
624 | ||
625 | crate::task_log!(worker, "Deduplication factor: {:.2}", deduplication_factor); | |
626 | ||
868c5852 DM |
627 | if gc_status.disk_chunks > 0 { |
628 | let avg_chunk = gc_status.disk_bytes/(gc_status.disk_chunks as u64); | |
f6b1d1cc | 629 | crate::task_log!(worker, "Average chunk size: {}", HumanByte::from(avg_chunk)); |
868c5852 | 630 | } |
64e53b28 | 631 | |
b683fd58 DC |
632 | if let Ok(serialized) = serde_json::to_string(&gc_status) { |
633 | let mut path = self.base_path(); | |
634 | path.push(".gc-status"); | |
635 | ||
636 | let backup_user = crate::backup::backup_user()?; | |
637 | let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644); | |
638 | // set the correct owner/group/permissions while saving file | |
639 | // owner(rw) = backup, group(r)= backup | |
640 | let options = CreateOptions::new() | |
641 | .perm(mode) | |
642 | .owner(backup_user.uid) | |
643 | .group(backup_user.gid); | |
644 | ||
645 | // ignore errors | |
646 | let _ = replace_file(path, serialized.as_bytes(), options); | |
647 | } | |
648 | ||
f2b99c34 DM |
649 | *self.last_gc_status.lock().unwrap() = gc_status; |
650 | ||
64e53b28 | 651 | } else { |
d4b59ae0 | 652 | bail!("Start GC failed - (already running/locked)"); |
64e53b28 | 653 | } |
3d5c11e5 DM |
654 | |
655 | Ok(()) | |
656 | } | |
3b7ade9e | 657 | |
1cf5178a DM |
658 | pub fn try_shared_chunk_store_lock(&self) -> Result<tools::ProcessLockSharedGuard, Error> { |
659 | self.chunk_store.try_shared_lock() | |
660 | } | |
661 | ||
d48a9955 DM |
662 | pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) { |
663 | self.chunk_store.chunk_path(digest) | |
664 | } | |
665 | ||
2585a8a4 DM |
666 | pub fn cond_touch_chunk(&self, digest: &[u8; 32], fail_if_not_exist: bool) -> Result<bool, Error> { |
667 | self.chunk_store.cond_touch_chunk(digest, fail_if_not_exist) | |
668 | } | |
669 | ||
f98ac774 | 670 | pub fn insert_chunk( |
3b7ade9e | 671 | &self, |
4ee8f53d DM |
672 | chunk: &DataBlob, |
673 | digest: &[u8; 32], | |
3b7ade9e | 674 | ) -> Result<(bool, u64), Error> { |
4ee8f53d | 675 | self.chunk_store.insert_chunk(chunk, digest) |
3b7ade9e | 676 | } |
60f9a6ea | 677 | |
39f18b30 | 678 | pub fn load_blob(&self, backup_dir: &BackupDir, filename: &str) -> Result<DataBlob, Error> { |
60f9a6ea DM |
679 | let mut path = self.base_path(); |
680 | path.push(backup_dir.relative_path()); | |
681 | path.push(filename); | |
682 | ||
39f18b30 DM |
683 | proxmox::try_block!({ |
684 | let mut file = std::fs::File::open(&path)?; | |
685 | DataBlob::load_from_reader(&mut file) | |
686 | }).map_err(|err| format_err!("unable to load blob '{:?}' - {}", path, err)) | |
687 | } | |
e4439025 DM |
688 | |
689 | ||
39f18b30 DM |
690 | pub fn load_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> { |
691 | ||
692 | let (chunk_path, digest_str) = self.chunk_store.chunk_path(digest); | |
693 | ||
694 | proxmox::try_block!({ | |
695 | let mut file = std::fs::File::open(&chunk_path)?; | |
696 | DataBlob::load_from_reader(&mut file) | |
697 | }).map_err(|err| format_err!( | |
698 | "store '{}', unable to load chunk '{}' - {}", | |
699 | self.name(), | |
700 | digest_str, | |
701 | err, | |
702 | )) | |
1a374fcf SR |
703 | } |
704 | ||
179145dc DC |
705 | /// Returns the filename to lock a manifest |
706 | /// | |
707 | /// Also creates the basedir. The lockfile is located in | |
708 | /// '/run/proxmox-backup/locks/{datastore}/{type}/{id}/{timestamp}.index.json.lck' | |
709 | fn manifest_lock_path( | |
710 | &self, | |
711 | backup_dir: &BackupDir, | |
712 | ) -> Result<String, Error> { | |
713 | let mut path = format!( | |
714 | "/run/proxmox-backup/locks/{}/{}/{}", | |
715 | self.name(), | |
716 | backup_dir.group().backup_type(), | |
717 | backup_dir.group().backup_id(), | |
718 | ); | |
719 | std::fs::create_dir_all(&path)?; | |
720 | use std::fmt::Write; | |
721 | write!(path, "/{}{}", backup_dir.backup_time_string(), &MANIFEST_LOCK_NAME)?; | |
722 | ||
723 | Ok(path) | |
724 | } | |
725 | ||
1a374fcf SR |
726 | fn lock_manifest( |
727 | &self, | |
728 | backup_dir: &BackupDir, | |
729 | ) -> Result<File, Error> { | |
179145dc | 730 | let path = self.manifest_lock_path(backup_dir)?; |
1a374fcf SR |
731 | |
732 | // update_manifest should never take a long time, so if someone else has | |
733 | // the lock we can simply block a bit and should get it soon | |
734 | open_file_locked(&path, Duration::from_secs(5), true) | |
735 | .map_err(|err| { | |
736 | format_err!( | |
737 | "unable to acquire manifest lock {:?} - {}", &path, err | |
738 | ) | |
739 | }) | |
740 | } | |
e4439025 | 741 | |
1a374fcf | 742 | /// Load the manifest without a lock. Must not be written back. |
521a0acb WB |
743 | pub fn load_manifest( |
744 | &self, | |
745 | backup_dir: &BackupDir, | |
ff86ef00 | 746 | ) -> Result<(BackupManifest, u64), Error> { |
39f18b30 DM |
747 | let blob = self.load_blob(backup_dir, MANIFEST_BLOB_NAME)?; |
748 | let raw_size = blob.raw_size(); | |
60f9a6ea | 749 | let manifest = BackupManifest::try_from(blob)?; |
ff86ef00 | 750 | Ok((manifest, raw_size)) |
60f9a6ea | 751 | } |
e4439025 | 752 | |
1a374fcf SR |
753 | /// Update the manifest of the specified snapshot. Never write a manifest directly, |
754 | /// only use this method - anything else may break locking guarantees. | |
755 | pub fn update_manifest( | |
e4439025 DM |
756 | &self, |
757 | backup_dir: &BackupDir, | |
1a374fcf | 758 | update_fn: impl FnOnce(&mut BackupManifest), |
e4439025 | 759 | ) -> Result<(), Error> { |
1a374fcf SR |
760 | |
761 | let _guard = self.lock_manifest(backup_dir)?; | |
762 | let (mut manifest, _) = self.load_manifest(&backup_dir)?; | |
763 | ||
764 | update_fn(&mut manifest); | |
765 | ||
883aa6d5 | 766 | let manifest = serde_json::to_value(manifest)?; |
e4439025 DM |
767 | let manifest = serde_json::to_string_pretty(&manifest)?; |
768 | let blob = DataBlob::encode(manifest.as_bytes(), None, true)?; | |
769 | let raw_data = blob.raw_data(); | |
770 | ||
771 | let mut path = self.base_path(); | |
772 | path.push(backup_dir.relative_path()); | |
773 | path.push(MANIFEST_BLOB_NAME); | |
774 | ||
1a374fcf | 775 | // atomic replace invalidates flock - no other writes past this point! |
e4439025 DM |
776 | replace_file(&path, raw_data, CreateOptions::new())?; |
777 | ||
778 | Ok(()) | |
779 | } | |
0698f78d SR |
780 | |
781 | pub fn verify_new(&self) -> bool { | |
782 | self.verify_new | |
783 | } | |
529de6c7 | 784 | } |
fc8920e3 | 785 |