Improve mount waiting + misc doc improvements

Adds user.mergerfs.branch and user.mergerfs.branch_mounts_here
Adds .mergerfs.branch and .mergerfs.branch_mounts_here files.
This commit is contained in:
Antonio SJ Musumeci 2025-04-18 01:08:41 -05:00
parent 212681ddc5
commit de2a82800a
26 changed files with 583 additions and 212 deletions

View file

@ -56,6 +56,7 @@ namespace l
{
IFERT("async_read");
IFERT("branches-mount-timeout");
IFERT("branches-mount-timeout-fail");
IFERT("cache.symlinks");
IFERT("cache.writeback");
IFERT("direct-io-allow-mmap");
@ -85,6 +86,7 @@ Config::Config()
minfreespace(MINFREESPACE_DEFAULT),
branches(minfreespace),
branches_mount_timeout(0),
branches_mount_timeout_fail(false),
cache_attr(1),
cache_entry(1),
cache_files(CacheFiles::ENUM::OFF),
@ -139,6 +141,7 @@ Config::Config()
_map["auto_cache"] = &auto_cache;
_map["branches"] = &branches;
_map["branches-mount-timeout"] = &branches_mount_timeout;
_map["branches-mount-timeout-fail"] = &branches_mount_timeout_fail;
_map["cache.attr"] = &cache_attr;
_map["cache.entry"] = &cache_entry;
_map["cache.files"] = &cache_files;

View file

@ -41,6 +41,8 @@
#include "rwlock.hpp"
#include "tofrom_wrapper.hpp"
#include "ghc/filesystem.hpp"
#include "fuse.h"
#include <cstdint>
@ -55,6 +57,7 @@ typedef ToFromWrapper<bool> ConfigBOOL;
typedef ToFromWrapper<uint64_t> ConfigUINT64;
typedef ToFromWrapper<int> ConfigINT;
typedef ToFromWrapper<std::string> ConfigSTR;
typedef ToFromWrapper<ghc::filesystem::path> ConfigPath;
typedef std::map<std::string,ToFromString*> Str2TFStrMap;
extern const std::string CONTROLFILE;
@ -108,6 +111,7 @@ public:
ConfigUINT64 minfreespace;
Branches branches;
ConfigUINT64 branches_mount_timeout;
ConfigBOOL branches_mount_timeout_fail;
ConfigUINT64 cache_attr;
ConfigUINT64 cache_entry;
CacheFiles cache_files;
@ -133,7 +137,7 @@ public:
ConfigBOOL link_cow;
LinkEXDEV link_exdev;
LogMetrics log_metrics;
ConfigSTR mountpoint;
ConfigPath mountpoint;
MoveOnENOSPC moveonenospc;
NFSOpenHack nfsopenhack;
ConfigBOOL nullrw;

View file

@ -16,12 +16,11 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "from_string.hpp"
#include "ef.hpp"
#include "errno.hpp"
#include <cstdint>
#include <string>
#include <stdlib.h>
@ -128,4 +127,13 @@ namespace str
{
return -EINVAL;
}
int
from(const std::string &value_,
fs::Path *path_)
{
*path_ = value_;
return 0;
}
}

View file

@ -18,6 +18,8 @@
#pragma once
#include "fs_path.hpp"
#include <cstdint>
#include <string>
@ -29,4 +31,5 @@ namespace str
int from(const std::string &, uint64_t *);
int from(const std::string &, std::string *);
int from(const std::string &, const std::string *);
int from(const std::string &, fs::Path *);
}

View file

@ -18,7 +18,7 @@
#pragma once
#include "ghc/filesystem.hpp"
#include "fs_path.hpp"
#include <string>
@ -49,8 +49,8 @@ namespace fs
static
inline
int
mkdir(const ghc::filesystem::path &path_,
const mode_t mode_)
mkdir(const fs::Path &path_,
const mode_t mode_)
{
return fs::mkdir(path_.c_str(),mode_);
}

View file

