mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 04:46:41 -05:00
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:
parent
416af29cc1
commit
4a665bbf32
10 changed files with 61 additions and 30 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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_)
|
||||
|
|
|
|||
|
|
@ -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 *);
|
||||
|
|
|
|||
|
|
@ -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_)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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&);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue