diff --git a/src/branch.cpp b/src/branch.cpp index 8f867404..d5c21311 100644 --- a/src/branch.cpp +++ b/src/branch.cpp @@ -31,7 +31,7 @@ Branch::Branch(const Branch &branch_) { } -Branch::Branch(const u64 &default_minfreespace_) +Branch::Branch(const std::atomic &default_minfreespace_) : _minfreespace(&default_minfreespace_) { } @@ -75,8 +75,8 @@ Branch::set_minfreespace(const u64 minfreespace_) u64 Branch::minfreespace(void) const { - if(std::holds_alternative(_minfreespace)) - return *std::get(_minfreespace); + if(std::holds_alternative*>(_minfreespace)) + return std::get*>(_minfreespace)->load(std::memory_order_relaxed); return std::get(_minfreespace); } diff --git a/src/branch.hpp b/src/branch.hpp index 522c45d7..c640cba5 100644 --- a/src/branch.hpp +++ b/src/branch.hpp @@ -22,6 +22,7 @@ #include "strvec.hpp" #include "fs_path.hpp" +#include #include #include #include @@ -41,14 +42,16 @@ public: }; public: - std::variant _minfreespace; + // A default held by pointer can be changed at runtime while + // requests read it. + std::variant*> _minfreespace; Mode mode; fs::path path; public: Branch(); Branch(const Branch&); - Branch(const u64 &default_minfreespace); + Branch(const std::atomic &default_minfreespace); public: Branch& operator=(const Branch&) = default; diff --git a/src/branches.cpp b/src/branches.cpp index bc942be0..74787ea5 100644 --- a/src/branches.cpp +++ b/src/branches.cpp @@ -36,7 +36,7 @@ #include -Branches::Impl::Impl(const u64 *default_minfreespace_) +Branches::Impl::Impl(const std::atomic *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(branch._minfreespace)) + if(std::holds_alternative*>(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(branch._minfreespace)) + if(std::holds_alternative*>(branch._minfreespace)) branch._minfreespace = _default_minfreespace; } @@ -86,7 +86,7 @@ Branches::Impl::operator=(Branches::Impl &&rval_) } const -u64& +std::atomic& Branches::Impl::minfreespace(void) const { return *_default_minfreespace; diff --git a/src/branches.hpp b/src/branches.hpp index dae01822..89d697cf 100644 --- a/src/branches.hpp +++ b/src/branches.hpp @@ -23,6 +23,7 @@ #include "strvec.hpp" #include "tofrom_string.hpp" +#include #include #include #include @@ -39,20 +40,20 @@ public: public std::vector { private: - const u64 *_default_minfreespace; + const std::atomic *_default_minfreespace; public: using Ptr = std::shared_ptr; public: - Impl(const u64 *default_minfreespace); + Impl(const std::atomic *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 &minfreespace(void) const; void to_paths(StrVec &strvec) const; std::vector 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 minfreespace{MINFREESPACE_DEFAULT}; private: Branches::Ptr _impl; diff --git a/src/config.hpp b/src/config.hpp index 0005a2e3..2f30fefa 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -140,7 +140,7 @@ public: ConfigBOOL link_cow; LinkEXDEV link_exdev; LogFile log_file; - TFSRef minfreespace; + TFSRef> minfreespace; fs::path mountpoint; MoveOnENOSPC moveonenospc; NFSOpenHack nfsopenhack; diff --git a/src/config_set.cpp b/src/config_set.cpp index 82f40c72..e112c109 100644 --- a/src/config_set.cpp +++ b/src/config_set.cpp @@ -20,6 +20,8 @@ #include "str.hpp" +#include + 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 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 lk(_mutex); + + _set.swap(tmp); return 0; } + +std::size_t +ConfigSet::count(const std::string &key_) const +{ + std::shared_lock lk(_mutex); + + return _set.count(key_); +} diff --git a/src/config_set.hpp b/src/config_set.hpp index e6333b80..99e358a0 100644 --- a/src/config_set.hpp +++ b/src/config_set.hpp @@ -20,11 +20,15 @@ #include "tofrom_string.hpp" +#include #include +#include #include -class ConfigSet : public std::set, 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 _set; }; diff --git a/src/enum.hpp b/src/enum.hpp index db079f66..d8cd213e 100644 --- a/src/enum.hpp +++ b/src/enum.hpp @@ -20,6 +20,7 @@ #include "tofrom_string.hpp" +#include #include @@ -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& + operator=(const Enum &o_) + { + _data = o_._data.load(); + return *this; + } + public: 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 _data; }; diff --git a/src/from_string.cpp b/src/from_string.cpp index bab0d4d6..4b02e3a3 100644 --- a/src/from_string.cpp +++ b/src/from_string.cpp @@ -191,6 +191,22 @@ str::from(const std::string_view val_, return 0; } +int +str::from(const std::string_view val_, + std::atomic *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_) diff --git a/src/from_string.hpp b/src/from_string.hpp index fc39db98..b034f36f 100644 --- a/src/from_string.hpp +++ b/src/from_string.hpp @@ -21,6 +21,7 @@ #include "base_types.h" #include "fs_path.hpp" +#include #include #include @@ -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 *); int from(const std::string_view, s64 *); int from(const std::string_view, std::string *); int from(const std::string_view, const std::string *); diff --git a/src/fuse_getxattr.cpp b/src/fuse_getxattr.cpp index 8bedd2d7..53ab8e29 100644 --- a/src/fuse_getxattr.cpp +++ b/src/fuse_getxattr.cpp @@ -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, diff --git a/src/fuse_removexattr.cpp b/src/fuse_removexattr.cpp index f43bb411..3c198b7f 100644 --- a/src/fuse_removexattr.cpp +++ b/src/fuse_removexattr.cpp @@ -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, diff --git a/src/fuse_setxattr.cpp b/src/fuse_setxattr.cpp index 260d2beb..de982cac 100644 --- a/src/fuse_setxattr.cpp +++ b/src/fuse_setxattr.cpp @@ -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, diff --git a/src/to_string.cpp b/src/to_string.cpp index 9a59828b..982471e7 100644 --- a/src/to_string.cpp +++ b/src/to_string.cpp @@ -41,6 +41,12 @@ str::to(cu64 uint64_) return fmt::format("{}",uint64_); } +std::string +str::to(const std::atomic &uint64_) +{ + return str::to(uint64_.load(std::memory_order_relaxed)); +} + std::string str::to(cs64 int64_) { diff --git a/src/to_string.hpp b/src/to_string.hpp index 53ec2e56..b855fe91 100644 --- a/src/to_string.hpp +++ b/src/to_string.hpp @@ -21,6 +21,7 @@ #include "base_types.h" #include "fs_path.hpp" +#include #include #include @@ -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&); std::string to(cs64); std::string to(const std::string&); std::string to(const fs::path&); diff --git a/tests/tests.cpp b/tests/tests.cpp index d1ccf796..222a18b8 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -416,7 +416,7 @@ test_branch_default_ctor_mode() void test_branch_to_string_modes() { - uint64_t global = 0; + std::atomic 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 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 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 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 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 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 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 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 default_a{1234}; + std::atomic 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(branch._minfreespace)) - TEST_CHECK(std::get(branch._minfreespace) == &default_a); + if(std::holds_alternative*>(branch._minfreespace)) + TEST_CHECK(std::get*>(branch._minfreespace) == &default_a); } } void test_branch_move_assignment_relinks_default_minfreespace() { - uint64_t default_a = 1234; - uint64_t default_b = 5678; + std::atomic default_a{1234}; + std::atomic 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(branch._minfreespace)) - TEST_CHECK(std::get(branch._minfreespace) == &default_a); + if(std::holds_alternative*>(branch._minfreespace)) + TEST_CHECK(std::get*>(branch._minfreespace) == &default_a); } }