@ -30,7 +30,7 @@
#define PAD_LEN 16
#define MAX_ATTEMPTS 3
static char const CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static char const CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static size_t const CHARS_SIZE = (sizeof(CHARS) - 1);
@ -57,8 +57,8 @@ namespace l
namespace fs
{
std::tuple<int,std::string>
mktemp_in_dir(std::string const dirpath_,
int const flags_)
mktemp_in_dir(const std::string dirpath_,
const int flags_)
{
int fd;
int count;
@ -85,10 +85,10 @@ namespace fs
}
std::tuple<int,std::string>
mktemp(std::string const filepath_,
int const flags_)
mktemp(const std::string filepath_,
const int flags_)
{
ghc::filesystem::path filepath{filepath_};
fs::Path filepath{filepath_};
return fs::mktemp_in_dir(filepath.parent_path(),flags_);
}

37
src/fs_mounts.cpp Normal file
View file

@ -0,0 +1,37 @@
#include "fs_mounts.hpp"
#include <cstdio>
#include <mntent.h>
#ifdef __linux__
void
fs::mounts(fs::MountVec &mounts_)
{
FILE *f;
f = setmntent("/proc/mounts","r");
if(f == NULL)
return;
struct mntent *entry;
while((entry = getmntent(f)) != NULL)
{
fs::Mount m;
m.dir = entry->mnt_dir;
m.fsname = entry->mnt_fsname;
m.type = entry->mnt_type;
m.opts = entry->mnt_opts;
mounts_.emplace_back(std::move(m));
}
endmntent(f);
}
#else
void
fs::mounts(fs::MountVec &mounts_)
{
}
#endif

21
src/fs_mounts.hpp Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include "fs_path.hpp"
#include <vector>
namespace fs
{
struct Mount
{
fs::Path dir;
std::string fsname;
std::string type;
std::string opts;
};
typedef std::vector<fs::Mount> MountVec;
void mounts(fs::MountVec &mounts);
}

View file

@ -19,6 +19,10 @@
#include "fs_wait_for_mount.hpp"
#include "syslog.hpp"
#include "fs_exists.hpp"
#include "fs_lstat.hpp"
#include "fs_lgetxattr.hpp"
#include <functional>
#include <thread>
#include <unordered_set>
@ -44,6 +48,40 @@ namespace std
};
}
static
bool
_branch_is_mounted(const struct stat &src_st_,
const fs::Path &branch_path_)
{
int rv;
struct stat st;
fs::Path filepath;
rv = fs::lgetxattr(branch_path_,"user.mergerfs.branch",NULL,0);
if(rv != -1)
return true;
filepath = branch_path_ / ".mergerfs.branch";
rv = fs::exists(filepath);
if(rv)
return true;
rv = fs::lgetxattr(branch_path_,"user.mergerfs.branch_mounts_here",NULL,0);
if(rv != -1)
return false;
filepath = branch_path_ / ".mergerfs.branch_mounts_here";
rv = fs::exists(filepath);
if(rv)
return false;
rv = fs::lstat(branch_path_,&st);
if(rv == 0)
return (st.st_dev != src_st_.st_dev);
return false;
}
static
void
_check_mounted(const struct stat &src_st_,
@ -56,30 +94,23 @@ _check_mounted(const struct stat &src_st_,
for(auto const &tgt_path : tgt_paths_)
{
int rv;
struct stat tgt_st;
bool mounted;
rv = fs::stat(tgt_path,&tgt_st);
if(rv == 0)
{
if(tgt_st.st_dev != src_st_.st_dev)
successes.push_back(tgt_path);
else
failures.push_back(tgt_path);
}
mounted = ::_branch_is_mounted(src_st_,tgt_path);
if(mounted)
successes.push_back(tgt_path);
else
{
failures.push_back(tgt_path);
}
failures.push_back(tgt_path);
}
}
static
void
int
_wait_for_mount(const struct stat &src_st_,
const fs::PathVector &tgt_paths_,
const std::chrono::milliseconds &timeout_)
{
bool first_loop;
fs::PathVector successes;
fs::PathVector failures;
std::unordered_set<fs::Path> tgt_paths;
@ -90,6 +121,7 @@ _wait_for_mount(const struct stat &src_st_,
now = std::chrono::steady_clock::now();
deadline = now + timeout_;
first_loop = true;
while(true)
{
if(tgt_paths.empty())
@ -100,10 +132,17 @@ _wait_for_mount(const struct stat &src_st_,
successes.clear();
failures.clear();
::_check_mounted(src_st_,tgt_paths,&successes,&failures);
for(auto const &path : successes)
for(const auto &path : successes)
{
tgt_paths.erase(path);
syslog_info("%s is mounted",path.string().c_str());
syslog_info("%s is mounted",path.c_str());
}
if(first_loop)
{
for(const auto &path : failures)
syslog_notice("%s is not mounted, waiting",path.c_str());
first_loop = false;
}
std::this_thread::sleep_for(SLEEP_DURATION);
@ -111,26 +150,24 @@ _wait_for_mount(const struct stat &src_st_,
}
for(auto const &path : failures)
syslog_notice("%s not mounted within timeout",path.string().c_str());
if(!failures.empty())
syslog_warning("Continuing to mount mergerfs despite %u branches not "
"being different from the mountpoint filesystem",
failures.size());
syslog_notice("%s not mounted within timeout",path.c_str());
return failures.size();
}
void
int
fs::wait_for_mount(const fs::Path &src_path_,
const fs::PathVector &tgt_paths_,
const std::chrono::milliseconds &timeout_)
{
int rv;
struct stat src_st;
struct stat src_st = {0};
rv = fs::stat(src_path_,&src_st);
if(rv == -1)
return syslog_error("Error stat'ing mount path: %s (%s)",
src_path_.c_str(),
strerror(errno));
syslog_error("Error stat'ing mount path: %s (%s)",
src_path_.c_str(),
strerror(errno));
::_wait_for_mount(src_st,tgt_paths_,timeout_);
return ::_wait_for_mount(src_st,tgt_paths_,timeout_);
}

View file

@ -1,6 +1,5 @@
/*
ISC License
Copyright (c) 2023, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
@ -29,7 +28,7 @@
namespace fs
{
void
int
wait_for_mount(const fs::Path &srcpath,
const fs::PathVector &tgtpaths,
const std::chrono::milliseconds &timeout);

View file

@ -19,12 +19,13 @@
#include "fs_readahead.hpp"
#include "syslog.hpp"
#include "fs_path.hpp"
#include "fs_exists.hpp"
#include "fmt/core.h"
#include "fuse.h"
#include "ghc/filesystem.hpp"
#include <thread>
#include <algorithm>
@ -82,7 +83,7 @@ namespace l
std::fstream f;
uint64_t max_pages_limit;
if(ghc::filesystem::exists(MAX_PAGES_LIMIT_FILEPATH))
if(fs::exists(MAX_PAGES_LIMIT_FILEPATH))
{
if(cfg_->fuse_msg_size > MAX_FUSE_MSG_SIZE)
syslog_info("fuse_msg_size > %u: setting it to %u",
@ -141,8 +142,8 @@ namespace l
static
void
readahead(const std::string path_,
const int readahead_)
readahead(const fs::Path path_,
const int readahead_)
{
int rv;

View file

@ -32,7 +32,6 @@
using std::string;
using std::vector;
namespace gfs = ghc::filesystem;
namespace error
{
@ -230,8 +229,8 @@ namespace l
fuse_timeouts_t *timeouts_)
{
int rv;
gfs::path target(oldpath_);
gfs::path linkpath(newpath_);
fs::Path target(oldpath_);
fs::Path linkpath(newpath_);
target = target.lexically_relative(linkpath.parent_path());
@ -278,11 +277,11 @@ namespace l
static
int
link_exdev_abs_pool_symlink(const std::string &mount_,
const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
link_exdev_abs_pool_symlink(const fs::Path mount_,
const char *oldpath_,
const char *newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
StrVec basepaths;

View file

@ -315,7 +315,7 @@ namespace l
int
rename_exdev_abs_symlink(const Policy::Action &actionPolicy_,
const Branches::CPtr &branches_,
const std::string &mount_,
const gfs::path &mount_,
const gfs::path &oldfusepath_,
const gfs::path &newfusepath_)
{

View file

@ -165,26 +165,51 @@ namespace l
}
static
void
bool
wait_for_mount(const Config::Read &cfg_)
{
int failures;
fs::PathVector paths;
std::chrono::milliseconds timeout;
paths = cfg_->branches->to_paths();
syslog_info("Waiting %u seconds for branches to mount",
(uint64_t)cfg_->branches_mount_timeout);
syslog_info("Waiting %u seconds for %zu branches to mount",
(uint64_t)cfg_->branches_mount_timeout,
paths.size());
timeout = std::chrono::milliseconds(cfg_->branches_mount_timeout * 1000);
fs::wait_for_mount((std::string)cfg_->mountpoint,
paths,
timeout);
failures = fs::wait_for_mount(cfg_->mountpoint,
paths,
timeout);
if(failures)
{
if(cfg_->branches_mount_timeout_fail)
{
syslog_error("%d of %zu branches were not mounted"
" within the timeout of %zus. Exiting",
failures,
paths.size(),
(uint64_t)cfg_->branches_mount_timeout);
return true;
}
syslog_warning("Continuing to mount mergerfs despite %d branches not "
"being different from the mountpoint filesystem",
failures);
}
else
{
syslog_info("All %zd branches are mounted",
paths.size());
}
return false;
}
static
void
lazy_umount(const std::string target_)
lazy_umount(const fs::Path target_)
{
int rv;
@ -276,7 +301,13 @@ namespace l
}
if(cfg->branches_mount_timeout > 0)
l::wait_for_mount(cfg);
{
bool failure;
failure = l::wait_for_mount(cfg);
if(failure)
return 1;
}
l::setup_resources(cfg->scheduling_priority);
l::setup_signal_handlers();

View file

@ -421,7 +421,7 @@ check_for_mount_loop(Config::Write &cfg_,
fs::PathVector branches;
std::error_code ec;
mount = (std::string)cfg_->mountpoint;
mount = *cfg_->mountpoint;
branches = cfg_->branches->to_paths();
for(const auto &branch : branches)
{

View file

@ -16,6 +16,8 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "to_string.hpp"
#include <cstdint>
#include <string>
@ -55,4 +57,10 @@ namespace str
{
return s_;
}
std::string
to(const fs::Path &path_)
{
return path_.string();
}
}

View file

@ -18,6 +18,8 @@
#pragma once
#include "fs_path.hpp"
#include <cstdint>
#include <string>
@ -28,4 +30,5 @@ namespace str
std::string to(const int);
std::string to(const uint64_t);
std::string to(const std::string&);
std::string to(const fs::Path&);
}

View file

@ -64,6 +64,13 @@ public:
return _data;
}
const
T&
operator*() const
{
return _data;
}
T*
operator->()
{