fix: make enum, minfreespace and process-name options safe to change

- Options set through the .mergerfs xattr are written while request
  threads read them, with no synchronization. That is a data race; on
  32-bit targets a request could see half of a new 64-bit minfreespace.
- Store the default minfreespace as std::atomic<u64> with relaxed
  loads and stores. Per-branch values are unchanged.
- Store every enum option (rename-exdev, xattr, cache.files, statfs,
  nfsopenhack, follow-symlinks and the rest) atomically. Copies take a
  snapshot.
- getxattr, setxattr and removexattr read the xattr option once per
  request instead of twice.
- cache.files.process-names was cleared and refilled in place while
  open and create looked names up in it, which could walk freed tree
  nodes. Build the new set aside and swap it in under a lock that
  lookups share.
- Other options (booleans, numbers and strings) are still plain values;
  this covers only the ones above.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:42:19 -05:00
parent e4e9d54fe7
commit 8cc6be6ff2
16 changed files with 118 additions and 45 deletions

View file

@ -31,7 +31,7 @@ Branch::Branch(const Branch &branch_)
{
}
Branch::Branch(const u64 &default_minfreespace_)
Branch::Branch(const std::atomic<u64> &default_minfreespace_)
: _minfreespace(&default_minfreespace_)
{
}
@ -75,8 +75,8 @@ Branch::set_minfreespace(const u64 minfreespace_)
u64
Branch::minfreespace(void) const
{
if(std::holds_alternative<const u64*>(_minfreespace))
return *std::get<const u64*>(_minfreespace);
if(std::holds_alternative<const std::atomic<u64>*>(_minfreespace))
return std::get<const std::atomic<u64>*>(_minfreespace)->load(std::memory_order_relaxed);
return std::get<u64>(_minfreespace);
}

View file

@ -22,6 +22,7 @@
#include "strvec.hpp"
#include "fs_path.hpp"
#include <atomic>
#include <cstdint>
#include <memory>
#include <optional>
@ -41,14 +42,16 @@ public:
};
public:
std::variant<u64,const u64*> _minfreespace;
// A default held by pointer can be changed at runtime while
// requests read it.
std::variant<u64,const std::atomic<u64>*> _minfreespace;
Mode mode;
fs::path path;
public:
Branch();
Branch(const Branch&);
Branch(const u64 &default_minfreespace);
Branch(const std::atomic<u64> &default_minfreespace);
public:
Branch& operator=(const Branch&) = default;

View file

