Rework config, centralize fuse config (#1547)
Some checks are pending
CodeQL / Analyze (push) Waiting to run
mkdocs / deploy (push) Waiting to run

This commit is contained in:
trapexit 2025-10-24 08:20:29 -05:00 • committed by GitHub
parent 00e5467e99
commit 727c522fb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 1294 additions and 1316 deletions

View file

@ -64,6 +64,7 @@ Branch::to_string(void) const
break;
}
if(std::holds_alternative<u64>(_minfreespace))
{
rv += ',';

View file

@ -39,213 +39,263 @@
#define MINFREESPACE_DEFAULT (4294967295ULL)
#define IFERT(S) if(S == s_) return true
constexpr static const char CACHE_FILES_PROCESS_NAMES_DEFAULT[] =
"rtorrent|"
"qbittorrent-nox";
Config cfg;
namespace l
Config::CfgConfigFile::CfgConfigFile()
{
static
bool
readonly(const std::string &s_)
{
IFERT("allow-idmap");
IFERT("async_read");
IFERT("branches-mount-timeout");
IFERT("branches-mount-timeout-fail");
IFERT("cache.symlinks");
IFERT("cache.writeback");
IFERT("direct-io-allow-mmap");
IFERT("export-support");
IFERT("fsname");
IFERT("fuse_msg_size");
IFERT("kernel-permissions-check");
IFERT("mount");
IFERT("nullrw");
IFERT("pid");
IFERT("pin-threads");
IFERT("process-thread-count");
IFERT("process-thread-queue-depth");
IFERT("read-thread-count");
IFERT("readdirplus");
IFERT("scheduling-priority");
IFERT("srcmounts");
IFERT("threads");
IFERT("version");
return false;
}
}
Config::Config()
: allow_idmap(false),
async_read(true),
auto_cache(false),
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),
cache_files_process_names(CACHE_FILES_PROCESS_NAMES_DEFAULT),
cache_negative_entry(0),
cache_readdir(false),
cache_statfs(0),
cache_symlinks(false),
category(func),
direct_io(false),
direct_io_allow_mmap(true),
dropcacheonclose(false),
export_support(true),
flushonclose(FlushOnClose::ENUM::OPENED_FOR_WRITE),
follow_symlinks(FollowSymlinks::ENUM::NEVER),
fsname(),
func(),
fuse_msg_size("1M"),
gid_cache_expire_timeout(60 * 60),
gid_cache_remove_timeout(60 * 60 * 12),
ignorepponrename(false),
inodecalc("hybrid-hash"),
lazy_umount_mountpoint(false),
link_cow(false),
link_exdev(LinkEXDEV::ENUM::PASSTHROUGH),
log_metrics(false),
mountpoint(),
moveonenospc(true),
nfsopenhack(NFSOpenHack::ENUM::OFF),
nullrw(false),
parallel_direct_writes(true),
posix_acl(false),
proxy_ioprio(false),
readahead(0),
readdir("seq"),
readdirplus(false),
rename_exdev(RenameEXDEV::ENUM::PASSTHROUGH),
scheduling_priority(-10),
security_capability(true),
srcmounts(branches),
statfs(StatFS::ENUM::BASE),
statfs_ignore(StatFSIgnore::ENUM::NONE),
symlinkify(false),
symlinkify_timeout(3600),
fuse_read_thread_count(0),
fuse_process_thread_count(-1),
fuse_process_thread_queue_depth(2),
fuse_pin_threads("false"),
version(MERGERFS_VERSION),
writeback_cache(false),
xattr(XAttr::ENUM::PASSTHROUGH),
_initialized(false)
{
_map["allow-idmap"] = &allow_idmap;
_map["async_read"] = &async_read;
_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;
_map["cache.files.process-names"] = &cache_files_process_names;
_map["cache.negative_entry"] = &cache_negative_entry;
_map["cache.readdir"] = &cache_readdir;
_map["cache.statfs"] = &cache_statfs;
_map["cache.symlinks"] = &cache_symlinks;
_map["cache.writeback"] = &writeback_cache;
_map["category.action"] = &category.action;
_map["category.create"] = &category.create;
_map["category.search"] = &category.search;
_map["direct_io"] = &direct_io;
_map["direct-io-allow-mmap"] = &direct_io_allow_mmap;
_map["dropcacheonclose"] = &dropcacheonclose;
_map["export-support"] = &export_support;
_map["flush-on-close"] = &flushonclose;
_map["follow-symlinks"] = &follow_symlinks;
_map["fsname"] = &fsname;
_map["func.access"] = &func.access;
_map["func.chmod"] = &func.chmod;
_map["func.chown"] = &func.chown;
_map["func.create"] = &func.create;
_map["func.getattr"] = &func.getattr;
_map["func.getxattr"] = &func.getxattr;
_map["func.link"] = &func.link;
_map["func.listxattr"] = &func.listxattr;
_map["func.mkdir"] = &func.mkdir;
_map["func.mknod"] = &func.mknod;
_map["func.open"] = &func.open;
_map["func.readdir"] = &readdir;
_map["func.readlink"] = &func.readlink;
_map["func.removexattr"] = &func.removexattr;
_map["func.rename"] = &func.rename;
_map["func.rmdir"] = &func.rmdir;
_map["func.setxattr"] = &func.setxattr;
_map["func.symlink"] = &func.symlink;
_map["func.truncate"] = &func.truncate;
_map["func.unlink"] = &func.unlink;
_map["func.utimens"] = &func.utimens;
_map["fuse_msg_size"] = &fuse_msg_size;
_map["gid-cache-expire-timeout"] = &gid_cache_expire_timeout;
_map["gid-cache-remove-timeout"] = &gid_cache_remove_timeout;
_map["handle-killpriv"] = &handle_killpriv;
_map["handle-killpriv-v2"] = &handle_killpriv_v2;
_map["ignorepponrename"] = &ignorepponrename;
_map["inodecalc"] = &inodecalc;
_map["kernel_cache"] = &kernel_cache;
_map["kernel-permissions-check"] = &kernel_permissions_check;
_map["lazy-umount-mountpoint"] = &lazy_umount_mountpoint;
_map["link_cow"] = &link_cow;
_map["link-exdev"] = &link_exdev;
_map["log.metrics"] = &log_metrics;
_map["minfreespace"] = &minfreespace;
_map["mount"] = &mountpoint;
_map["moveonenospc"] = &moveonenospc;
_map["nfsopenhack"] = &nfsopenhack;
_map["nullrw"] = &nullrw;
_map["passthrough"] = &passthrough;
_map["pid"] = &pid;
_map["parallel-direct-writes"] = &parallel_direct_writes;
_map["pin-threads"] = &fuse_pin_threads;
_map["posix_acl"] = &posix_acl;
_map["proxy-ioprio"] = &proxy_ioprio;
_map["readahead"] = &readahead;
_map["readdirplus"] = &readdirplus;
_map["rename-exdev"] = &rename_exdev;
_map["scheduling-priority"] = &scheduling_priority;
_map["security_capability"] = &security_capability;
_map["srcmounts"] = &srcmounts;
_map["statfs"] = &statfs;
_map["statfs_ignore"] = &statfs_ignore;
_map["symlinkify"] = &symlinkify;
_map["symlinkify_timeout"] = &symlinkify_timeout;
_map["threads"] = &fuse_read_thread_count;
_map["read-thread-count"] = &fuse_read_thread_count;
_map["process-thread-count"] = &fuse_process_thread_count;
_map["process-thread-queue-depth"] = &fuse_process_thread_queue_depth;
_map["version"] = &version;
_map["xattr"] = &xattr;
}
Config&
Config::operator=(const Config &cfg_)
int
Config::CfgConfigFile::from_string(const std::string_view s_)
{
int rv;
std::string val;
fs::path cfg_file;
for(auto &kv : _map)
{
rv = cfg_.get(kv.first,&val);
if(rv)
continue;
if(_depth > 5)
return -ELOOP;
_depth++;
kv.second->from_string(val);
}
cfg_file = (s_.empty() ? _cfg_file : s_);
return *this;
rv = cfg.from_file(cfg_file);
if(rv == 0)
_cfg_file = cfg_file;
_depth--;
return rv;
}
std::string
Config::CfgConfigFile::to_string() const
{
return _cfg_file;
}
Config::Config()
:
allow_idmap(false),
async_read(true),
branches(minfreespace),
branches_mount_timeout(0),
branches_mount_timeout_fail(false),
cache_attr(1),
cache_entry(1),
cache_files(CacheFiles::ENUM::OFF),
cache_files_process_names(CACHE_FILES_PROCESS_NAMES_DEFAULT),
cache_negative_entry(0),
cache_readdir(false),
cache_statfs(0),
cache_symlinks(false),
cache_writeback(false),
category(func),
config_file(),
congestion_threshold(fuse_cfg.congestion_threshold,0),
debug(fuse_cfg.debug,false),
direct_io_allow_mmap(true),
dropcacheonclose(false),
export_support(true),
flushonclose(FlushOnClose::ENUM::OPENED_FOR_WRITE),
follow_symlinks(FollowSymlinks::ENUM::NEVER),
fsname(),
func(),
fuse_msg_size("1M"),
gid(fuse_cfg.gid,FUSE_CFG_INVALID_ID),
gid_cache_expire_timeout(60 * 60),
gid_cache_remove_timeout(60 * 60 * 12),
handle_killpriv(true),
handle_killpriv_v2(true),
ignorepponrename(false),
inodecalc("hybrid-hash"),
kernel_permissions_check(true),
lazy_umount_mountpoint(false),
link_cow(false),
link_exdev(LinkEXDEV::ENUM::PASSTHROUGH),
log_metrics(false),
max_background(fuse_cfg.max_background,0),
minfreespace(MINFREESPACE_DEFAULT),
mountpoint(),
_mount(mountpoint),
_mountpoint(mountpoint),
moveonenospc(true),
nfsopenhack(NFSOpenHack::ENUM::OFF),
noforget(),
nullrw(false),
parallel_direct_writes(true),
passthrough_io(PassthroughIO::ENUM::OFF),
passthrough_max_stack_depth(fuse_cfg.passthrough_max_stack_depth,1),
pin_threads(fuse_cfg.pin_threads,"false"),
posix_acl(false),
process_thread_count(fuse_cfg.process_thread_count,-1),
process_thread_queue_depth(fuse_cfg.process_thread_queue_depth,2),
proxy_ioprio(false),
read_thread_count(fuse_cfg.read_thread_count,0),
readahead(0),
readdir("seq"),
remember_nodes(fuse_cfg.remember_nodes,0),
rename_exdev(RenameEXDEV::ENUM::PASSTHROUGH),
scheduling_priority(-10),
security_capability(true),
srcmounts(branches),
statfs(StatFS::ENUM::BASE),
statfs_ignore(StatFSIgnore::ENUM::NONE),
symlinkify(false),
symlinkify_timeout(3600),
threads(fuse_cfg.read_thread_count),
uid(fuse_cfg.uid,FUSE_CFG_INVALID_ID),
umask(fuse_cfg.umask,FUSE_CFG_INVALID_UMASK),
version(MERGERFS_VERSION),
xattr(XAttr::ENUM::PASSTHROUGH),
_initialized(false)
{
allow_idmap.ro =
async_read.ro =
branches_mount_timeout.ro =
branches_mount_timeout_fail.ro =
cache_symlinks.ro =
cache_writeback.ro =
congestion_threshold.ro =
direct_io_allow_mmap.ro =
export_support.ro =
fsname.ro =
fuse_msg_size.ro =
handle_killpriv.ro =
handle_killpriv_v2.ro =
kernel_permissions_check.ro =
max_background.ro =
_mount.ro =
_mountpoint.ro =
nullrw.ro =
pid.ro =
pin_threads.ro =
process_thread_count.ro =
process_thread_queue_depth.ro =
read_thread_count.ro =
scheduling_priority.ro =
srcmounts.ro =
version.ro =
true;
congestion_threshold.ro =
gid.display =
max_background.ro =
threads.display =
_mount.display =
uid.display =
umask.display =
false;
_map["allow-idmap"] = &allow_idmap;
_map["async-read"] = &async_read;
_map["atomic-o-trunc"] = &_dummy;
_map["auto-cache"] = &_dummy;
_map["big-writes"] = &_dummy;
_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;
_map["cache.files.process-names"] = &cache_files_process_names;
_map["cache.negative-entry"] = &cache_negative_entry;
_map["cache.open"] = &_dummy;
_map["cache.readdir"] = &cache_readdir;
_map["cache.statfs"] = &cache_statfs;
_map["cache.symlinks"] = &cache_symlinks;
_map["cache.writeback"] = &cache_writeback;
_map["category.action"] = &category.action;
_map["category.create"] = &category.create;
_map["category.search"] = &category.search;
_map["config"] = &config_file;
_map["debug"] = &debug;
_map["direct-io-allow-mmap"] = &direct_io_allow_mmap;
_map["direct-io"] = &_dummy;
_map["dropcacheonclose"] = &dropcacheonclose;
_map["export-support"] = &export_support;
_map["flush-on-close"] = &flushonclose;
_map["follow-symlinks"] = &follow_symlinks;
_map["fsname"] = &fsname;
_map["func.access"] = &func.access;
_map["func.chmod"] = &func.chmod;
_map["func.chown"] = &func.chown;
_map["func.create"] = &func.create;
_map["func.getattr"] = &func.getattr;
_map["func.getxattr"] = &func.getxattr;
_map["func.link"] = &func.link;
_map["func.listxattr"] = &func.listxattr;
_map["func.mkdir"] = &func.mkdir;
_map["func.mknod"] = &func.mknod;
_map["func.open"] = &func.open;
_map["func.readdir"] = &readdir;
_map["func.readlink"] = &func.readlink;
_map["func.removexattr"] = &func.removexattr;
_map["func.rename"] = &func.rename;
_map["func.rmdir"] = &func.rmdir;
_map["func.setxattr"] = &func.setxattr;
_map["func.symlink"] = &func.symlink;
_map["func.truncate"] = &func.truncate;
_map["func.unlink"] = &func.unlink;
_map["func.utimens"] = &func.utimens;
_map["fuse_msg_size"] = &fuse_msg_size;
_map["gid"] = &gid;
_map["gid-cache.expire-timeout"] = &gid_cache_expire_timeout;
_map["gid-cache.remove-timeout"] = &gid_cache_remove_timeout;
_map["handle-killpriv"] = &handle_killpriv;
_map["handle-killpriv-v2"] = &handle_killpriv_v2;
_map["hard-remove"] = &_dummy;
_map["ignorepponrename"] = &ignorepponrename;
_map["inodecalc"] = &inodecalc;
_map["kernel-permissions-check"] = &kernel_permissions_check;
_map["kernel-cache"] = &_dummy;
_map["lazy-umount-mountpoint"] = &lazy_umount_mountpoint;
_map["link-exdev"] = &link_exdev;
_map["link-cow"] = &link_cow;
_map["log.metrics"] = &log_metrics;
_map["minfreespace"] = &minfreespace;
_map["mount"] = &_mount;
_map["mountpoint"] = &_mountpoint;
_map["moveonenospc"] = &moveonenospc;
_map["negative-entry"] = &_dummy;
_map["nfsopenhack"] = &nfsopenhack;
_map["no-splice-move"] = &_dummy;
_map["no-splice-read"] = &_dummy;
_map["no-splice-write"] = &_dummy;
_map["noforget"] = &noforget;
_map["nonempty"] = &_dummy;
_map["nullrw"] = &nullrw;
_map["parallel-direct-writes"] = &parallel_direct_writes;
_map["passthrough.io"] = &passthrough_io;
_map["passthrough.max-stack-depth"] = &passthrough_max_stack_depth;
_map["pid"] = &pid;
_map["pin-threads"] = &pin_threads;
_map["posix_acl"] = &posix_acl;
_map["process-thread-count"] = &process_thread_count;
_map["process-thread-queue-depth"] = &process_thread_queue_depth;
_map["proxy-ioprio"] = &proxy_ioprio;
_map["read-thread-count"] = &read_thread_count;
_map["readahead"] = &readahead;
_map["remember-nodes"] = &remember_nodes;
_map["rename-exdev"] = &rename_exdev;
_map["scheduling-priority"] = &scheduling_priority;
_map["security-capability"] = &security_capability;
_map["splice-move"] = &_dummy;
_map["splice-read"] = &_dummy;
_map["splice-write"] = &_dummy;
_map["srcmounts"] = &srcmounts;
_map["statfs"] = &statfs;
_map["statfs-ignore"] = &statfs_ignore;
_map["symlinkify"] = &symlinkify;
_map["symlinkify-timeout"] = &symlinkify_timeout;
_map["threads"] = &threads;
_map["uid"] = &uid;
_map["umask"] = &umask;
_map["use-ino"] = &_dummy;
_map["version"] = &version;
_map["xattr"] = &xattr;
}
bool
@ -275,6 +325,9 @@ Config::keys_xattr(std::string &s_) const
s_.reserve(1024);
for(const auto &[key,val] : _map)
{
if(val->display == false)
continue;
s_ += "user.mergerfs.";
s_ += key;
s_ += '\0';
@ -289,6 +342,9 @@ Config::keys_listxattr_size() const
rv = 0;
for(const auto &[key,val] : _map)
{
if(val->display == false)
continue;
rv += sizeof("user.mergerfs.");
rv += key.size();
}
@ -308,6 +364,9 @@ Config::keys_listxattr(char *list_,
for(const auto &[key,val] : _map)
{
if(val->display == false)
continue;
auto rv = fmt::format_to_n(list,size,
"user.mergerfs.{}\0",
key);
@ -324,9 +383,12 @@ int
Config::get(const std::string &key_,
std::string *val_) const
{
std::string key;
Str2TFStrMap::const_iterator i;
i = _map.find(key_);
key = str::replace(key_,'_','-');
i = _map.find(key);
if(i == _map.end())
return -ENOATTR;
@ -335,27 +397,22 @@ Config::get(const std::string &key_,
return 0;
}
int
Config::set_raw(const std::string &key_,
const std::string &value_)
{
Str2TFStrMap::iterator i;
i = _map.find(key_);
if(i == _map.end())
return -ENOATTR;
return i->second->from_string(value_);
}
int
Config::set(const std::string &key_,
const std::string &value_)
{
if(_initialized && l::readonly(key_))
std::string key;
Str2TFStrMap::iterator i;
key = str::replace(key_,'_','-');
i = _map.find(key);
if(i == _map.end())
return -ENOATTR;
if(_initialized && i->second->ro)
return -EROFS;
return set_raw(key_,value_);
return i->second->from_string(value_);
}
int
@ -372,16 +429,13 @@ Config::set(const std::string &kv_)
}
int
Config::from_stream(std::istream &istrm_,
ErrVec *errs_)
Config::from_stream(std::istream &istrm_)
{
int rv;
std::string line;
std::string key;
std::string val;
Config newcfg;
newcfg = *this;
Config::ErrVec new_errs;
while(std::getline(istrm_,line,'\n'))
{
@ -393,22 +447,21 @@ Config::from_stream(std::istream &istrm_,
key = str::trim(key);
val = str::trim(val);
rv = newcfg.set(key,val);
rv = set(key,val);
if(rv < 0)
errs_->push_back({rv,key});
new_errs.push_back({-rv,key+'='+val});
}
if(!errs_->empty())
return -EINVAL;
rv = (new_errs.empty() ? 0 : -EINVAL);
errs.insert(errs.end(),
new_errs.begin(),
new_errs.end());
*this = newcfg;
return 0;
return rv;
}
int
Config::from_file(const std::string &filepath_,
ErrVec *errs_)
Config::from_file(const std::string &filepath_)
{
int rv;
std::ifstream ifstrm;
@ -416,11 +469,11 @@ Config::from_file(const std::string &filepath_,
ifstrm.open(filepath_);
if(!ifstrm.good())
{
errs_->push_back({-errno,filepath_});
errs.push_back({errno,filepath_});
return -errno;
}
rv = from_stream(ifstrm,errs_);
rv = from_stream(ifstrm);
ifstrm.close();
@ -477,52 +530,31 @@ Config::prune_cmd_xattr(const std::string_view &s_)
return {};
}
std::ostream&
operator<<(std::ostream &os_,
const Config &c_)
{
for(const auto &[key,val] : c_._map)
os_ << key << '=' << val->to_string() << std::endl;
return os_;
}
static
std::string
err2str(const int err_)
Config::Err::to_string() const
{
switch(err_)
std::string s;
switch(err)
{
case 0:
return std::string();
case -EINVAL:
return "invalid value";
case -ENOATTR:
return "unknown option";
case -EROFS:
return "read-only option";
break;
case EINVAL:
s = "invalid value";
break;
case ENOATTR:
s = "unknown option";
break;
case EROFS:
s = "read-only option";
break;
case ELOOP:
s = "too many levels of config";
break;
default:
return strerror(-err_);
s = strerror(err);
break;
}
return std::string();
}
std::ostream&
operator<<(std::ostream &os_,
const Config::ErrVec &ev_)
{
std::string errstr;
for(auto &err : ev_)
{
os_ << "* ERROR: ";
errstr = err2str(err.err);
if(!errstr.empty())
os_ << errstr << " - ";
os_ << err.str << std::endl;
}
return os_;
return fmt::format("{} - {}",s,str);
}

View file

@ -19,6 +19,7 @@
#include "branches.hpp"
#include "category.hpp"
#include "config_cachefiles.hpp"
#include "config_dummy.hpp"
#include "config_flushonclose.hpp"
#include "config_follow_symlinks.hpp"
#include "config_gidcache.hpp"
@ -27,8 +28,9 @@
#include "config_log_metrics.hpp"
#include "config_moveonenospc.hpp"
#include "config_nfsopenhack.hpp"
#include "config_noforget.hpp"
#include "config_pagesize.hpp"
#include "config_passthrough.hpp"
#include "config_passthrough_io.hpp"
#include "config_pid.hpp"
#include "config_proxy_ioprio.hpp"
#include "config_rename_exdev.hpp"
@ -40,10 +42,13 @@
#include "errno.hpp"
#include "fs_path.hpp"
#include "funcs.hpp"
#include "fuse_cfg.hpp"
#include "fuse_readdir.hpp"
#include "policy.hpp"
#include "rwlock.hpp"
#include "tofrom_ref.hpp"
#include "tofrom_wrapper.hpp"
#include "syslog.hpp"
#include "fuse.h"
@ -55,13 +60,14 @@
#include <sys/stat.h>
typedef ToFromWrapper<bool> ConfigBOOL;
typedef ToFromWrapper<uint64_t> ConfigUINT64;
typedef ToFromWrapper<int64_t> ConfigS64;
typedef ToFromWrapper<int> ConfigINT;
typedef ToFromWrapper<std::string> ConfigSTR;
typedef ToFromWrapper<fs::path> ConfigPath;
typedef std::map<std::string,ToFromString*> Str2TFStrMap;
typedef ToFromWrapper<bool> ConfigBOOL;
typedef ToFromWrapper<uint64_t> ConfigUINT64;
typedef ToFromWrapper<int64_t> ConfigS64;
typedef ToFromWrapper<int> ConfigINT;
typedef ToFromWrapper<std::string> ConfigSTR;
typedef ToFromWrapper<fs::path> ConfigPath;
typedef std::map<std::string,ToFromString*> Str2TFStrMap;
typedef ROToFromWrapper<std::string> ConfigROSTR;
extern const std::string CONTROLFILE;
@ -72,10 +78,30 @@ public:
{
int err;
std::string str;
std::string to_string() const;
};
typedef std::vector<Err> ErrVec;
public:
ErrVec errs;
public:
class CfgConfigFile : public ToFromString
{
private:
fs::path _cfg_file;
int _depth = 0;
public:
CfgConfigFile();
public:
int from_string(const std::string_view s_) final;
std::string to_string(void) const final;
};
public:
Config();
@ -85,8 +111,6 @@ public:
public:
ConfigBOOL allow_idmap;
ConfigBOOL async_read;
ConfigBOOL auto_cache;
ConfigUINT64 minfreespace;
Branches branches;
ConfigUINT64 branches_mount_timeout;
ConfigBOOL branches_mount_timeout_fail;
@ -98,8 +122,11 @@ public:
ConfigBOOL cache_readdir;
ConfigUINT64 cache_statfs;
ConfigBOOL cache_symlinks;
ConfigBOOL cache_writeback;
Categories category;
ConfigBOOL direct_io;
CfgConfigFile config_file;
TFSRef<int> congestion_threshold;
TFSRef<bool> debug;
ConfigBOOL direct_io_allow_mmap;
ConfigBOOL dropcacheonclose;
ConfigBOOL export_support;
@ -108,30 +135,40 @@ public:
ConfigSTR fsname;
Funcs func;
ConfigPageSize fuse_msg_size;
TFSRef<s64> gid;
GIDCacheExpireTimeout gid_cache_expire_timeout;
GIDCacheRemoveTimeout gid_cache_remove_timeout;
ConfigBOOL handle_killpriv = true;
ConfigBOOL handle_killpriv_v2 = true;
ConfigBOOL handle_killpriv;
ConfigBOOL handle_killpriv_v2;
ConfigBOOL ignorepponrename;
InodeCalc inodecalc;
ConfigBOOL kernel_cache;
ConfigBOOL kernel_permissions_check = true;
ConfigBOOL kernel_permissions_check;
ConfigBOOL lazy_umount_mountpoint;
ConfigBOOL link_cow;
LinkEXDEV link_exdev;
LogMetrics log_metrics;
ConfigPath mountpoint;
TFSRef<int> max_background;
ConfigUINT64 minfreespace;
fs::path mountpoint;
TFSRef<fs::path> _mount;
TFSRef<fs::path> _mountpoint;
MoveOnENOSPC moveonenospc;
NFSOpenHack nfsopenhack;
CfgNoforget noforget;
ConfigBOOL nullrw;
Passthrough passthrough = Passthrough::ENUM::OFF;
ConfigBOOL parallel_direct_writes;
PassthroughIO passthrough_io;
TFSRef<int> passthrough_max_stack_depth;
ConfigGetPid pid;
TFSRef<std::string> pin_threads;
ConfigBOOL posix_acl;
TFSRef<int> process_thread_count;
TFSRef<int> process_thread_queue_depth;
ProxyIOPrio proxy_ioprio;
TFSRef<int> read_thread_count;
ConfigUINT64 readahead;
FUSE::ReadDir readdir;
ConfigBOOL readdirplus;
TFSRef<s64> remember_nodes;
RenameEXDEV rename_exdev;
ConfigINT scheduling_priority;
ConfigBOOL security_capability;
@ -140,14 +177,15 @@ public:
StatFSIgnore statfs_ignore;
ConfigBOOL symlinkify;
ConfigS64 symlinkify_timeout;
ConfigINT fuse_read_thread_count;
ConfigINT fuse_process_thread_count;
ConfigINT fuse_process_thread_queue_depth;
ConfigSTR fuse_pin_threads;
ConfigSTR version;
ConfigBOOL writeback_cache;
TFSRef<int> threads;
TFSRef<s64> uid;
TFSRef<s64> umask;
ConfigROSTR version;
XAttr xattr;
private:
CfgDummy _dummy;
private:
bool _initialized;
@ -167,13 +205,12 @@ public:
public:
int get(const std::string &key, std::string *val) const;
int set_raw(const std::string &key, const std::string &val);
int set(const std::string &key, const std::string &val);
int set(const std::string &kv);
public:
int from_stream(std::istream &istrm, ErrVec *errs);
int from_file(const std::string &filepath, ErrVec *errs);
int from_stream(std::istream &istrm);
int from_file(const std::string &filepath);
public:
static bool is_rootdir(const fs::path &fusepath);
@ -185,10 +222,6 @@ public:
private:
Str2TFStrMap _map;
public:
friend class Read;
friend class Write;
};
std::ostream& operator<<(std::ostream &s,const Config::ErrVec &ev);

View file

@ -26,8 +26,6 @@ CacheFiles::to_string() const
{
switch(_data)
{
case CacheFiles::ENUM::LIBFUSE:
return "libfuse";
case CacheFiles::ENUM::OFF:
return "off";
case CacheFiles::ENUM::PARTIAL:
@ -47,9 +45,7 @@ template<>
int
CacheFiles::from_string(const std::string_view s_)
{
if(s_ == "libfuse")
_data = CacheFiles::ENUM::LIBFUSE;
ef(s_ == "off")
if(s_ == "off")
_data = CacheFiles::ENUM::OFF;
ef(s_ == "partial")
_data = CacheFiles::ENUM::PARTIAL;

View file

@ -23,7 +23,6 @@
enum class CacheFilesEnum
{
LIBFUSE,
OFF,
PARTIAL,
FULL,

24
src/config_dummy.hpp Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include "tofrom_string.hpp"
class CfgDummy : public ToFromString
{
public:
CfgDummy()
{
display = false;
}
std::string
to_string() const
{
return {};
}
int
from_string(const std::string_view)
{
return 0;
}
};

40
src/config_noforget.hpp Normal file
View file

@ -0,0 +1,40 @@
#pragma once
#include "tofrom_string.hpp"
#include "from_string.hpp"
#include "fuse_cfg.hpp"
class CfgNoforget : public ToFromString
{
public:
int
from_string(const std::string_view s_)
{
int rv;
bool b = false;
rv = str::from(s_,&b);
if((b == true) || s_.empty())
{
fuse_cfg.remember_nodes = -1;
return 0;
}
if(rv)
return rv;
fuse_cfg.remember_nodes = 0;
return 0;
}
std::string
to_string() const
{
if(fuse_cfg.remember_nodes == -1)
return "true";
return "false";
}
};

View file

@ -16,23 +16,23 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config_passthrough.hpp"
#include "config_passthrough_io.hpp"
#include "ef.hpp"
#include "errno.hpp"
template<>
std::string
Passthrough::to_string() const
PassthroughIO::to_string() const
{
switch(_data)
{
case Passthrough::ENUM::OFF:
case PassthroughIO::ENUM::OFF:
return "off";
case Passthrough::ENUM::RO:
case PassthroughIO::ENUM::RO:
return "ro";
case Passthrough::ENUM::WO:
case PassthroughIO::ENUM::WO:
return "wo";
case Passthrough::ENUM::RW:
case PassthroughIO::ENUM::RW:
return "rw";
}
@ -41,16 +41,16 @@ Passthrough::to_string() const
template<>
int
Passthrough::from_string(const std::string_view s_)
PassthroughIO::from_string(const std::string_view s_)
{
if(s_ == "off")
_data = Passthrough::ENUM::OFF;
_data = PassthroughIO::ENUM::OFF;
ef(s_ == "ro")
_data = Passthrough::ENUM::RO;
_data = PassthroughIO::ENUM::RO;
ef(s_ == "wo")
_data = Passthrough::ENUM::WO;
_data = PassthroughIO::ENUM::WO;
ef(s_ == "rw")
_data = Passthrough::ENUM::RW;
_data = PassthroughIO::ENUM::RW;
else
return -EINVAL;

View file

@ -21,7 +21,7 @@
#include "enum.hpp"
enum class PassthroughEnum
enum class PassthroughIOEnum
{
OFF = 0,
RO,
@ -29,4 +29,4 @@ enum class PassthroughEnum
RW
};
typedef Enum<PassthroughEnum> Passthrough;
typedef Enum<PassthroughIOEnum> PassthroughIO;

View file

@ -46,7 +46,7 @@
*/
static
void
_tweak_flags_writeback_cache(int *flags_)
_tweak_flags_cache_writeback(int *flags_)
{
if((*flags_ & O_ACCMODE) == O_WRONLY)
*flags_ = ((*flags_ & ~O_ACCMODE) | O_RDWR);
@ -87,11 +87,6 @@ _config_to_ffi_flags(Config &cfg_,
{
switch(cfg_.cache_files)
{
case CacheFiles::ENUM::LIBFUSE:
ffi_->direct_io = cfg_.direct_io;
ffi_->keep_cache = cfg_.kernel_cache;
ffi_->auto_cache = cfg_.auto_cache;
break;
case CacheFiles::ENUM::OFF:
ffi_->direct_io = 1;
ffi_->keep_cache = 0;
@ -215,8 +210,8 @@ _create(const Policy::Search &searchFunc_,
constexpr
const
uint64_t
_(const PassthroughEnum e_,
const uint64_t m_)
_(const PassthroughIOEnum e_,
const uint64_t m_)
{
return ((((uint64_t)e_) << 32) | (m_ & O_ACCMODE));
}
@ -234,8 +229,8 @@ _create_for_insert_lambda(const fuse_req_ctx_t *ctx_,
const ugid::Set ugid(ctx_->uid,ctx_->gid);
::_config_to_ffi_flags(cfg,ctx_->pid,ffi_);
if(cfg.writeback_cache)
::_tweak_flags_writeback_cache(&ffi_->flags);
if(cfg.cache_writeback)
::_tweak_flags_cache_writeback(&ffi_->flags);
ffi_->noflush = !::_calculate_flush(cfg.flushonclose,
ffi_->flags);
@ -266,13 +261,13 @@ _create_for_insert_lambda(const fuse_req_ctx_t *ctx_,
of_->ref_count = 1;
of_->fi = fi;
switch(_(cfg.passthrough,ffi_->flags))
switch(_(cfg.passthrough_io,ffi_->flags))
{
case _(Passthrough::ENUM::RO,O_RDONLY):
case _(Passthrough::ENUM::WO,O_WRONLY):
case _(Passthrough::ENUM::RW,O_RDONLY):
case _(Passthrough::ENUM::RW,O_WRONLY):
case _(Passthrough::ENUM::RW,O_RDWR):
case _(PassthroughIO::ENUM::RO,O_RDONLY):
case _(PassthroughIO::ENUM::WO,O_WRONLY):
case _(PassthroughIO::ENUM::RW,O_RDONLY):
case _(PassthroughIO::ENUM::RW,O_WRONLY):
case _(PassthroughIO::ENUM::RW,O_RDWR):
break;
default:
return 0;

View file

@ -131,7 +131,7 @@ _want_if_capable_max_pages(fuse_conn_info *conn_,
if(::_capable(conn_,FUSE_CAP_MAX_PAGES))
{
::_want(conn_,FUSE_CAP_MAX_PAGES);
conn_->max_pages = cfg_.fuse_msg_size;
fuse_cfg.max_pages = cfg_.fuse_msg_size;
SysLog::info("requesting max pages size of {}",
(uint64_t)cfg_.fuse_msg_size);
}
@ -204,8 +204,8 @@ FUSE::init(fuse_conn_info *conn_)
::_want_if_capable(conn_,FUSE_CAP_PARALLEL_DIROPS);
::_want_if_capable(conn_,FUSE_CAP_PASSTHROUGH);
::_want_if_capable(conn_,FUSE_CAP_POSIX_ACL,&cfg.posix_acl);
::_want_if_capable(conn_,FUSE_CAP_READDIR_PLUS,&cfg.readdirplus);
::_want_if_capable(conn_,FUSE_CAP_WRITEBACK_CACHE,&cfg.writeback_cache);
// ::_want_if_capable(conn_,FUSE_CAP_READDIR_PLUS,&cfg.readdirplus);
::_want_if_capable(conn_,FUSE_CAP_WRITEBACK_CACHE,&cfg.cache_writeback);
::_want_if_capable(conn_,FUSE_CAP_ALLOW_IDMAP,&cfg.allow_idmap);
// ::_want_if_capable(conn_,FUSE_CAP_READDIR_PLUS_AUTO);
::_want_if_capable_max_pages(conn_,cfg);
@ -214,21 +214,22 @@ FUSE::init(fuse_conn_info *conn_)
::_spawn_thread_to_set_readahead();
if(!(conn_->capable & FUSE_CAP_PASSTHROUGH) && (cfg.passthrough != Passthrough::ENUM::OFF))
if(!(conn_->capable & FUSE_CAP_PASSTHROUGH) &&
(cfg.passthrough_io != PassthroughIO::ENUM::OFF))
{
SysLog::warning("passthrough enabled but not supported by kernel. disabling.");
cfg.passthrough = Passthrough::ENUM::OFF;
cfg.passthrough_io = PassthroughIO::ENUM::OFF;
}
if((cfg.passthrough != Passthrough::ENUM::OFF) &&
(cfg.cache_files == CacheFiles::ENUM::OFF))
if((cfg.passthrough_io != PassthroughIO::ENUM::OFF) &&
(cfg.cache_files == CacheFiles::ENUM::OFF))
{
SysLog::warning("passthrough enabled and cache.files disabled:"
" passthrough will not function");
}
if((cfg.passthrough != Passthrough::ENUM::OFF) &&
(cfg.writeback_cache == true))
if((cfg.passthrough_io != PassthroughIO::ENUM::OFF) &&
(cfg.cache_writeback == true))
{
SysLog::warning("passthrough and cache.writeback are incompatible.");
}

View file

@ -111,7 +111,7 @@ _nfsopenhack(const fs::path &fullpath_,
*/
static
void
_tweak_flags_writeback_cache(int *flags_)
_tweak_flags_cache_writeback(int *flags_)
{
if((*flags_ & O_ACCMODE) == O_WRONLY)
*flags_ = ((*flags_ & ~O_ACCMODE) | O_RDWR);
@ -145,11 +145,6 @@ _config_to_ffi_flags(Config &cfg_,
{
switch(cfg.cache_files)
{
case CacheFiles::ENUM::LIBFUSE:
ffi_->direct_io = cfg.direct_io;
ffi_->keep_cache = cfg.kernel_cache;
ffi_->auto_cache = cfg.auto_cache;
break;
case CacheFiles::ENUM::OFF:
ffi_->direct_io = 1;
ffi_->keep_cache = 0;
@ -272,8 +267,8 @@ _open(const Policy::Search &searchFunc_,
constexpr
const
uint64_t
_(const PassthroughEnum e_,
const uint64_t m_)
_(const PassthroughIOEnum e_,
const uint64_t m_)
{
return ((((uint64_t)e_) << 32) | (m_ & O_ACCMODE));
}
@ -291,8 +286,8 @@ _open_for_insert_lambda(const fuse_req_ctx_t *ctx_,
::_config_to_ffi_flags(cfg,ctx_->pid,ffi_);
if(cfg.writeback_cache)
::_tweak_flags_writeback_cache(&ffi_->flags);
if(cfg.cache_writeback)
::_tweak_flags_cache_writeback(&ffi_->flags);
ffi_->noflush = !::_calculate_flush(cfg.flushonclose,
ffi_->flags);
@ -312,13 +307,13 @@ _open_for_insert_lambda(const fuse_req_ctx_t *ctx_,
of_->ref_count = 1;
of_->fi = fi;
switch(_(cfg.passthrough,ffi_->flags))
switch(_(cfg.passthrough_io,ffi_->flags))
{
case _(Passthrough::ENUM::RO,O_RDONLY):
case _(Passthrough::ENUM::WO,O_WRONLY):
case _(Passthrough::ENUM::RW,O_RDONLY):
case _(Passthrough::ENUM::RW,O_WRONLY):
case _(Passthrough::ENUM::RW,O_RDWR):
case _(PassthroughIO::ENUM::RO,O_RDONLY):
case _(PassthroughIO::ENUM::WO,O_WRONLY):
case _(PassthroughIO::ENUM::RW,O_RDONLY):
case _(PassthroughIO::ENUM::RW,O_WRONLY):
case _(PassthroughIO::ENUM::RW,O_RDWR):
break;
default:
return 0;
@ -347,8 +342,8 @@ _open_for_update_lambda(const fuse_req_ctx_t *ctx_,
::_config_to_ffi_flags(cfg,ctx_->pid,ffi_);
if(cfg.writeback_cache)
::_tweak_flags_writeback_cache(&ffi_->flags);
if(cfg.cache_writeback)
::_tweak_flags_cache_writeback(&ffi_->flags);
ffi_->noflush = !::_calculate_flush(cfg.flushonclose,
ffi_->flags);

View file

@ -1,13 +0,0 @@
#pragma once
#include <stdint.h>
typedef uint64_t u32;
typedef int64_t s32;
typedef uint64_t u64;
typedef int64_t s64;
typedef const uint64_t cu32;
typedef const int64_t cs32;
typedef const uint64_t cu64;
typedef const int64_t cs64;

View file

@ -18,6 +18,7 @@
#include "mergerfs_fsck.hpp"
#include "mergerfs_collect_info.hpp"
#include "config.hpp"
#include "fs_path.hpp"
#include "fs_readahead.hpp"
#include "fs_umount2.hpp"
@ -89,272 +90,279 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <unordered_set>
namespace l
static
void
_get_fuse_operations(struct fuse_operations &ops_,
const bool nullrw_)
{
static
void
get_fuse_operations(struct fuse_operations &ops_,
const bool nullrw_)
{
ops_.access = FUSE::access;
ops_.bmap = FUSE::bmap;
ops_.chmod = FUSE::chmod;
ops_.chown = FUSE::chown;
ops_.copy_file_range = FUSE::copy_file_range;
ops_.create = FUSE::create;
ops_.destroy = FUSE::destroy;
ops_.fallocate = FUSE::fallocate;
ops_.fchmod = FUSE::fchmod;
ops_.fchown = FUSE::fchown;
ops_.fgetattr = FUSE::fgetattr;
ops_.flock = FUSE::flock;
ops_.flush = FUSE::flush;
ops_.fsync = FUSE::fsync;
ops_.fsyncdir = FUSE::fsyncdir;
ops_.ftruncate = FUSE::ftruncate;
ops_.futimens = FUSE::futimens;
ops_.getattr = FUSE::getattr;
ops_.getxattr = FUSE::getxattr;
ops_.init = FUSE::init;
ops_.ioctl = FUSE::ioctl;
ops_.link = FUSE::link;
ops_.listxattr = FUSE::listxattr;
ops_.lock = FUSE::lock;
ops_.mkdir = FUSE::mkdir;
ops_.mknod = FUSE::mknod;
ops_.open = FUSE::open;
ops_.opendir = FUSE::opendir;
ops_.poll = FUSE::poll;;
ops_.read = (nullrw_ ? FUSE::read_null : FUSE::read);
ops_.readdir = FUSE::readdir;
ops_.readdir_plus = FUSE::readdir_plus;
ops_.readlink = FUSE::readlink;
ops_.release = FUSE::release;
ops_.releasedir = FUSE::releasedir;
ops_.removemapping = FUSE::removemapping;
ops_.removexattr = FUSE::removexattr;
ops_.rename = FUSE::rename;
ops_.rmdir = FUSE::rmdir;
ops_.setupmapping = FUSE::setupmapping;
ops_.setxattr = FUSE::setxattr;
ops_.statfs = FUSE::statfs;
ops_.statx = FUSE::statx;
ops_.statx_fh = FUSE::statx_fh;
ops_.symlink = FUSE::symlink;
ops_.syncfs = FUSE::syncfs;
ops_.tmpfile = FUSE::tmpfile;
ops_.truncate = FUSE::truncate;
ops_.unlink = FUSE::unlink;
ops_.utimens = FUSE::utimens;
ops_.write = (nullrw_ ? FUSE::write_null : FUSE::write);
ops_.access = FUSE::access;
ops_.bmap = FUSE::bmap;
ops_.chmod = FUSE::chmod;
ops_.chown = FUSE::chown;
ops_.copy_file_range = FUSE::copy_file_range;
ops_.create = FUSE::create;
ops_.destroy = FUSE::destroy;
ops_.fallocate = FUSE::fallocate;
ops_.fchmod = FUSE::fchmod;
ops_.fchown = FUSE::fchown;
ops_.fgetattr = FUSE::fgetattr;
ops_.flock = FUSE::flock;
ops_.flush = FUSE::flush;
ops_.fsync = FUSE::fsync;
ops_.fsyncdir = FUSE::fsyncdir;
ops_.ftruncate = FUSE::ftruncate;
ops_.futimens = FUSE::futimens;
ops_.getattr = FUSE::getattr;
ops_.getxattr = FUSE::getxattr;
ops_.init = FUSE::init;
ops_.ioctl = FUSE::ioctl;
ops_.link = FUSE::link;
ops_.listxattr = FUSE::listxattr;
ops_.lock = FUSE::lock;
ops_.mkdir = FUSE::mkdir;
ops_.mknod = FUSE::mknod;
ops_.open = FUSE::open;
ops_.opendir = FUSE::opendir;
ops_.poll = FUSE::poll;;
ops_.read = (nullrw_ ? FUSE::read_null : FUSE::read);
ops_.readdir = FUSE::readdir;
ops_.readdir_plus = FUSE::readdir_plus;
ops_.readlink = FUSE::readlink;
ops_.release = FUSE::release;
ops_.releasedir = FUSE::releasedir;
ops_.removemapping = FUSE::removemapping;
ops_.removexattr = FUSE::removexattr;
ops_.rename = FUSE::rename;
ops_.rmdir = FUSE::rmdir;
ops_.setupmapping = FUSE::setupmapping;
ops_.setxattr = FUSE::setxattr;
ops_.statfs = FUSE::statfs;
ops_.statx = FUSE::statx;
ops_.statx_fh = FUSE::statx_fh;
ops_.symlink = FUSE::symlink;
ops_.syncfs = FUSE::syncfs;
ops_.tmpfile = FUSE::tmpfile;
ops_.truncate = FUSE::truncate;
ops_.unlink = FUSE::unlink;
ops_.utimens = FUSE::utimens;
ops_.write = (nullrw_ ? FUSE::write_null : FUSE::write);
return;
}
static
void
_setup_resources(const int scheduling_priority_)
{
std::srand(time(NULL));
resources::reset_umask();
resources::maxout_rlimit_nofile();
resources::maxout_rlimit_fsize();
resources::setpriority(scheduling_priority_);
}
static
void
_set_oom_score_adj()
{
int rv;
int orig;
int score;
if(!oom::has_oom_score_adj())
return;
}
static
void
setup_resources(const int scheduling_priority_)
{
std::srand(time(NULL));
resources::reset_umask();
resources::maxout_rlimit_nofile();
resources::maxout_rlimit_fsize();
resources::setpriority(scheduling_priority_);
}
score = -990;
static
void
set_oom_score_adj()
{
int rv;
int orig;
int score;
orig = oom::get_oom_score_adj();
rv = oom::set_oom_score_adj(score);
if(!oom::has_oom_score_adj())
return;
(void)rv;
SysLog::info("set oom_score_adj to {}, originally {}",
score,
orig);
}
score = -990;
static
bool
_wait_for_mount()
{
int failures;
std::vector<fs::path> paths;
std::chrono::milliseconds timeout;
orig = oom::get_oom_score_adj();
rv = oom::set_oom_score_adj(score);
paths = cfg.branches->to_paths();
(void)rv;
SysLog::info("set oom_score_adj to {}, originally {}",
score,
orig);
}
SysLog::info("Waiting {} seconds for {} branches to mount",
(uint64_t)cfg.branches_mount_timeout,
paths.size());
static
bool
wait_for_mount(const Config &cfg_)
{
int failures;
std::vector<fs::path> paths;
std::chrono::milliseconds timeout;
paths = cfg_.branches->to_paths();
SysLog::info("Waiting {} seconds for {} branches to mount",
(uint64_t)cfg_.branches_mount_timeout,
paths.size());
timeout = std::chrono::milliseconds(cfg_.branches_mount_timeout * 1000);
failures = fs::wait_for_mount(cfg_.mountpoint,
paths,
timeout);
if(failures)
{
if(cfg_.branches_mount_timeout_fail)
{
SysLog::error("{} of {} branches were not mounted"
" within the timeout of {}s. Exiting",
failures,
paths.size(),
(uint64_t)cfg_.branches_mount_timeout);
return true;
}
SysLog::warning("Continuing to mount mergerfs despite {} branches not "
"being different from the mountpoint filesystem",
failures);
}
else
{
SysLog::info("All {} branches are mounted",
paths.size());
}
return false;
}
static
void
lazy_umount(const fs::path &target_)
{
int rv;
rv = fs::umount_lazy(target_);
switch(rv)
{
case 0:
SysLog::notice("{} has been successfully lazily unmounted",
target_.string());
break;
case -EINVAL:
SysLog::notice("{} was not a mount point needing to be unmounted",
target_.string());
break;
default:
SysLog::error("Error unmounting {}: {} - {}",
target_.string(),
-rv,
strerror(-rv));
break;
}
}
static
void
usr1_signal_handler(int signal_)
{
// SysLog::info("Received SIGUSR1 - invalidating all nodes");
// fuse_invalidate_all_nodes();
}
static
void
usr2_signal_handler(int signal_)
{
// SysLog::info("Received SIGUSR2 - triggering thorough gc");
// fuse_gc();
// GIDCache::clear_all();
}
static
void
setup_signal_handlers()
{
std::signal(SIGUSR1,l::usr1_signal_handler);
std::signal(SIGUSR2,l::usr2_signal_handler);
}
static
void
warn_if_not_root()
{
uid_t uid;
uid = geteuid();
if(uid == 0)
return;
constexpr const char s[] = "mergerfs is not running as root and may not work correctly\n";
fmt::print(stderr,"warning: {}",s);
SysLog::warning(s);
}
int
main(int argc_,
char **argv_)
{
int rv;
Config::ErrVec errs;
fuse_args args;
fuse_operations ops;
SysLog::open();
memset(&ops,0,sizeof(fuse_operations));
args.argc = argc_;
args.argv = argv_;
args.allocated = 0;
options::parse(&args,&errs);
if(errs.size())
{
std::cerr << errs << std::endl;
return 1;
}
if(cfg.branches_mount_timeout > 0)
{
bool failure;
failure = l::wait_for_mount(cfg);
if(failure)
return 1;
}
SysLog::info("mergerfs v{} started",MERGERFS_VERSION);
SysLog::info("Go to https://trapexit.github.io/mergerfs/support for support");
l::warn_if_not_root();
MaintenanceThread::push_job([](int count_)
timeout = std::chrono::milliseconds(cfg.branches_mount_timeout * 1000);
failures = fs::wait_for_mount(cfg.mountpoint,
paths,
timeout);
if(failures)
{
if((count_ % 60) == 0)
GIDCache::clear_unused();
});
l::setup_resources(cfg.scheduling_priority);
l::setup_signal_handlers();
l::set_oom_score_adj();
l::get_fuse_operations(ops,cfg.nullrw);
if(cfg.branches_mount_timeout_fail)
{
SysLog::error("{} of {} branches were not mounted"
" within the timeout of {}s. Exiting",
failures,
paths.size(),
(uint64_t)cfg.branches_mount_timeout);
return true;
}
if(cfg.lazy_umount_mountpoint)
l::lazy_umount(cfg.mountpoint);
SysLog::warning("Continuing to mount mergerfs despite {} branches not "
"being different from the mountpoint filesystem",
failures);
}
else
{
SysLog::info("All {} branches are mounted",
paths.size());
}
rv = fuse_main(args.argc,
args.argv,
&ops);
return false;
}
SysLog::info("exiting main loop with return code {}",rv);
static
void
_lazy_umount(const fs::path &target_)
{
int rv;
SysLog::close();
rv = fs::umount_lazy(target_);
switch(rv)
{
case 0:
SysLog::notice("{} has been successfully lazily unmounted",
target_.string());
break;
case -EINVAL:
SysLog::notice("{} was not a mount point needing to be unmounted",
target_.string());
break;
default:
SysLog::error("Error unmounting {}: {} - {}",
target_.string(),
-rv,
strerror(-rv));
break;
}
}
return rv;
}
static
void
_usr1_signal_handler(int signal_)
{
// SysLog::info("Received SIGUSR1 - invalidating all nodes");
// fuse_invalidate_all_nodes();
}
static
void
_usr2_signal_handler(int signal_)
{
// SysLog::info("Received SIGUSR2 - triggering thorough gc");
// fuse_gc();
// GIDCache::clear_all();
}
static
void
_setup_signal_handlers()
{
std::signal(SIGUSR1,::_usr1_signal_handler);
std::signal(SIGUSR2,::_usr2_signal_handler);
}
static
void
_warn_if_not_root()
{
uid_t uid;
uid = geteuid();
if(uid == 0)
return;
constexpr const char s[] =
"mergerfs is not running as root"
" and may not work correctly";
fmt::println(stderr,"* WARNING: {}",s);
SysLog::warning(s);
}
int
_main(int argc_,
char **argv_)
{
int rv;
fuse_args args;
fuse_operations ops;
SysLog::open();
memset(&ops,0,sizeof(fuse_operations));
args.argc = argc_;
args.argv = argv_;
args.allocated = false;
SysLog::info("mergerfs v{} started",MERGERFS_VERSION);
SysLog::info("Go to https://trapexit.github.io/mergerfs/support for support");
options::parse(&args);
if(!cfg.errs.empty())
{
for(auto &err : cfg.errs)
{
std::string s = err.to_string();
SysLog::error("error: {}",s);
fmt::println(stderr,"* ERROR: {}",s);
}
return 1;
}
if(cfg.branches_mount_timeout > 0)
{
bool failure;
failure = ::_wait_for_mount();
if(failure)
return 1;
}
::_warn_if_not_root();
MaintenanceThread::push_job([](int count_)
{
if((count_ % 60) == 0)
GIDCache::clear_unused();
});
::_setup_resources(cfg.scheduling_priority);
::_setup_signal_handlers();
::_set_oom_score_adj();
::_get_fuse_operations(ops,cfg.nullrw);
if(cfg.lazy_umount_mountpoint)
::_lazy_umount(cfg.mountpoint);
rv = fuse_main(args.argc,
args.argv,
&ops);
SysLog::info("exiting main loop with return code {}",rv);
SysLog::close();
return rv;
}
static
@ -372,7 +380,7 @@ _pick_app_and_run(int argc_,
if(appname == "mergerfs.collect-info")
return mergerfs::collect_info::main(argc_,argv_);
return l::main(argc_,argv_);
return ::_main(argc_,argv_);
}
int

View file

@ -31,7 +31,6 @@
#include "version.hpp"
#include "fuse.h"
#include "fuse_config.hpp"
#include <array>
#include <fstream>
@ -49,10 +48,16 @@
enum
{
MERGERFS_OPT_DEBUG,
MERGERFS_OPT_HELP,
MERGERFS_OPT_VERSION
};
enum
{
OPT_DISCARD = 0,
OPT_KEEP = 1
};
static
void
@ -79,30 +84,19 @@ _set_kv_option(const std::string &key_,
static
void
_set_fuse_threads(Config &cfg_)
_set_fsname(fuse_args *args_)
{
fuse_config_set_read_thread_count(cfg_.fuse_read_thread_count);
fuse_config_set_process_thread_count(cfg_.fuse_process_thread_count);
fuse_config_set_process_thread_queue_depth(cfg_.fuse_process_thread_queue_depth);
fuse_config_set_pin_threads(cfg_.fuse_pin_threads);
}
static
void
_set_fsname(Config &cfg_,
fuse_args *args_)
{
if(cfg_.fsname->empty())
if(cfg.fsname->empty())
{
std::vector<std::string> paths;
cfg_.branches->to_paths(paths);
cfg.branches->to_paths(paths);
if(paths.size() > 0)
cfg_.fsname = str::remove_common_prefix_and_join(paths,':');
cfg.fsname = str::remove_common_prefix_and_join(paths,':');
}
::_set_kv_option("fsname",cfg_.fsname,args_);
::_set_kv_option("fsname",cfg.fsname,args_);
}
static
@ -114,10 +108,9 @@ _set_subtype(fuse_args *args_)
static
void
_set_default_options(fuse_args *args_,
Config &cfg_)
_set_default_options(fuse_args *args_)
{
if(cfg_.kernel_permissions_check)
if(cfg.kernel_permissions_check)
::_set_option("default_permissions",args_);
if(geteuid() == 0)
@ -126,84 +119,28 @@ _set_default_options(fuse_args *args_,
SysLog::notice("not auto setting allow_other since not running as root");
}
static
bool
_should_ignore(const std::string &key_)
{
constexpr const std::array<std::string_view,13> ignored_keys =
{
"atomic_o_trunc",
"big_writes",
"cache.open",
"defaults",
"hard_remove",
"no_splice_move",
"no_splice_read",
"no_splice_write",
"nonempty",
"splice_move",
"splice_read",
"splice_write",
"use_ino",
};
for(const auto &key : ignored_keys)
{
if(key == key_)
return true;
}
return false;
}
static
int
_parse_and_process_kv_arg(Config &cfg_,
Config::ErrVec *errs_,
const std::string &key_,
_parse_and_process_kv_arg(const std::string &key_,
const std::string &val_)
{
int rv;
std::string key(key_);
std::string val(val_);
rv = 0;
if(key == "config")
return ((cfg_.from_file(val_,errs_) < 0) ? 1 : 0);
ef(key == "attr_timeout")
key = "cache.attr";
ef(key == "entry_timeout")
key = "cache.entry";
ef(key == "negative_entry")
key = "cache.negative_entry";
ef(key == "direct_io" && val.empty())
val = "true";
ef(key == "kernel_cache" && val.empty())
val = "true";
ef(key == "auto_cache" && val.empty())
val = "true";
ef(key == "async_read" && val.empty())
val = "true";
ef(key == "sync_read" && val.empty())
{key = "async_read", val = "false";}
ef(::_should_ignore(key_))
return 0;
if(cfg_.has_key(key) == false)
if(cfg.has_key(key) == false)
return 1;
rv = cfg_.set_raw(key,val);
if(rv)
errs_->push_back({rv,key+'='+val});
rv = cfg.set(key,val);
if(rv < 0)
cfg.errs.push_back({-rv,key+'='+val});
return 0;
return OPT_DISCARD;
}
static
int
_process_opt(Config &cfg_,
Config::ErrVec *errs_,
const std::string &arg_)
_process_opt(const std::string &arg_)
{
std::string key;
std::string val;
@ -212,68 +149,7 @@ _process_opt(Config &cfg_,
key = str::trim(key);
val = str::trim(val);
return ::_parse_and_process_kv_arg(cfg_,errs_,key,val);
}
static
int
_process_branches(Config &cfg_,
Config::ErrVec *errs_,
const char *arg_)
{
int rv;
std::string arg;
arg = arg_;
rv = cfg_.set_raw("branches",arg);
if(rv)
errs_->push_back({rv,"branches="+arg});
return 0;
}
static
int
_process_mount(Config &cfg_,
Config::ErrVec *errs_,
const char *arg_)
{
int rv;
std::string arg;
arg = arg_;
rv = cfg_.set_raw("mount",arg);
if(rv)
errs_->push_back({rv,"mount="+arg});
return 1;
}
static
void
_postprocess_passthrough(Config &cfg_)
{
if(cfg_.passthrough == Passthrough::ENUM::OFF)
return;
if(cfg_.cache_files == CacheFiles::ENUM::OFF)
{
SysLog::warning("'cache.files' can not be 'off' when using 'passthrough'."
" Setting 'cache.files=full'");
cfg_.cache_files = CacheFiles::ENUM::FULL;
}
if(cfg_.writeback_cache == true)
{
SysLog::warning("'cache.writeback' can not be enabled when using 'passthrough'."
" Setting 'cache.writeback=false'");
cfg_.writeback_cache = false;
}
if(cfg_.moveonenospc.enabled == true)
{
SysLog::warning("`moveonenospc` will not function when `passthrough` is enabled");
}
return ::_parse_and_process_kv_arg(key,val);
}
static
@ -285,6 +161,36 @@ _usage(void)
"Visit https://trapexit.github.io/mergerfs for options.\n\n");
}
static
void
_version(void)
{
fmt::print("mergerfs v{}\n\n"
"https://github.com/trapexit/mergerfs\n"
"https://trapexit.github.io/mergerfs\n"
"https://github.com/trapexit/support\n"
"\n"
"ISC License (ISC)\n"
"\n"
"Copyright 2025, Antonio SJ Musumeci <trapexit@spawn.link>\n"
"\n"
"Permission to use, copy, modify, and/or distribute this software for\n"
"any purpose with or without fee is hereby granted, provided that the\n"
"above copyright notice and this permission notice appear in all\n"
"copies.\n"
"\n"
"THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL\n"
"WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED\n"
"WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE\n"
"AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\n"
"DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\n"
"PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n"
"TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n"
"PERFORMANCE OF THIS SOFTWARE.\n\n"
,
(MERGERFS_VERSION[0] ? MERGERFS_VERSION : "unknown"));
}
static
int
_option_processor(void *data_,
@ -292,44 +198,38 @@ _option_processor(void *data_,
int key_,
fuse_args *outargs_)
{
Config::ErrVec *errs = (Config::ErrVec*)data_;
std::string &prev_nonopt =
*reinterpret_cast<std::string*>(data_);
switch(key_)
{
case FUSE_OPT_KEY_OPT:
return ::_process_opt(cfg,errs,arg_);
return ::_process_opt(arg_);
case FUSE_OPT_KEY_NONOPT:
if(cfg.branches->empty())
return ::_process_branches(cfg,errs,arg_);
else
return ::_process_mount(cfg,errs,arg_);
{
if(prev_nonopt.empty())
{
prev_nonopt = arg_;
}
else
{
cfg.branches.from_string("+>" + prev_nonopt);
prev_nonopt = arg_;
}
}
return OPT_DISCARD;
case MERGERFS_OPT_DEBUG:
fuse_cfg.debug = true;
return OPT_DISCARD;
case MERGERFS_OPT_HELP:
::_usage();
exit(0);
case MERGERFS_OPT_VERSION:
fmt::print("mergerfs v{}\n\n"
"https://github.com/trapexit/mergerfs\n"
"https://trapexit.github.io/mergerfs\n"
"https://github.com/trapexit/support\n\n"
"ISC License (ISC)\n\n"
"Copyright 2025, Antonio SJ Musumeci <trapexit@spawn.link>\n\n"
"Permission to use, copy, modify, and/or distribute this software for\n"
"any purpose with or without fee is hereby granted, provided that the\n"
"above copyright notice and this permission notice appear in all\n"
"copies.\n\n"
"THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL\n"
"WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED\n"
"WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE\n"
"AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\n"
"DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\n"
"PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n"
"TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n"
"PERFORMANCE OF THIS SOFTWARE.\n\n"
,
(MERGERFS_VERSION[0] ? MERGERFS_VERSION : "unknown"));
::_version();
exit(0);
default:
@ -341,15 +241,14 @@ _option_processor(void *data_,
static
void
_check_for_mount_loop(Config &cfg_,
Config::ErrVec *errs_)
_check_for_mount_loop()
{
fs::path mount;
std::vector<fs::path> branches;
std::error_code ec;
mount = *cfg_.mountpoint;
branches = cfg_.branches->to_paths();
mount = cfg.mountpoint;
branches = cfg.branches->to_paths();
for(const auto &branch : branches)
{
if(std::filesystem::equivalent(branch,mount,ec))
@ -358,54 +257,66 @@ _check_for_mount_loop(Config &cfg_,
errstr = fmt::format("branches can not include the mountpoint: {}",
branch.string());
errs_->push_back({0,errstr});
cfg.errs.push_back({0,errstr});
}
}
}
static
void
_print_warnings(Config &cfg_)
_postprocess_and_print_warnings()
{
if(cfg_.passthrough != Passthrough::ENUM::OFF)
if(fuse_cfg.uid != FUSE_CFG_INVALID_ID)
SysLog::warning("overwriting 'uid' is untested and unsupported,"
" use at your own risk");
if(fuse_cfg.gid != FUSE_CFG_INVALID_ID)
SysLog::warning("overwriting 'gid' is untested and unsupported,"
" use at your own risk");
if(fuse_cfg.umask != FUSE_CFG_INVALID_UMASK)
SysLog::warning("overwriting 'umask' is untested and unsupported,"
" use at your own risk");
if(cfg.passthrough_io != PassthroughIO::ENUM::OFF)
{
if(cfg_.cache_files == CacheFiles::ENUM::OFF)
if(cfg.cache_files == CacheFiles::ENUM::OFF)
{
SysLog::warning("'cache.files' can not be 'off' when using 'passthrough'."
" Setting 'cache.files=auto-full'");
cfg_.cache_files = CacheFiles::ENUM::AUTO_FULL;
cfg.cache_files = CacheFiles::ENUM::AUTO_FULL;
}
if(cfg_.writeback_cache == true)
if(cfg.cache_writeback == true)
{
SysLog::warning("'cache.writeback' can not be enabled when using 'passthrough'."
" Setting 'cache.writeback=false'");
cfg_.writeback_cache = false;
cfg.cache_writeback = false;
}
if(cfg_.moveonenospc.enabled == true)
if(cfg.moveonenospc.enabled == true)
{
SysLog::warning("`moveonenospc` will not function when `passthrough` is enabled");
SysLog::warning("'moveonenospc' will not function when 'passthrough' is enabled");
}
}
}
static
void
_cleanup_options(Config &cfg_)
_cleanup_options()
{
if(!cfg_.symlinkify)
cfg_.symlinkify_timeout = -1;
if(!cfg.symlinkify)
cfg.symlinkify_timeout = -1;
}
namespace options
{
void
parse(fuse_args *args_,
Config::ErrVec *errs_)
parse(fuse_args *args_)
{
std::string prev_nonopt;
const struct fuse_opt opts[] =
{
FUSE_OPT_KEY("-d",MERGERFS_OPT_DEBUG),
FUSE_OPT_KEY("-h",MERGERFS_OPT_HELP),
FUSE_OPT_KEY("--help",MERGERFS_OPT_HELP),
FUSE_OPT_KEY("-v",MERGERFS_OPT_VERSION),
@ -415,23 +326,34 @@ namespace options
};
fuse_opt_parse(args_,
errs_,
&prev_nonopt,
opts,
::_option_processor);
if(cfg.branches->empty())
errs_->push_back({0,"branches not set"});
if(cfg.mountpoint->empty())
errs_->push_back({0,"mountpoint not set"});
if(!prev_nonopt.empty())
{
if(cfg.mountpoint.empty())
cfg.mountpoint = prev_nonopt;
else
cfg.branches.from_string("+>" + prev_nonopt);
}
::_postprocess_passthrough(cfg);
::_check_for_mount_loop(cfg,errs_);
::_set_default_options(args_,cfg);
::_set_fsname(cfg,args_);
if(cfg.branches->empty())
cfg.errs.push_back({EINVAL,"branches not set"});
if(cfg.mountpoint.empty())
cfg.errs.push_back({EINVAL,"mountpoint not set"});
if(!cfg.errs.empty())
return;
fuse_opt_add_arg(args_,cfg.mountpoint.c_str());
::_postprocess_and_print_warnings();
::_check_for_mount_loop();
::_set_default_options(args_);
::_set_fsname(args_);
::_set_subtype(args_);
::_set_fuse_threads(cfg);
::_print_warnings(cfg);
::_cleanup_options(cfg);
::_cleanup_options();
cfg.finish_initializing();
}

View file

@ -16,14 +16,11 @@
#pragma once
#include "config.hpp"
#include "fuse.h"
namespace options
{
void
parse(fuse_args *args,
Config::ErrVec *errs);
parse(fuse_args *args);
}

View file

@ -16,6 +16,7 @@
#include "str.hpp"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <set>
@ -331,3 +332,18 @@ str::startswith(const char *s_,
return true;
}
std::string
str::replace(const std::string &s_,
const char src_,
const char dst_)
{
std::string s(s_);
std::replace(s.begin(),
s.end(),
src_,
dst_);
return s;
}

View file

@ -101,4 +101,9 @@ namespace str
bool
eq(const char *s0,
const char *s1);
std::string
replace(const std::string &s,
const char src,
const char dst);
}

39
src/tofrom_ref.hpp Normal file
View file

@ -0,0 +1,39 @@
#pragma once
#include "to_string.hpp"
#include "from_string.hpp"
#include "tofrom_string.hpp"
template<typename T>
class TFSRef : public ToFromString
{
public:
int
from_string(const std::string_view s_) final
{
return str::from(s_,&_data);
}
std::string
to_string(void) const final
{
return str::to(_data);
}
public:
TFSRef(T &data_)
: _data(data_)
{
}
TFSRef(T &data_,
const T val_)
: _data(data_)
{
_data = val_;
}
private:
T &_data;
};

View file

@ -26,4 +26,6 @@ class ToFromString
public:
virtual std::string to_string() const = 0;
virtual int from_string(const std::string_view) = 0;
bool display = true;
bool ro = false;
};

View file

@ -102,7 +102,7 @@ public:
int
from_string(const std::string_view s_) final
{
return -EINVAL;
return -EROFS;
}
std::string