use proxmox::tools::fs::{replace_file, file_read_optional_string, CreateOptions, open_file_locked};
+use pbs_tools::format::HumanByte;
+use pbs_tools::fs::{lock_dir_noblock, DirLockGuard};
+
use super::backup_info::{BackupGroup, BackupDir};
use super::chunk_store::ChunkStore;
use super::dynamic_index::{DynamicIndexReader, DynamicIndexWriter};
use crate::config::datastore::{self, DataStoreConfig};
use crate::task::TaskState;
use crate::tools;
-use crate::tools::format::HumanByte;
-use crate::tools::fs::{lock_dir_noblock, DirLockGuard};
use crate::api2::types::{Authid, GarbageCollectionStatus};
use crate::server::UPID;
/// management interface for backup.
pub struct DataStore {
chunk_store: Arc<ChunkStore>,
- gc_mutex: Mutex<bool>,
+ gc_mutex: Mutex<()>,
last_gc_status: Mutex<GarbageCollectionStatus>,
verify_new: bool,
}
Ok(datastore)
}
+ /// removes all datastores that are not configured anymore
+ pub fn remove_unused_datastores() -> Result<(), Error>{
+ let (config, _digest) = datastore::config()?;
+
+ let mut map = DATASTORE_MAP.lock().unwrap();
+ // removes all elements that are not in the config
+ map.retain(|key, _| {
+ config.sections.contains_key(key)
+ });
+ Ok(())
+ }
+
fn open_with_path(store_name: &str, path: &Path, config: DataStoreConfig) -> Result<Self, Error> {
let chunk_store = ChunkStore::open(store_name, path)?;
Ok(Self {
chunk_store: Arc::new(chunk_store),
- gc_mutex: Mutex::new(false),
+ gc_mutex: Mutex::new(()),
last_gc_status: Mutex::new(gc_status),
verify_new: config.verify_new.unwrap_or(false),
})
pub fn get_chunk_iterator(
&self,
) -> Result<
- impl Iterator<Item = (Result<tools::fs::ReadDirEntry, Error>, usize, bool)>,
+ impl Iterator<Item = (Result<pbs_tools::fs::ReadDirEntry, Error>, usize, bool)>,
Error
> {
self.chunk_store.get_chunk_iterator()
Ok(out)
}
+ /// Fast index verification - only check if chunks exists
+ pub fn fast_index_verification(
+ &self,
+ index: &dyn IndexFile,
+ checked: &mut HashSet<[u8;32]>,
+ ) -> Result<(), Error> {
+
+ for pos in 0..index.index_count() {
+ let info = index.chunk_info(pos).unwrap();
+ if checked.contains(&info.digest) {
+ continue;
+ }
+
+ self.stat_chunk(&info.digest).
+ map_err(|err| {
+ format_err!(
+ "fast_index_verification error, stat_chunk {} failed - {}",
+ proxmox::tools::digest_to_hex(&info.digest),
+ err,
+ )
+ })?;
+
+ checked.insert(info.digest);
+ }
+
+ Ok(())
+ }
+
pub fn name(&self) -> &str {
self.chunk_store.name()
}
wanted_files.insert(CLIENT_LOG_BLOB_NAME.to_string());
manifest.files().iter().for_each(|item| { wanted_files.insert(item.filename.clone()); });
- for item in tools::fs::read_subdir(libc::AT_FDCWD, &full_path)? {
+ for item in pbs_tools::fs::read_subdir(libc::AT_FDCWD, &full_path)? {
if let Ok(item) = item {
if let Some(file_type) = item.file_type() {
if file_type != nix::dir::Type::File { continue; }
let full_path = self.group_path(backup_group);
- let _guard = tools::fs::lock_dir_noblock(&full_path, "backup group", "possible running backup")?;
+ let _guard = pbs_tools::fs::lock_dir_noblock(&full_path, "backup group", "possible running backup")?;
log::info!("removing backup group {:?}", full_path);
}
let handle_entry_err = |err: walkdir::Error| {
if let Some(inner) = err.io_error() {
- let path = err.path().unwrap_or(Path::new(""));
- match inner.kind() {
- io::ErrorKind::PermissionDenied => {
+ if let Some(path) = err.path() {
+ if inner.kind() == io::ErrorKind::PermissionDenied {
// only allow to skip ext4 fsck directory, avoid GC if, for example,
// a user got file permissions wrong on datastore rsync to new server
if err.depth() > 1 || !path.ends_with("lost+found") {
- bail!("cannot continue garbage-collection safely, permission denied on: {}", path.display())
+ bail!("cannot continue garbage-collection safely, permission denied on: {:?}", path)
}
- },
- _ => bail!("unexpected error on datastore traversal: {} - {}", inner, path.display()),
+ } else {
+ bail!("unexpected error on datastore traversal: {} - {:?}", inner, path)
+ }
+ } else {
+ bail!("unexpected error on datastore traversal: {}", inner)
}
}
Ok(())
worker.check_abort()?;
tools::fail_on_shutdown()?;
let digest = index.index_digest(pos).unwrap();
- if let Err(err) = self.chunk_store.touch_chunk(digest) {
+ if !self.chunk_store.cond_touch_chunk(digest, false)? {
crate::task_warn!(
worker,
- "warning: unable to access chunk {}, required by {:?} - {}",
+ "warning: unable to access non-existent chunk {}, required by {:?}",
proxmox::tools::digest_to_hex(digest),
file_name,
- err,
);
// touch any corresponding .bad files to keep them around, meaning if a chunk is
let image_list = self.list_images()?;
let image_count = image_list.len();
- let mut done = 0;
let mut last_percentage: usize = 0;
let mut strange_paths_count: u64 = 0;
- for img in image_list {
+ for (i, img) in image_list.into_iter().enumerate() {
worker.check_abort()?;
tools::fail_on_shutdown()?;
Err(err) if err.kind() == io::ErrorKind::NotFound => (), // ignore vanished files
Err(err) => bail!("can't open index {} - {}", img.to_string_lossy(), err),
}
- done += 1;
- let percentage = done*100/image_count;
+ let percentage = (i + 1) * 100 / image_count;
if percentage > last_percentage {
crate::task_log!(
worker,
"marked {}% ({} of {} index files)",
percentage,
- done,
+ i + 1,
image_count,
);
last_percentage = percentage;
}
pub fn garbage_collection_running(&self) -> bool {
- if let Ok(_) = self.gc_mutex.try_lock() { false } else { true }
+ !matches!(self.gc_mutex.try_lock(), Ok(_))
}
pub fn garbage_collection(&self, worker: &dyn TaskState, upid: &UPID) -> Result<(), Error> {
}
+ pub fn stat_chunk(&self, digest: &[u8; 32]) -> Result<std::fs::Metadata, Error> {
+ let (chunk_path, _digest_str) = self.chunk_store.chunk_path(digest);
+ std::fs::metadata(chunk_path).map_err(Error::from)
+ }
+
pub fn load_chunk(&self, digest: &[u8; 32]) -> Result<DataBlob, Error> {
let (chunk_path, digest_str) = self.chunk_store.chunk_path(digest);
pub fn verify_new(&self) -> bool {
self.verify_new
}
-}
+ /// returns a list of chunks sorted by their inode number on disk
+ /// chunks that could not be stat'ed are at the end of the list
+ pub fn get_chunks_in_order<F, A>(
+ &self,
+ index: &Box<dyn IndexFile + Send>,
+ skip_chunk: F,
+ check_abort: A,
+ ) -> Result<Vec<(usize, u64)>, Error>
+ where
+ F: Fn(&[u8; 32]) -> bool,
+ A: Fn(usize) -> Result<(), Error>,
+ {
+ let index_count = index.index_count();
+ let mut chunk_list = Vec::with_capacity(index_count);
+ use std::os::unix::fs::MetadataExt;
+ for pos in 0..index_count {
+ check_abort(pos)?;
+
+ let info = index.chunk_info(pos).unwrap();
+
+ if skip_chunk(&info.digest) {
+ continue;
+ }
+
+ let ino = match self.stat_chunk(&info.digest) {
+ Err(_) => u64::MAX, // could not stat, move to end of list
+ Ok(metadata) => metadata.ino(),
+ };
+
+ chunk_list.push((pos, ino));
+ }
+
+ // sorting by inode improves data locality, which makes it lots faster on spinners
+ chunk_list.sort_unstable_by(|(_, ino_a), (_, ino_b)| ino_a.cmp(&ino_b));
+
+ Ok(chunk_list)
+ }
+}