@ -36,7 +36,7 @@
#include <fnmatch.h>
Branches::Impl::Impl(const u64 *default_minfreespace_)
Branches::Impl::Impl(const std::atomic<u64> *default_minfreespace_)
: _default_minfreespace(default_minfreespace_)
{
}
@ -56,7 +56,7 @@ Branches::Impl::operator=(Branches::Impl &rval_)
// Impl's _default_minfreespace.
for(auto &branch : *this)
{
if(std::holds_alternative<const u64*>(branch._minfreespace))
if(std::holds_alternative<const std::atomic<u64>*>(branch._minfreespace))
branch._minfreespace = _default_minfreespace;
}
@ -78,7 +78,7 @@ Branches::Impl::operator=(Branches::Impl &&rval_)
// Impl's _default_minfreespace.
for(auto &branch : *this)
{
if(std::holds_alternative<const u64*>(branch._minfreespace))
if(std::holds_alternative<const std::atomic<u64>*>(branch._minfreespace))
branch._minfreespace = _default_minfreespace;
}
@ -86,7 +86,7 @@ Branches::Impl::operator=(Branches::Impl &&rval_)
}
const
u64&
std::atomic<u64>&
Branches::Impl::minfreespace(void) const
{
return *_default_minfreespace;

View file

@ -23,6 +23,7 @@
#include "strvec.hpp"
#include "tofrom_string.hpp"
#include <atomic>
#include <cstdint>
#include <memory>
#include <shared_mutex>
@ -39,20 +40,20 @@ public:
public std::vector<Branch>
{
private:
const u64 *_default_minfreespace;
const std::atomic<u64> *_default_minfreespace;
public:
using Ptr = std::shared_ptr<Impl>;
public:
Impl(const u64 *default_minfreespace);
Impl(const std::atomic<u64> *default_minfreespace);
public:
int from_string(const std::string_view str) final;
std::string to_string(void) const final;
public:
const u64 &minfreespace(void) const;
const std::atomic<u64> &minfreespace(void) const;
void to_paths(StrVec &strvec) const;
std::vector<fs::path> to_paths() const;
@ -66,7 +67,8 @@ public:
using Ptr = Branches::Impl::Ptr;
public:
u64 minfreespace = MINFREESPACE_DEFAULT;
// Set by runtime config changes while requests read it.
std::atomic<u64> minfreespace{MINFREESPACE_DEFAULT};
private:
Branches::Ptr _impl;

View file

@ -140,7 +140,7 @@ public:
ConfigBOOL link_cow;
LinkEXDEV link_exdev;
LogFile log_file;
TFSRef<u64> minfreespace;
TFSRef<std::atomic<u64>> minfreespace;
fs::path mountpoint;
MoveOnENOSPC moveonenospc;
NFSOpenHack nfsopenhack;

View file

@ -20,6 +20,8 @@
#include "str.hpp"
#include <mutex>
ConfigSet::ConfigSet(const std::string &str_)
{
@ -29,16 +31,26 @@ ConfigSet::ConfigSet(const std::string &str_)
std::string
ConfigSet::to_string(void) const
{
return str::join(*this,'|');
std::shared_lock<std::shared_mutex> lk(_mutex);
return str::join(_set,'|');
}
int
ConfigSet::from_string(const std::string_view str_)
{
this->clear();
auto tmp = str::split_to_set(str_,'|');
this->insert(tmp.begin(), tmp.end());
std::unique_lock<std::shared_mutex> lk(_mutex);
_set.swap(tmp);
return 0;
}
std::size_t
ConfigSet::count(const std::string &key_) const
{
std::shared_lock<std::shared_mutex> lk(_mutex);
return _set.count(key_);
}

View file

@ -20,11 +20,15 @@
#include "tofrom_string.hpp"
#include <cstddef>
#include <set>
#include <shared_mutex>
#include <string>
class ConfigSet : public std::set<std::string>, public ToFromString
// setxattr can replace the set while requests read it, so every access
// goes through the lock.
class ConfigSet : public ToFromString
{
public:
ConfigSet(const std::string &str);
@ -32,4 +36,11 @@ public:
public:
std::string to_string(void) const final;
int from_string(const std::string_view) final;
public:
std::size_t count(const std::string &key) const;
private:
mutable std::shared_mutex _mutex;
std::set<std::string> _set;
};

View file

@ -20,6 +20,7 @@
#include "tofrom_string.hpp"
#include <atomic>
#include <string>
@ -39,6 +40,21 @@ public:
{
}
// The option values are set at runtime through xattrs while request
// threads read them, so the storage is atomic. Copies take a
// snapshot since several call sites pass options by value.
Enum(const Enum &o_)
: _data(o_._data.load())
{
}
Enum<ENUM>&
operator=(const Enum &o_)
{
_data = o_._data.load();
return *this;
}
public:
Enum<ENUM>&
operator=(const ENUM data_)
@ -57,13 +73,13 @@ public:
public:
operator ENUM() const
{
return _data;
return _data.load();
}
public:
bool operator==(const ENUM data_) const
{
return (_data == data_);
return (_data.load() == data_);
}
public:
@ -73,9 +89,9 @@ public:
public:
int to_int() const
{
return (int)_data;
return (int)_data.load();
}
private:
ENUM _data;
std::atomic<ENUM> _data;
};

View file

@ -191,6 +191,22 @@ str::from(const std::string_view val_,
return 0;
}
int
str::from(const std::string_view val_,
std::atomic<u64> *rv_)
{
int rv;
u64 tmp;
rv = str::from(val_,&tmp);
if(rv < 0)
return rv;
rv_->store(tmp,std::memory_order_relaxed);
return 0;
}
int
str::from(const std::string_view value_,
std::string *str_)

View file

@ -21,6 +21,7 @@
#include "base_types.h"
#include "fs_path.hpp"
#include <atomic>
#include <string>
#include <string_view>
@ -30,6 +31,7 @@ namespace str
int from(const std::string_view, bool *);
int from(const std::string_view, int *);
int from(const std::string_view, u64 *);
int from(const std::string_view, std::atomic<u64> *);
int from(const std::string_view, s64 *);
int from(const std::string_view, std::string *);
int from(const std::string_view, const std::string *);

View file

@ -193,8 +193,9 @@ FUSE::getxattr(const fuse_req_ctx_t *ctx_,
::_is_attrname_security_capability(attrname_))
return -ENOATTR;
if(cfg.xattr.to_int())
return -cfg.xattr.to_int();
const int xattr = cfg.xattr.to_int();
if(xattr)
return -xattr;
return ::_getxattr(cfg.func.getxattr.policy,
cfg.branches,

View file

@ -103,8 +103,9 @@ FUSE::removexattr(const fuse_req_ctx_t *ctx_,
if(Config::is_ctrl_file(fusepath))
return -ENOATTR;
if(cfg.xattr.to_int())
return -cfg.xattr.to_int();
const int xattr = cfg.xattr.to_int();
if(xattr)
return -xattr;
return ::_removexattr(cfg.func.removexattr.policy,
cfg.func.getxattr.policy,

View file

@ -191,8 +191,9 @@ _setxattr(const fuse_req_ctx_t *ctx_,
::_is_attrname_security_capability(attrname_))
return -ENOATTR;
if(cfg.xattr.to_int())
return -cfg.xattr.to_int();
const int xattr = cfg.xattr.to_int();
if(xattr)
return -xattr;
return ::_setxattr(cfg.func.setxattr.policy,
cfg.func.getxattr.policy,

View file

@ -41,6 +41,12 @@ str::to(cu64 uint64_)
return fmt::format("{}",uint64_);
}
std::string
str::to(const std::atomic<u64> &uint64_)
{
return str::to(uint64_.load(std::memory_order_relaxed));
}
std::string
str::to(cs64 int64_)
{

View file

@ -21,6 +21,7 @@
#include "base_types.h"
#include "fs_path.hpp"
#include <atomic>
#include <filesystem>
#include <string>
@ -30,6 +31,7 @@ namespace str
std::string to(const bool);
std::string to(const int);
std::string to(cu64);
std::string to(const std::atomic<u64>&);
std::string to(cs64);
std::string to(const std::string&);
std::string to(const fs::path&);

View file

@ -416,7 +416,7 @@ test_branch_default_ctor_mode()
void
test_branch_to_string_modes()
{
uint64_t global = 0;
std::atomic<u64> global{0};
Branch b(global);
b.path = "/mnt/disk1";
@ -434,7 +434,7 @@ test_branch_to_string_modes()
void
test_branch_to_string_with_minfreespace()
{
uint64_t global = 0;
std::atomic<u64> global{0};
Branch b(global);
b.path = "/mnt/disk1";
@ -481,7 +481,7 @@ test_branch_mode_predicates()
void
test_branch_minfreespace_pointer_vs_value()
{
uint64_t global = 9999;
std::atomic<u64> global{9999};
Branch b(global);
// Reads through the pointer
@ -503,7 +503,7 @@ test_branch_minfreespace_pointer_vs_value()
void
test_branch_copy_constructor()
{
uint64_t global = 100;
std::atomic<u64> global{100};
Branch orig(global);
orig.path = "/orig";
orig.mode = Branch::Mode::RO;
@ -517,7 +517,7 @@ test_branch_copy_constructor()
void
test_branch_copy_assignment()
{
uint64_t global = 77;
std::atomic<u64> global{77};
Branch a(global);
a.path = "/a";
a.mode = Branch::Mode::NC;
@ -758,7 +758,7 @@ test_branches_srcmounts_empty()
void
test_branch_to_string_1t()
{
uint64_t global = 0;
std::atomic<u64> global{0};
Branch b(global);
b.path = "/mnt/disk1";
b.mode = Branch::Mode::RW;
@ -774,7 +774,7 @@ void
test_branch_to_string_non_exact_multiple()
{
// 1536 = 1.5K: not divisible by any tier, emitted as raw decimal
uint64_t global = 0;
std::atomic<u64> global{0};
Branch b(global);
b.path = "/mnt/disk1";
b.mode = Branch::Mode::RW;
@ -803,7 +803,7 @@ test_branch_copy_pointer_aliasing()
{
// A copy of a pointer-backed Branch shares the same pointer —
// mutating the original global value is seen by both.
uint64_t global = 1024;
std::atomic<u64> global{1024};
Branch orig(global);
orig.path = "/mnt/a";
orig.mode = Branch::Mode::RW;
@ -4343,8 +4343,8 @@ test_rnd_shrink_to_rand_elem()
void
test_branch_copy_assignment_relinks_default_minfreespace()
{
uint64_t default_a = 1234;
uint64_t default_b = 5678;
std::atomic<u64> default_a{1234};
std::atomic<u64> default_b{5678};
Branches::Impl impl_a(&default_a);
Branches::Impl impl_b(&default_b);
@ -4356,16 +4356,16 @@ test_branch_copy_assignment_relinks_default_minfreespace()
TEST_CHECK(impl_a.size() == 1);
for(auto &branch : impl_a)
{
if(std::holds_alternative<const u64*>(branch._minfreespace))
TEST_CHECK(std::get<const u64*>(branch._minfreespace) == &default_a);
if(std::holds_alternative<const std::atomic<u64>*>(branch._minfreespace))
TEST_CHECK(std::get<const std::atomic<u64>*>(branch._minfreespace) == &default_a);
}
}
void
test_branch_move_assignment_relinks_default_minfreespace()
{
uint64_t default_a = 1234;
uint64_t default_b = 5678;
std::atomic<u64> default_a{1234};
std::atomic<u64> default_b{5678};
Branches::Impl impl_a(&default_a);
Branches::Impl impl_b(&default_b);
@ -4377,8 +4377,8 @@ test_branch_move_assignment_relinks_default_minfreespace()
TEST_CHECK(impl_a.size() == 1);
for(auto &branch : impl_a)
{
if(std::holds_alternative<const u64*>(branch._minfreespace))
TEST_CHECK(std::get<const u64*>(branch._minfreespace) == &default_a);
if(std::holds_alternative<const std::atomic<u64>*>(branch._minfreespace))
TEST_CHECK(std::get<const std::atomic<u64>*>(branch._minfreespace) == &default_a);
}
}