2019-06-13 09:47:23 +00:00
|
|
|
use std::io::Write;
|
2019-03-22 08:42:15 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2021-07-07 12:37:47 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
use anyhow::{bail, format_err, Error};
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2019-12-18 11:25:56 +00:00
|
|
|
use proxmox::tools::fs::{CreateOptions, create_path, create_dir};
|
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
use pbs_api_types::GarbageCollectionStatus;
|
|
|
|
use pbs_tools::process_locker::{self, ProcessLocker};
|
2021-09-24 05:40:49 +00:00
|
|
|
use pbs_tools::{task_log, task::WorkerTaskContext};
|
2020-01-23 12:31:52 +00:00
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
use crate::DataBlob;
|
2018-12-19 09:02:24 +00:00
|
|
|
|
2019-02-12 13:13:31 +00:00
|
|
|
/// File system based chunk store
|
2018-12-07 09:51:53 +00:00
|
|
|
pub struct ChunkStore {
|
2018-12-19 12:40:26 +00:00
|
|
|
name: String, // used for error reporting
|
2018-12-22 16:37:25 +00:00
|
|
|
pub (crate) base: PathBuf,
|
2018-12-07 09:51:53 +00:00
|
|
|
chunk_dir: PathBuf,
|
2021-01-20 16:23:49 +00:00
|
|
|
mutex: Mutex<()>,
|
2021-07-07 12:37:47 +00:00
|
|
|
locker: Arc<Mutex<ProcessLocker>>,
|
2018-12-07 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-22 14:39:05 +00:00
|
|
|
// TODO: what about sysctl setting vm.vfs_cache_pressure (0 - 100) ?
|
|
|
|
|
2019-05-30 11:28:24 +00:00
|
|
|
pub fn verify_chunk_size(size: usize) -> Result<(), Error> {
|
2019-02-19 14:19:12 +00:00
|
|
|
|
2019-05-30 11:28:24 +00:00
|
|
|
static SIZES: [usize; 7] = [64*1024, 128*1024, 256*1024, 512*1024, 1024*1024, 2048*1024, 4096*1024];
|
2019-02-19 14:19:12 +00:00
|
|
|
|
|
|
|
if !SIZES.contains(&size) {
|
|
|
|
bail!("Got unsupported chunk size '{}'", size);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-19 08:51:33 +00:00
|
|
|
fn digest_to_prefix(digest: &[u8]) -> PathBuf {
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-22 13:04:05 +00:00
|
|
|
let mut buf = Vec::<u8>::with_capacity(2+1+2+1);
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
const HEX_CHARS: &[u8; 16] = b"0123456789abcdef";
|
2019-01-25 09:58:28 +00:00
|
|
|
|
2018-12-07 13:44:56 +00:00
|
|
|
buf.push(HEX_CHARS[(digest[0] as usize) >> 4]);
|
|
|
|
buf.push(HEX_CHARS[(digest[0] as usize) &0xf]);
|
2018-12-22 13:04:05 +00:00
|
|
|
buf.push(HEX_CHARS[(digest[1] as usize) >> 4]);
|
2018-12-07 13:44:56 +00:00
|
|
|
buf.push(HEX_CHARS[(digest[1] as usize) & 0xf]);
|
2021-01-19 09:38:00 +00:00
|
|
|
buf.push(b'/');
|
2018-12-07 13:44:56 +00:00
|
|
|
|
|
|
|
let path = unsafe { String::from_utf8_unchecked(buf)};
|
|
|
|
|
|
|
|
path.into()
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ChunkStore {
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
fn chunk_dir<P: AsRef<Path>>(path: P) -> PathBuf {
|
|
|
|
|
|
|
|
let mut chunk_dir: PathBuf = PathBuf::from(path.as_ref());
|
|
|
|
chunk_dir.push(".chunks");
|
|
|
|
|
|
|
|
chunk_dir
|
|
|
|
}
|
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
pub fn base(&self) -> &Path {
|
|
|
|
&self.base
|
|
|
|
}
|
|
|
|
|
2021-09-24 05:40:49 +00:00
|
|
|
pub fn create<P>(name: &str, path: P, uid: nix::unistd::Uid, gid: nix::unistd::Gid, worker: Option<&dyn WorkerTaskContext>) -> Result<Self, Error>
|
2019-12-19 12:14:49 +00:00
|
|
|
where
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
{
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
let base: PathBuf = path.into();
|
2019-01-20 15:49:22 +00:00
|
|
|
|
|
|
|
if !base.is_absolute() {
|
|
|
|
bail!("expected absolute path - got {:?}", base);
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
let chunk_dir = Self::chunk_dir(&base);
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2019-12-20 08:11:40 +00:00
|
|
|
let options = CreateOptions::new()
|
2019-12-20 08:23:58 +00:00
|
|
|
.owner(uid)
|
|
|
|
.group(gid);
|
2019-12-20 08:11:40 +00:00
|
|
|
|
2019-12-18 11:25:56 +00:00
|
|
|
let default_options = CreateOptions::new();
|
|
|
|
|
2021-01-15 13:38:27 +00:00
|
|
|
match create_path(&base, Some(default_options), Some(options.clone())) {
|
2020-07-09 16:15:28 +00:00
|
|
|
Err(err) => bail!("unable to create chunk store '{}' at {:?} - {}", name, base, err),
|
|
|
|
Ok(res) => if ! res { nix::unistd::chown(&base, Some(uid), Some(gid))? },
|
2018-12-08 07:38:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 11:25:56 +00:00
|
|
|
if let Err(err) = create_dir(&chunk_dir, options.clone()) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to create chunk store '{}' subdir {:?} - {}", name, chunk_dir, err);
|
2018-12-08 07:38:42 +00:00
|
|
|
}
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2019-12-19 05:49:03 +00:00
|
|
|
// create lock file with correct owner/group
|
|
|
|
let lockfile_path = Self::lockfile_path(&base);
|
2021-10-20 12:56:15 +00:00
|
|
|
proxmox::tools::fs::replace_file(lockfile_path, b"", options.clone(), false)?;
|
2019-12-19 05:49:03 +00:00
|
|
|
|
2019-01-04 09:35:22 +00:00
|
|
|
// create 64*1024 subdirs
|
2018-12-22 13:04:05 +00:00
|
|
|
let mut last_percentage = 0;
|
|
|
|
|
2019-01-04 09:35:22 +00:00
|
|
|
for i in 0..64*1024 {
|
2018-12-16 12:52:16 +00:00
|
|
|
let mut l1path = chunk_dir.clone();
|
2019-01-04 09:35:22 +00:00
|
|
|
l1path.push(format!("{:04x}", i));
|
2019-12-18 11:25:56 +00:00
|
|
|
if let Err(err) = create_dir(&l1path, options.clone()) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to create chunk store '{}' subdir {:?} - {}", name, l1path, err);
|
2018-12-08 07:38:42 +00:00
|
|
|
}
|
2019-01-04 09:35:22 +00:00
|
|
|
let percentage = (i*100)/(64*1024);
|
|
|
|
if percentage != last_percentage {
|
2021-06-02 11:27:03 +00:00
|
|
|
if let Some(worker) = worker {
|
|
|
|
task_log!(worker, "Chunkstore create: {}%", percentage)
|
|
|
|
}
|
2019-01-04 09:35:22 +00:00
|
|
|
last_percentage = percentage;
|
2018-12-22 13:04:05 +00:00
|
|
|
}
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
Self::open(name, base)
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 05:49:03 +00:00
|
|
|
fn lockfile_path<P: Into<PathBuf>>(base: P) -> PathBuf {
|
2021-01-15 13:38:27 +00:00
|
|
|
let mut lockfile_path: PathBuf = base.into();
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2019-12-19 05:49:03 +00:00
|
|
|
lockfile_path.push(".lock");
|
|
|
|
|
|
|
|
lockfile_path
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open<P: Into<PathBuf>>(name: &str, base: P) -> Result<Self, Error> {
|
|
|
|
|
|
|
|
let base: PathBuf = base.into();
|
2019-01-20 15:49:22 +00:00
|
|
|
|
|
|
|
if !base.is_absolute() {
|
|
|
|
bail!("expected absolute path - got {:?}", base);
|
|
|
|
}
|
|
|
|
|
2018-12-07 17:14:07 +00:00
|
|
|
let chunk_dir = Self::chunk_dir(&base);
|
|
|
|
|
2018-12-09 08:42:17 +00:00
|
|
|
if let Err(err) = std::fs::metadata(&chunk_dir) {
|
2018-12-19 12:40:26 +00:00
|
|
|
bail!("unable to open chunk store '{}' at {:?} - {}", name, chunk_dir, err);
|
2018-12-09 08:42:17 +00:00
|
|
|
}
|
2018-12-07 17:14:07 +00:00
|
|
|
|
2019-12-19 05:49:03 +00:00
|
|
|
let lockfile_path = Self::lockfile_path(&base);
|
2018-12-07 17:14:07 +00:00
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
let locker = ProcessLocker::new(&lockfile_path)?;
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2018-12-08 07:21:20 +00:00
|
|
|
Ok(ChunkStore {
|
2018-12-19 12:40:26 +00:00
|
|
|
name: name.to_owned(),
|
2018-12-08 07:21:20 +00:00
|
|
|
base,
|
|
|
|
chunk_dir,
|
2019-03-22 08:42:15 +00:00
|
|
|
locker,
|
2021-01-20 16:23:49 +00:00
|
|
|
mutex: Mutex::new(())
|
2018-12-08 07:21:20 +00:00
|
|
|
})
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 05:57:43 +00:00
|
|
|
pub fn touch_chunk(&self, digest: &[u8; 32]) -> Result<(), Error> {
|
2020-01-02 12:26:28 +00:00
|
|
|
self.cond_touch_chunk(digest, true)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cond_touch_chunk(&self, digest: &[u8; 32], fail_if_not_exist: bool) -> Result<bool, Error> {
|
2019-07-04 05:57:43 +00:00
|
|
|
let (chunk_path, _digest_str) = self.chunk_path(digest);
|
2020-11-12 14:50:08 +00:00
|
|
|
self.cond_touch_path(&chunk_path, fail_if_not_exist)
|
|
|
|
}
|
2018-12-18 10:06:03 +00:00
|
|
|
|
2020-11-12 14:50:08 +00:00
|
|
|
pub fn cond_touch_path(&self, path: &Path, fail_if_not_exist: bool) -> Result<bool, Error> {
|
2020-05-18 09:35:56 +00:00
|
|
|
const UTIME_NOW: i64 = (1 << 30) - 1;
|
|
|
|
const UTIME_OMIT: i64 = (1 << 30) - 2;
|
2018-12-25 10:59:02 +00:00
|
|
|
|
2018-12-25 12:29:27 +00:00
|
|
|
let times: [libc::timespec; 2] = [
|
2018-12-25 10:59:02 +00:00
|
|
|
libc::timespec { tv_sec: 0, tv_nsec: UTIME_NOW },
|
|
|
|
libc::timespec { tv_sec: 0, tv_nsec: UTIME_OMIT }
|
|
|
|
];
|
|
|
|
|
|
|
|
use nix::NixPath;
|
|
|
|
|
2020-11-12 14:50:08 +00:00
|
|
|
let res = path.with_nix_path(|cstr| unsafe {
|
2020-01-02 12:26:28 +00:00
|
|
|
let tmp = libc::utimensat(-1, cstr.as_ptr(), ×[0], libc::AT_SYMLINK_NOFOLLOW);
|
|
|
|
nix::errno::Errno::result(tmp)
|
2018-12-25 10:59:02 +00:00
|
|
|
})?;
|
|
|
|
|
2020-01-02 12:26:28 +00:00
|
|
|
if let Err(err) = res {
|
|
|
|
if !fail_if_not_exist && err.as_errno() == Some(nix::errno::Errno::ENOENT) {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:50:08 +00:00
|
|
|
bail!("update atime failed for chunk/file {:?} - {}", path, err);
|
2018-12-25 10:59:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 12:26:28 +00:00
|
|
|
Ok(true)
|
2018-12-18 10:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 10:16:34 +00:00
|
|
|
pub fn get_chunk_iterator(
|
|
|
|
&self,
|
|
|
|
) -> Result<
|
2021-07-06 11:26:35 +00:00
|
|
|
impl Iterator<Item = (Result<pbs_tools::fs::ReadDirEntry, Error>, usize, bool)> + std::iter::FusedIterator,
|
2019-02-14 10:16:34 +00:00
|
|
|
Error
|
|
|
|
> {
|
|
|
|
use nix::dir::Dir;
|
|
|
|
use nix::fcntl::OFlag;
|
|
|
|
use nix::sys::stat::Mode;
|
|
|
|
|
2019-07-04 09:39:10 +00:00
|
|
|
let base_handle = Dir::open(&self.chunk_dir, OFlag::O_RDONLY, Mode::empty())
|
|
|
|
.map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"unable to open store '{}' chunk dir {:?} - {}",
|
|
|
|
self.name,
|
|
|
|
self.chunk_dir,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
})?;
|
2019-02-14 10:16:34 +00:00
|
|
|
|
2019-07-04 08:51:52 +00:00
|
|
|
let mut done = false;
|
2021-07-06 11:26:35 +00:00
|
|
|
let mut inner: Option<pbs_tools::fs::ReadDir> = None;
|
2019-07-04 08:51:52 +00:00
|
|
|
let mut at = 0;
|
|
|
|
let mut percentage = 0;
|
|
|
|
Ok(std::iter::from_fn(move || {
|
|
|
|
if done {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
if let Some(ref mut inner) = inner {
|
|
|
|
match inner.next() {
|
|
|
|
Some(Ok(entry)) => {
|
|
|
|
// skip files if they're not a hash
|
|
|
|
let bytes = entry.file_name().to_bytes();
|
2020-09-07 15:30:34 +00:00
|
|
|
if bytes.len() != 64 && bytes.len() != 64 + ".0.bad".len() {
|
2019-07-04 08:51:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-07 15:30:34 +00:00
|
|
|
if !bytes.iter().take(64).all(u8::is_ascii_hexdigit) {
|
2019-07-04 08:51:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-07 15:30:34 +00:00
|
|
|
|
2021-01-19 09:38:00 +00:00
|
|
|
let bad = bytes.ends_with(b".bad");
|
2020-09-07 15:30:34 +00:00
|
|
|
return Some((Ok(entry), percentage, bad));
|
2019-07-04 08:51:52 +00:00
|
|
|
}
|
|
|
|
Some(Err(err)) => {
|
|
|
|
// stop after first error
|
|
|
|
done = true;
|
|
|
|
// and pass the error through:
|
2020-09-07 15:30:34 +00:00
|
|
|
return Some((Err(err), percentage, false));
|
2019-07-04 08:51:52 +00:00
|
|
|
}
|
|
|
|
None => (), // open next directory
|
2019-02-13 14:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-04 08:51:52 +00:00
|
|
|
|
|
|
|
inner = None;
|
|
|
|
|
|
|
|
if at == 0x10000 {
|
|
|
|
done = true;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let subdir: &str = &format!("{:04x}", at);
|
|
|
|
percentage = (at * 100) / 0x10000;
|
|
|
|
at += 1;
|
2021-07-06 11:26:35 +00:00
|
|
|
match pbs_tools::fs::read_subdir(base_handle.as_raw_fd(), subdir) {
|
2019-07-04 08:51:52 +00:00
|
|
|
Ok(dir) => {
|
|
|
|
inner = Some(dir);
|
|
|
|
// start reading:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(ref err) if err.as_errno() == Some(nix::errno::Errno::ENOENT) => {
|
|
|
|
// non-existing directories are okay, just keep going:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// other errors are fatal, so end our iteration
|
|
|
|
done = true;
|
|
|
|
// and pass the error through:
|
2020-09-07 15:30:34 +00:00
|
|
|
return Some((Err(format_err!("unable to read subdir '{}' - {}", subdir, err)), percentage, false));
|
2019-07-04 08:51:52 +00:00
|
|
|
}
|
2019-02-14 10:38:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-04 08:51:52 +00:00
|
|
|
}).fuse())
|
2019-02-13 14:51:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 15:21:36 +00:00
|
|
|
pub fn oldest_writer(&self) -> Option<i64> {
|
2021-07-07 12:37:47 +00:00
|
|
|
ProcessLocker::oldest_shared_lock(self.locker.clone())
|
2019-03-31 15:21:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 09:56:53 +00:00
|
|
|
pub fn sweep_unused_chunks(
|
2019-03-31 15:21:36 +00:00
|
|
|
&self,
|
2020-04-06 07:50:40 +00:00
|
|
|
oldest_writer: i64,
|
2020-07-21 15:29:40 +00:00
|
|
|
phase1_start_time: i64,
|
2019-07-04 07:26:44 +00:00
|
|
|
status: &mut GarbageCollectionStatus,
|
2021-09-24 05:40:49 +00:00
|
|
|
worker: &dyn WorkerTaskContext,
|
2019-03-31 15:21:36 +00:00
|
|
|
) -> Result<(), Error> {
|
2019-02-12 09:37:43 +00:00
|
|
|
use nix::sys::stat::fstatat;
|
2020-09-07 15:30:34 +00:00
|
|
|
use nix::unistd::{unlinkat, UnlinkatFlags};
|
2018-12-19 08:51:33 +00:00
|
|
|
|
2020-07-21 15:29:40 +00:00
|
|
|
let mut min_atime = phase1_start_time - 3600*24; // at least 24h (see mount option relatime)
|
2019-03-31 15:21:36 +00:00
|
|
|
|
2020-04-06 07:50:40 +00:00
|
|
|
if oldest_writer < min_atime {
|
|
|
|
min_atime = oldest_writer;
|
2019-03-31 15:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
min_atime -= 300; // add 5 mins gap for safety
|
|
|
|
|
2019-07-04 07:26:44 +00:00
|
|
|
let mut last_percentage = 0;
|
2019-07-04 09:27:11 +00:00
|
|
|
let mut chunk_count = 0;
|
|
|
|
|
2020-09-07 15:30:34 +00:00
|
|
|
for (entry, percentage, bad) in self.get_chunk_iterator()? {
|
2019-07-04 07:26:44 +00:00
|
|
|
if last_percentage != percentage {
|
|
|
|
last_percentage = percentage;
|
2021-09-23 08:09:19 +00:00
|
|
|
task_log!(
|
2020-10-12 09:46:34 +00:00
|
|
|
worker,
|
2020-11-30 15:22:17 +00:00
|
|
|
"processed {}% ({} chunks)",
|
2020-10-12 09:46:34 +00:00
|
|
|
percentage,
|
|
|
|
chunk_count,
|
|
|
|
);
|
2019-07-04 07:26:44 +00:00
|
|
|
}
|
2019-04-01 10:13:02 +00:00
|
|
|
|
2020-10-12 09:46:34 +00:00
|
|
|
worker.check_abort()?;
|
2021-09-24 09:56:53 +00:00
|
|
|
worker.fail_on_shutdown()?;
|
2019-04-01 10:13:02 +00:00
|
|
|
|
2019-02-12 09:35:49 +00:00
|
|
|
let (dirfd, entry) = match entry {
|
2019-02-13 14:51:27 +00:00
|
|
|
Ok(entry) => (entry.parent_fd(), entry),
|
2019-07-04 09:21:54 +00:00
|
|
|
Err(err) => bail!("chunk iterator on chunk store '{}' failed - {}", self.name, err),
|
2018-12-19 11:49:23 +00:00
|
|
|
};
|
2019-02-12 09:35:49 +00:00
|
|
|
|
2018-12-19 11:49:23 +00:00
|
|
|
let file_type = match entry.file_type() {
|
|
|
|
Some(file_type) => file_type,
|
2018-12-19 12:40:26 +00:00
|
|
|
None => bail!("unsupported file system type on chunk store '{}'", self.name),
|
2018-12-19 11:49:23 +00:00
|
|
|
};
|
2019-02-12 09:43:31 +00:00
|
|
|
if file_type != nix::dir::Type::File {
|
|
|
|
continue;
|
|
|
|
}
|
2018-12-19 11:49:23 +00:00
|
|
|
|
2019-07-04 09:27:11 +00:00
|
|
|
chunk_count += 1;
|
|
|
|
|
2019-03-31 14:16:14 +00:00
|
|
|
let filename = entry.file_name();
|
|
|
|
|
2019-03-31 08:03:01 +00:00
|
|
|
let lock = self.mutex.lock();
|
|
|
|
|
2019-02-12 09:37:43 +00:00
|
|
|
if let Ok(stat) = fstatat(dirfd, filename, nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
|
2020-11-12 14:50:08 +00:00
|
|
|
if stat.st_atime < min_atime {
|
2020-04-06 07:50:40 +00:00
|
|
|
//let age = now - stat.st_atime;
|
|
|
|
//println!("UNLINK {} {:?}", age/(3600*24), filename);
|
2020-09-07 15:30:34 +00:00
|
|
|
if let Err(err) = unlinkat(Some(dirfd), filename, UnlinkatFlags::NoRemoveDir) {
|
2020-11-12 14:50:08 +00:00
|
|
|
if bad {
|
|
|
|
status.still_bad += 1;
|
|
|
|
}
|
2019-02-12 09:37:43 +00:00
|
|
|
bail!(
|
2020-09-07 15:30:34 +00:00
|
|
|
"unlinking chunk {:?} failed on store '{}' - {}",
|
2019-02-12 09:37:43 +00:00
|
|
|
filename,
|
|
|
|
self.name,
|
|
|
|
err,
|
|
|
|
);
|
2018-12-19 12:40:26 +00:00
|
|
|
}
|
2020-11-12 14:50:08 +00:00
|
|
|
if bad {
|
|
|
|
status.removed_bad += 1;
|
|
|
|
} else {
|
|
|
|
status.removed_chunks += 1;
|
|
|
|
}
|
2019-07-04 05:57:43 +00:00
|
|
|
status.removed_bytes += stat.st_size as u64;
|
2020-10-14 09:33:45 +00:00
|
|
|
} else if stat.st_atime < oldest_writer {
|
2020-11-12 14:50:08 +00:00
|
|
|
if bad {
|
|
|
|
status.still_bad += 1;
|
|
|
|
} else {
|
|
|
|
status.pending_chunks += 1;
|
|
|
|
}
|
2020-10-14 09:33:45 +00:00
|
|
|
status.pending_bytes += stat.st_size as u64;
|
2020-04-06 07:50:40 +00:00
|
|
|
} else {
|
2020-11-12 14:50:08 +00:00
|
|
|
if !bad {
|
|
|
|
status.disk_chunks += 1;
|
|
|
|
}
|
2020-10-14 09:33:45 +00:00
|
|
|
status.disk_bytes += stat.st_size as u64;
|
2018-12-19 08:51:33 +00:00
|
|
|
}
|
2018-12-19 11:49:23 +00:00
|
|
|
}
|
2019-03-31 08:03:01 +00:00
|
|
|
drop(lock);
|
2018-12-19 11:49:23 +00:00
|
|
|
}
|
2019-07-04 07:26:44 +00:00
|
|
|
|
2018-12-19 12:40:26 +00:00
|
|
|
Ok(())
|
2018-12-19 08:51:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:23 +00:00
|
|
|
pub fn insert_chunk(
|
2019-02-22 09:35:40 +00:00
|
|
|
&self,
|
2019-10-06 08:31:06 +00:00
|
|
|
chunk: &DataBlob,
|
|
|
|
digest: &[u8; 32],
|
2019-02-22 09:35:40 +00:00
|
|
|
) -> Result<(bool, u64), Error> {
|
2018-12-22 13:31:59 +00:00
|
|
|
|
2019-06-14 09:40:04 +00:00
|
|
|
//println!("DIGEST {}", proxmox::tools::digest_to_hex(digest));
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2019-06-28 13:48:09 +00:00
|
|
|
let (chunk_path, digest_str) = self.chunk_path(digest);
|
2018-12-07 15:12:45 +00:00
|
|
|
|
|
|
|
let lock = self.mutex.lock();
|
|
|
|
|
|
|
|
if let Ok(metadata) = std::fs::metadata(&chunk_path) {
|
|
|
|
if metadata.is_file() {
|
2020-09-07 15:30:36 +00:00
|
|
|
self.touch_chunk(digest)?;
|
2019-02-22 09:35:40 +00:00
|
|
|
return Ok((true, metadata.len()));
|
2018-12-07 15:12:45 +00:00
|
|
|
} else {
|
2018-12-19 12:54:22 +00:00
|
|
|
bail!("Got unexpected file type on store '{}' for chunk {}", self.name, digest_str);
|
2018-12-07 15:12:45 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
let mut tmp_path = chunk_path.clone();
|
|
|
|
tmp_path.set_extension("tmp");
|
2019-02-25 06:26:40 +00:00
|
|
|
|
2021-07-13 09:11:24 +00:00
|
|
|
let mut file = std::fs::File::create(&tmp_path).map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"creating temporary chunk on store '{}' failed for {} - {}",
|
|
|
|
self.name,
|
|
|
|
digest_str,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})?;
|
2019-02-25 06:26:40 +00:00
|
|
|
|
2019-06-13 09:47:23 +00:00
|
|
|
let raw_data = chunk.raw_data();
|
|
|
|
let encoded_size = raw_data.len() as u64;
|
2019-02-25 06:26:40 +00:00
|
|
|
|
2021-07-13 09:11:24 +00:00
|
|
|
file.write_all(raw_data).map_err(|err| {
|
|
|
|
format_err!(
|
|
|
|
"writing temporary chunk on store '{}' failed for {} - {}",
|
|
|
|
self.name,
|
|
|
|
digest_str,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
})?;
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 15:12:45 +00:00
|
|
|
if let Err(err) = std::fs::rename(&tmp_path, &chunk_path) {
|
2021-01-19 09:27:59 +00:00
|
|
|
if std::fs::remove_file(&tmp_path).is_err() { /* ignore */ }
|
2019-02-12 09:37:43 +00:00
|
|
|
bail!(
|
|
|
|
"Atomic rename on store '{}' failed for chunk {} - {}",
|
|
|
|
self.name,
|
|
|
|
digest_str,
|
|
|
|
err,
|
|
|
|
);
|
2018-12-07 15:12:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drop(lock);
|
|
|
|
|
2019-06-13 09:47:23 +00:00
|
|
|
Ok((false, encoded_size))
|
2018-12-07 13:44:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 13:48:09 +00:00
|
|
|
pub fn chunk_path(&self, digest:&[u8; 32]) -> (PathBuf, String) {
|
|
|
|
let mut chunk_path = self.chunk_dir.clone();
|
|
|
|
let prefix = digest_to_prefix(digest);
|
|
|
|
chunk_path.push(&prefix);
|
|
|
|
let digest_str = proxmox::tools::digest_to_hex(digest);
|
|
|
|
chunk_path.push(&digest_str);
|
|
|
|
(chunk_path, digest_str)
|
|
|
|
}
|
|
|
|
|
2018-12-15 13:51:05 +00:00
|
|
|
pub fn relative_path(&self, path: &Path) -> PathBuf {
|
|
|
|
|
|
|
|
let mut full_path = self.base.clone();
|
|
|
|
full_path.push(path);
|
|
|
|
full_path
|
|
|
|
}
|
|
|
|
|
2020-06-24 04:54:21 +00:00
|
|
|
pub fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
2018-12-18 10:06:03 +00:00
|
|
|
pub fn base_path(&self) -> PathBuf {
|
|
|
|
self.base.clone()
|
|
|
|
}
|
2019-03-22 08:42:15 +00:00
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
pub fn try_shared_lock(&self) -> Result<process_locker::ProcessLockSharedGuard, Error> {
|
|
|
|
ProcessLocker::try_shared_lock(self.locker.clone())
|
2019-03-22 08:42:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
pub fn try_exclusive_lock(&self) -> Result<process_locker::ProcessLockExclusiveGuard, Error> {
|
|
|
|
ProcessLocker::try_exclusive_lock(self.locker.clone())
|
2019-03-22 08:42:15 +00:00
|
|
|
}
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_chunk_store1() {
|
|
|
|
|
2021-03-10 15:37:09 +00:00
|
|
|
let mut path = std::fs::canonicalize(".").unwrap(); // we need absolute path
|
2019-01-25 09:14:25 +00:00
|
|
|
path.push(".testdir");
|
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
if let Err(_e) = std::fs::remove_dir_all(".testdir") { /* ignore */ }
|
|
|
|
|
2019-01-25 09:14:25 +00:00
|
|
|
let chunk_store = ChunkStore::open("test", &path);
|
2018-12-07 09:51:53 +00:00
|
|
|
assert!(chunk_store.is_err());
|
|
|
|
|
2019-12-20 08:23:58 +00:00
|
|
|
let user = nix::unistd::User::from_uid(nix::unistd::Uid::current()).unwrap().unwrap();
|
2021-06-02 11:27:03 +00:00
|
|
|
let chunk_store = ChunkStore::create("test", &path, user.uid, user.gid, None).unwrap();
|
2019-06-13 09:47:23 +00:00
|
|
|
|
2021-07-07 12:37:47 +00:00
|
|
|
let (chunk, digest) = crate::data_blob::DataChunkBuilder::new(&[0u8, 1u8]).build().unwrap();
|
2019-06-13 09:47:23 +00:00
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
let (exists, _) = chunk_store.insert_chunk(&chunk, &digest).unwrap();
|
2018-12-08 10:25:11 +00:00
|
|
|
assert!(!exists);
|
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
let (exists, _) = chunk_store.insert_chunk(&chunk, &digest).unwrap();
|
2018-12-08 10:25:11 +00:00
|
|
|
assert!(exists);
|
2018-12-07 13:44:56 +00:00
|
|
|
|
2018-12-07 09:51:53 +00:00
|
|
|
|
2021-06-02 11:27:03 +00:00
|
|
|
let chunk_store = ChunkStore::create("test", &path, user.uid, user.gid, None);
|
2018-12-07 09:51:53 +00:00
|
|
|
assert!(chunk_store.is_err());
|
|
|
|
|
2019-01-31 14:29:25 +00:00
|
|
|
if let Err(_e) = std::fs::remove_dir_all(".testdir") { /* ignore */ }
|
2018-12-07 09:51:53 +00:00
|
|
|
}
|