fix: make the default minfreespace safe to change at runtime

- Setting minfreespace through the .mergerfs xattr wrote the shared
  default while request threads read it through each branch, with no
  synchronization. That is a data race; on 32-bit targets a request
  could see half of the new 64-bit value.
- Store the default as std::atomic<u64> and read and write it with
  relaxed loads and stores. Per-branch values are unchanged.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:42:19 -05:00
parent de2dad1a88
commit fd225b999c
10 changed files with 61 additions and 30 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

@ -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

@ -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

@ -417,7 +417,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";
@ -435,7 +435,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";
@ -482,7 +482,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
@ -504,7 +504,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;
@ -518,7 +518,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;
@ -759,7 +759,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;
@ -775,7 +775,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;
@ -804,7 +804,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;
@ -4261,8 +4261,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);
@ -4274,16 +4274,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);
@ -4295,8 +4295,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);
}
}