Loading CHANGELOG.md +9 Changes for CHANGELOG.md: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ### New - FUSE client: CMake option `GKFS_FUSE_POINTERTRICK` uses inode addresses as node ids, avoiding the locked inode map. ### Changed - FUSE client: inode handling moved to `InodeTable`. ### Fixed - FUSE client pointer trick: repeated lookups did not count references (use after free), rename left the old path on the inode, FIFO and root node ids were treated as heap pointers. - FUSE client inode lifetime: races between lookup and forget, leaked inodes in rename and readdirplus, forgetting an old inode removed the path binding of its successor, `getattr` failed on FIFOs. ## [0.9.6] - 2026-10-02 ### New Loading CMake/gkfs-options.cmake +8 Changes for CMake/gkfs-options.cmake: 8 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -264,6 +264,14 @@ gkfs_define_option( DEFAULT_VALUE ON ) # FUSE node ids are inode addresses instead of keys of a locked map gkfs_define_option( GKFS_FUSE_POINTERTRICK HELP_TEXT "Use inode addresses as FUSE node ids" DEFAULT_VALUE OFF DESCRIPTION "FUSE client resolves node ids without a lock" ) cmake_dependent_option(GKFS_INSTALL_TESTS "Install GekkoFS self tests" OFF "GKFS_BUILD_TESTS" OFF) Loading README.md +2 Changes for README.md: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -996,6 +996,8 @@ Once it is enabled, `--dbbackend` option will be functional. - `GKFS_USE_GUIDED_DISTRIBUTION_PATH` - File Path for guided distributor (default: /tmp/guided.txt) - `GKFS_BUILD_USER_LIB` - Build the user library (default: ON) - `GKFS_BUILD_LIBC_INTERCEPTION` - Builds the libc interception .so (default: ON) - `GKFS_FUSE_POINTERTRICK` - FUSE client uses inode addresses as node ids, so requests resolve their inode without the locked inode map (default: OFF). Lookup, create, unlink and rename still lock a shard of the path map. #### Logging - `GKFS_ENABLE_CLIENT_LOG` - Enable logging messages in clients (default: ON) Loading include/client/fuse/fuse_client.hpp +1 −36 Changes for include/client/fuse/fuse_client.hpp: 1 added line, 36 removed lines. Original line number Diff line number Diff line Loading @@ -90,42 +90,7 @@ extern "C" { #include <client/preload.hpp> #include <client/preload_context.hpp> #include <client/user_functions.hpp> // Pointer trick to save a hash table and the lookup for inodes. // Only works if the fuse_ino_t has the size of uintptr_t. // For older compilers, the else case fails during compile time if the size does // not match. From libfuse passthrough example #if defined(__GNUC__) && \ (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && \ !defined __cplusplus _Static_assert(!gkfs::config::fuse::pointertrick || sizeof(fuse_ino_t) >= sizeof(uintptr_t), "fuse_ino_t too small to hold uintptr_t values!"); #else struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct { unsigned _uintptr_to_must_hold_fuse_ino_t : (!gkfs::config::fuse::pointertrick || (sizeof(fuse_ino_t) >= sizeof(uintptr_t)) ? 1 : -1); }; #endif struct Inode { std::string path; std::atomic<uint64_t> lookup_count; Inode() : lookup_count(0) {} Inode(const std::string& p, uint64_t lc) : path(p), lookup_count(lc) {} Inode(const Inode& other) : path(other.path), lookup_count(other.lookup_count.load()) {} Inode& operator=(const Inode& other) { if (this != &other) { path = other.path; lookup_count.store(other.lookup_count.load()); } return *this; } }; #include <client/fuse/inode_table.hpp> enum { CACHE_NEVER, Loading include/client/fuse/inode_table.hpp +416 Changes for include/client/fuse/inode_table.hpp: 416 added lines, 0 removed lines. Original line number Diff line number Diff line /* Copyright 2018-2026, Barcelona Supercomputing Center (BSC), Spain Copyright 2015-2026, Johannes Gutenberg Universitaet Mainz, Germany This software was partially supported by the EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu). This software was partially supported by the ADA-FS project under the SPPEXA project funded by the DFG. This software was partially supported by the the European Union’s Horizon 2020 JTI-EuroHPC research and innovation programme, by the project ADMIRE (Project ID: 956748, admire-eurohpc.eu) This project was partially promoted by the Ministry for Digital Transformation and the Civil Service, within the framework of the Recovery, Transformation and Resilience Plan - Funded by the European Union -NextGenerationEU. This file is part of GekkoFS' POSIX interface. GekkoFS' POSIX interface is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. GekkoFS' POSIX interface is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with GekkoFS' POSIX interface. If not, see <https://www.gnu.org/licenses/>. SPDX-License-Identifier: LGPL-3.0-or-later */ #ifndef GKFS_CLIENT_FUSE_INODE_TABLE_HPP #define GKFS_CLIENT_FUSE_INODE_TABLE_HPP #include <algorithm> #include <atomic> #include <cstdint> #include <mutex> #include <string> #include <unordered_map> namespace gkfs::fuse { /// FUSE node identifier, same type as `fuse_ino_t` in libfuse. using node_id = std::uint64_t; /// Same value as `FUSE_ROOT_ID` in libfuse. constexpr node_id root_ino = 1; static_assert(sizeof(node_id) >= sizeof(std::uintptr_t), "FUSE node ids are too small to hold pointers"); /// How node ids are resolved to inodes. enum class Mode { /// Counter ids resolved through a sharded, mutex protected hash map. map, /// The id is the address of the inode: resolving it takes no lock. pointer }; /** * State kept for every node id known to the kernel. * * path() can be read without a lock while a rename replaces it: replaced paths * stay allocated until the inode is destroyed. A reference from path() is * therefore valid while the kernel holds a lookup reference on the inode, * which covers every request carrying its node id. The cost is memory: an inode * keeps every path it was renamed to until it is forgotten. */ class Inode { public: Inode(const std::string& path, std::uint64_t lookup_count) : path_(new PathNode{path, nullptr}), lookup_count_(lookup_count) {} Inode(const Inode&) = delete; Inode& operator=(const Inode&) = delete; ~Inode() { auto* node = path_.load(std::memory_order_acquire); while(node) { auto* prev = node->prev; delete node; node = prev; } } const std::string& path() const { return path_.load(std::memory_order_acquire)->path; } std::uint64_t lookup_count() const { return lookup_count_.load(std::memory_order_acquire); } private: template <Mode> friend class InodeTable; struct PathNode { std::string path; PathNode* prev; }; void set_path(const std::string& path) { auto* node = new PathNode{path, path_.load(std::memory_order_relaxed)}; path_.store(node, std::memory_order_release); } std::atomic<PathNode*> path_; std::atomic<std::uint64_t> lookup_count_; }; /** * Maps FUSE node ids to inodes, and paths to the node id bound to them. * * In Mode::pointer, get() cannot detect unknown node ids. As in libfuse's * passthrough example, this relies on the kernel only sending node ids it * holds a lookup reference on. * * The path map gives a path a stable node id across lookups, which the kernel * needs to keep its dentries valid, and lets rename, which only receives names, * find the moved inode. Requests by node id do not touch it. * * Lifetime rules: * - An inode bound in the path map has a lookup count above zero. * - The count is only increased through the path map, under its shard lock. * - The count only reaches zero under the shard lock, in the critical section * that removes the binding. So no lookup can revive an inode about to be * destroyed, and exactly one thread destroys it. * * Lock order: path shard (ascending index if two), then inode shard. */ template <Mode mode> class InodeTable { public: explicit InodeTable(const std::string& root_path = "/") : root_(root_path, 1) { shard(root_path).paths[root_path] = root_ino; } InodeTable(const InodeTable&) = delete; InodeTable& operator=(const InodeTable&) = delete; ~InodeTable() { release_all(); } /// @return the inode, or nullptr if unknown. Mode::pointer never returns /// nullptr. Inode* get(node_id ino) { if(ino == root_ino) { return &root_; } if constexpr(mode == Mode::pointer) { return reinterpret_cast<Inode*>(static_cast<std::uintptr_t>(ino)); } else { auto& s = ino_shards_[ino % shard_count]; std::lock_guard<std::mutex> lk(s.mutex); auto it = s.inodes.find(ino); return it != s.inodes.end() ? it->second : nullptr; } } /// Takes a lookup reference on the inode bound to path, allocating one if /// there is none. Balanced by forget(). node_id lookup(const std::string& path) { auto& s = shard(path); std::lock_guard<std::mutex> lk(s.mutex); auto it = s.paths.find(path); if(it != s.paths.end()) { get(it->second) ->lookup_count_.fetch_add(1, std::memory_order_acq_rel); return it->second; } auto ino = allocate(path); s.paths.emplace(path, ino); return ino; } /// Allocates an inode with one lookup reference for a created file and /// binds it to path. A previous inode of path lives until forgotten. node_id create(const std::string& path) { auto& s = shard(path); std::lock_guard<std::mutex> lk(s.mutex); auto ino = allocate(path); s.paths[path] = ino; return ino; } /// Removes the binding of path. The inode lives until forgotten. void unlink(const std::string& path) { auto& s = shard(path); std::lock_guard<std::mutex> lk(s.mutex); s.paths.erase(path); } /// Moves the inode bound to old_path to new_path. An inode bound to /// new_path loses its binding and gets an empty path. References are /// unchanged, and nothing is allocated if old_path has no inode. void rename(const std::string& old_path, const std::string& new_path) { if(old_path == new_path) { return; } auto old_index = shard_index(old_path); auto new_index = shard_index(new_path); std::unique_lock<std::mutex> first( path_shards_[std::min(old_index, new_index)].mutex); std::unique_lock<std::mutex> second; if(old_index != new_index) { second = std::unique_lock<std::mutex>( path_shards_[std::max(old_index, new_index)].mutex); } auto& old_shard = path_shards_[old_index]; auto& new_shard = path_shards_[new_index]; auto dst = new_shard.paths.find(new_path); if(dst != new_shard.paths.end()) { get(dst->second)->set_path(""); new_shard.paths.erase(dst); } auto src = old_shard.paths.find(old_path); if(src != old_shard.paths.end()) { auto ino = src->second; old_shard.paths.erase(src); new_shard.paths[new_path] = ino; get(ino)->set_path(new_path); } } /// Drops nlookup references and destroys the inode with the last one. /// Dropping more references than held counts as dropping all. The root /// inode lives as long as the table. /// @return true if the inode was destroyed bool forget(node_id ino, std::uint64_t nlookup) { if(ino == root_ino) { return false; } auto* inode = get(ino); if(!inode) { return false; } // References remain: nothing to unbind, no lock needed. if(drop_unless_last(*inode, nlookup)) { return false; } { auto lk = lock_path_of(*inode); // A lookup may have taken a reference before we got the lock. if(drop_unless_last(*inode, nlookup)) { return false; } inode->lookup_count_.store(0, std::memory_order_release); // The path may already be bound to a newer inode, e.g., after // unlink and create. Keep that binding. auto& s = shard(inode->path()); auto it = s.paths.find(inode->path()); if(it != s.paths.end() && it->second == ino) { s.paths.erase(it); } } destroy(ino, inode); return true; } /// Destroys all inodes the table can reach. Only valid once no request can /// run anymore, i.e., after unmount: the kernel need not send forgets for /// what it still holds. In Mode::pointer, unlinked inodes the kernel did /// not forget are unreachable and stay counted by live(). void release_all() { for(auto& s : path_shards_) { std::lock_guard<std::mutex> lk(s.mutex); for(auto& binding : s.paths) { if(binding.second != root_ino) { destroy(binding.second, get(binding.second)); } } s.paths.clear(); } if constexpr(mode == Mode::map) { for(auto& s : ino_shards_) { std::lock_guard<std::mutex> lk(s.mutex); for(auto& entry : s.inodes) { delete entry.second; } released_.fetch_add(s.inodes.size(), std::memory_order_relaxed); s.inodes.clear(); } } } /// Inodes allocated over the lifetime of the table, without root. std::uint64_t allocated() const { return allocated_.load(std::memory_order_relaxed); } /// Inodes destroyed over the lifetime of the table, without root. std::uint64_t released() const { return released_.load(std::memory_order_relaxed); } /// Inodes currently alive, without root. std::uint64_t live() const { return allocated() - released(); } private: static constexpr std::size_t shard_count = 32; struct PathShard { std::mutex mutex; std::unordered_map<std::string, node_id> paths; }; struct InodeShard { std::mutex mutex; std::unordered_map<node_id, Inode*> inodes; }; static std::size_t shard_index(const std::string& path) { return std::hash<std::string>{}(path) % shard_count; } PathShard& shard(const std::string& path) { return path_shards_[shard_index(path)]; } /// Subtracts nlookup if at least one reference remains. /// @return false if nothing was subtracted static bool drop_unless_last(Inode& inode, std::uint64_t nlookup) { auto current = inode.lookup_count_.load(std::memory_order_acquire); while(current > nlookup) { if(inode.lookup_count_.compare_exchange_weak( current, current - nlookup, std::memory_order_acq_rel)) { return true; } } return false; } /// Locks the path shard of an inode. Renames change the path under this /// lock, so the path is stable while it is held. std::unique_lock<std::mutex> lock_path_of(const Inode& inode) { for(;;) { const auto& path = inode.path(); std::unique_lock<std::mutex> lk(shard(path).mutex); if(&inode.path() == &path) { return lk; } } } node_id allocate(const std::string& path) { allocated_.fetch_add(1, std::memory_order_relaxed); auto* inode = new Inode(path, 1); if constexpr(mode == Mode::pointer) { return static_cast<node_id>( reinterpret_cast<std::uintptr_t>(inode)); } else { auto ino = next_ino_.fetch_add(1, std::memory_order_relaxed); auto& s = ino_shards_[ino % shard_count]; std::lock_guard<std::mutex> lk(s.mutex); s.inodes.emplace(ino, inode); return ino; } } void destroy(node_id ino, Inode* inode) { released_.fetch_add(1, std::memory_order_relaxed); if constexpr(mode == Mode::map) { auto& s = ino_shards_[ino % shard_count]; std::lock_guard<std::mutex> lk(s.mutex); s.inodes.erase(ino); } delete inode; } Inode root_; PathShard path_shards_[shard_count]; InodeShard ino_shards_[shard_count]; // Mode::map only std::atomic<node_id> next_ino_{root_ino + 1}; std::atomic<std::uint64_t> allocated_{0}; std::atomic<std::uint64_t> released_{0}; }; } // namespace gkfs::fuse #endif // GKFS_CLIENT_FUSE_INODE_TABLE_HPP Loading
CHANGELOG.md +9 Changes for CHANGELOG.md: 9 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] ### New - FUSE client: CMake option `GKFS_FUSE_POINTERTRICK` uses inode addresses as node ids, avoiding the locked inode map. ### Changed - FUSE client: inode handling moved to `InodeTable`. ### Fixed - FUSE client pointer trick: repeated lookups did not count references (use after free), rename left the old path on the inode, FIFO and root node ids were treated as heap pointers. - FUSE client inode lifetime: races between lookup and forget, leaked inodes in rename and readdirplus, forgetting an old inode removed the path binding of its successor, `getattr` failed on FIFOs. ## [0.9.6] - 2026-10-02 ### New Loading
CMake/gkfs-options.cmake +8 Changes for CMake/gkfs-options.cmake: 8 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -264,6 +264,14 @@ gkfs_define_option( DEFAULT_VALUE ON ) # FUSE node ids are inode addresses instead of keys of a locked map gkfs_define_option( GKFS_FUSE_POINTERTRICK HELP_TEXT "Use inode addresses as FUSE node ids" DEFAULT_VALUE OFF DESCRIPTION "FUSE client resolves node ids without a lock" ) cmake_dependent_option(GKFS_INSTALL_TESTS "Install GekkoFS self tests" OFF "GKFS_BUILD_TESTS" OFF) Loading
README.md +2 Changes for README.md: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -996,6 +996,8 @@ Once it is enabled, `--dbbackend` option will be functional. - `GKFS_USE_GUIDED_DISTRIBUTION_PATH` - File Path for guided distributor (default: /tmp/guided.txt) - `GKFS_BUILD_USER_LIB` - Build the user library (default: ON) - `GKFS_BUILD_LIBC_INTERCEPTION` - Builds the libc interception .so (default: ON) - `GKFS_FUSE_POINTERTRICK` - FUSE client uses inode addresses as node ids, so requests resolve their inode without the locked inode map (default: OFF). Lookup, create, unlink and rename still lock a shard of the path map. #### Logging - `GKFS_ENABLE_CLIENT_LOG` - Enable logging messages in clients (default: ON) Loading
include/client/fuse/fuse_client.hpp +1 −36 Changes for include/client/fuse/fuse_client.hpp: 1 added line, 36 removed lines. Original line number Diff line number Diff line Loading @@ -90,42 +90,7 @@ extern "C" { #include <client/preload.hpp> #include <client/preload_context.hpp> #include <client/user_functions.hpp> // Pointer trick to save a hash table and the lookup for inodes. // Only works if the fuse_ino_t has the size of uintptr_t. // For older compilers, the else case fails during compile time if the size does // not match. From libfuse passthrough example #if defined(__GNUC__) && \ (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 6) && \ !defined __cplusplus _Static_assert(!gkfs::config::fuse::pointertrick || sizeof(fuse_ino_t) >= sizeof(uintptr_t), "fuse_ino_t too small to hold uintptr_t values!"); #else struct _uintptr_to_must_hold_fuse_ino_t_dummy_struct { unsigned _uintptr_to_must_hold_fuse_ino_t : (!gkfs::config::fuse::pointertrick || (sizeof(fuse_ino_t) >= sizeof(uintptr_t)) ? 1 : -1); }; #endif struct Inode { std::string path; std::atomic<uint64_t> lookup_count; Inode() : lookup_count(0) {} Inode(const std::string& p, uint64_t lc) : path(p), lookup_count(lc) {} Inode(const Inode& other) : path(other.path), lookup_count(other.lookup_count.load()) {} Inode& operator=(const Inode& other) { if (this != &other) { path = other.path; lookup_count.store(other.lookup_count.load()); } return *this; } }; #include <client/fuse/inode_table.hpp> enum { CACHE_NEVER, Loading
include/client/fuse/inode_table.hpp +416 Changes for include/client/fuse/inode_table.hpp: 416 added lines, 0 removed lines. Original line number Diff line number Diff line /* Copyright 2018-2026, Barcelona Supercomputing Center (BSC), Spain Copyright 2015-2026, Johannes Gutenberg Universitaet Mainz, Germany This software was partially supported by the EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu). This software was partially supported by the ADA-FS project under the SPPEXA project funded by the DFG. This software was partially supported by the the European Union’s Horizon 2020 JTI-EuroHPC research and innovation programme, by the project ADMIRE (Project ID: 956748, admire-eurohpc.eu) This project was partially promoted by the Ministry for Digital Transformation and the Civil Service, within the framework of the Recovery, Transformation and Resilience Plan - Funded by the European Union -NextGenerationEU. This file is part of GekkoFS' POSIX interface. GekkoFS' POSIX interface is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. GekkoFS' POSIX interface is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with GekkoFS' POSIX interface. If not, see <https://www.gnu.org/licenses/>. SPDX-License-Identifier: LGPL-3.0-or-later */ #ifndef GKFS_CLIENT_FUSE_INODE_TABLE_HPP #define GKFS_CLIENT_FUSE_INODE_TABLE_HPP #include <algorithm> #include <atomic> #include <cstdint> #include <mutex> #include <string> #include <unordered_map> namespace gkfs::fuse { /// FUSE node identifier, same type as `fuse_ino_t` in libfuse. using node_id = std::uint64_t; /// Same value as `FUSE_ROOT_ID` in libfuse. constexpr node_id root_ino = 1; static_assert(sizeof(node_id) >= sizeof(std::uintptr_t), "FUSE node ids are too small to hold pointers"); /// How node ids are resolved to inodes. enum class Mode { /// Counter ids resolved through a sharded, mutex protected hash map. map, /// The id is the address of the inode: resolving it takes no lock. pointer }; /** * State kept for every node id known to the kernel. * * path() can be read without a lock while a rename replaces it: replaced paths * stay allocated until the inode is destroyed. A reference from path() is * therefore valid while the kernel holds a lookup reference on the inode, * which covers every request carrying its node id. The cost is memory: an inode * keeps every path it was renamed to until it is forgotten. */ class Inode { public: Inode(const std::string& path, std::uint64_t lookup_count) : path_(new PathNode{path, nullptr}), lookup_count_(lookup_count) {} Inode(const Inode&) = delete; Inode& operator=(const Inode&) = delete; ~Inode() { auto* node = path_.load(std::memory_order_acquire); while(node) { auto* prev = node->prev; delete node; node = prev; } } const std::string& path() const { return path_.load(std::memory_order_acquire)->path; } std::uint64_t lookup_count() const { return lookup_count_.load(std::memory_order_acquire); } private: template <Mode> friend class InodeTable; struct PathNode { std::string path; PathNode* prev; }; void set_path(const std::string& path) { auto* node = new PathNode{path, path_.load(std::memory_order_relaxed)}; path_.store(node, std::memory_order_release); } std::atomic<PathNode*> path_; std::atomic<std::uint64_t> lookup_count_; }; /** * Maps FUSE node ids to inodes, and paths to the node id bound to them. * * In Mode::pointer, get() cannot detect unknown node ids. As in libfuse's * passthrough example, this relies on the kernel only sending node ids it * holds a lookup reference on. * * The path map gives a path a stable node id across lookups, which the kernel * needs to keep its dentries valid, and lets rename, which only receives names, * find the moved inode. Requests by node id do not touch it. * * Lifetime rules: * - An inode bound in the path map has a lookup count above zero. * - The count is only increased through the path map, under its shard lock. * - The count only reaches zero under the shard lock, in the critical section * that removes the binding. So no lookup can revive an inode about to be * destroyed, and exactly one thread destroys it. * * Lock order: path shard (ascending index if two), then inode shard. */ template <Mode mode> class InodeTable { public: explicit InodeTable(const std::string& root_path = "/") : root_(root_path, 1) { shard(root_path).paths[root_path] = root_ino; } InodeTable(const InodeTable&) = delete; InodeTable& operator=(const InodeTable&) = delete; ~InodeTable() { release_all(); } /// @return the inode, or nullptr if unknown. Mode::pointer never returns /// nullptr. Inode* get(node_id ino) { if(ino == root_ino) { return &root_; } if constexpr(mode == Mode::pointer) { return reinterpret_cast<Inode*>(static_cast<std::uintptr_t>(ino)); } else { auto& s = ino_shards_[ino % shard_count]; std::lock_guard<std::mutex> lk(s.mutex); auto it = s.inodes.find(ino); return it != s.inodes.end() ? it->second : nullptr; } } /// Takes a lookup reference on the inode bound to path, allocating one if /// there is none. Balanced by forget(). node_id lookup(const std::string& path) { auto& s = shard(path); std::lock_guard<std::mutex> lk(s.mutex); auto it = s.paths.find(path); if(it != s.paths.end()) { get(it->second) ->lookup_count_.fetch_add(1, std::memory_order_acq_rel); return it->second; } auto ino = allocate(path); s.paths.emplace(path, ino); return ino; } /// Allocates an inode with one lookup reference for a created file and /// binds it to path. A previous inode of path lives until forgotten. node_id create(const std::string& path) { auto& s = shard(path); std::lock_guard<std::mutex> lk(s.mutex); auto ino = allocate(path); s.paths[path] = ino; return ino; } /// Removes the binding of path. The inode lives until forgotten. void unlink(const std::string& path) { auto& s = shard(path); std::lock_guard<std::mutex> lk(s.mutex); s.paths.erase(path); } /// Moves the inode bound to old_path to new_path. An inode bound to /// new_path loses its binding and gets an empty path. References are /// unchanged, and nothing is allocated if old_path has no inode. void rename(const std::string& old_path, const std::string& new_path) { if(old_path == new_path) { return; } auto old_index = shard_index(old_path); auto new_index = shard_index(new_path); std::unique_lock<std::mutex> first( path_shards_[std::min(old_index, new_index)].mutex); std::unique_lock<std::mutex> second; if(old_index != new_index) { second = std::unique_lock<std::mutex>( path_shards_[std::max(old_index, new_index)].mutex); } auto& old_shard = path_shards_[old_index]; auto& new_shard = path_shards_[new_index]; auto dst = new_shard.paths.find(new_path); if(dst != new_shard.paths.end()) { get(dst->second)->set_path(""); new_shard.paths.erase(dst); } auto src = old_shard.paths.find(old_path); if(src != old_shard.paths.end()) { auto ino = src->second; old_shard.paths.erase(src); new_shard.paths[new_path] = ino; get(ino)->set_path(new_path); } } /// Drops nlookup references and destroys the inode with the last one. /// Dropping more references than held counts as dropping all. The root /// inode lives as long as the table. /// @return true if the inode was destroyed bool forget(node_id ino, std::uint64_t nlookup) { if(ino == root_ino) { return false; } auto* inode = get(ino); if(!inode) { return false; } // References remain: nothing to unbind, no lock needed. if(drop_unless_last(*inode, nlookup)) { return false; } { auto lk = lock_path_of(*inode); // A lookup may have taken a reference before we got the lock. if(drop_unless_last(*inode, nlookup)) { return false; } inode->lookup_count_.store(0, std::memory_order_release); // The path may already be bound to a newer inode, e.g., after // unlink and create. Keep that binding. auto& s = shard(inode->path()); auto it = s.paths.find(inode->path()); if(it != s.paths.end() && it->second == ino) { s.paths.erase(it); } } destroy(ino, inode); return true; } /// Destroys all inodes the table can reach. Only valid once no request can /// run anymore, i.e., after unmount: the kernel need not send forgets for /// what it still holds. In Mode::pointer, unlinked inodes the kernel did /// not forget are unreachable and stay counted by live(). void release_all() { for(auto& s : path_shards_) { std::lock_guard<std::mutex> lk(s.mutex); for(auto& binding : s.paths) { if(binding.second != root_ino) { destroy(binding.second, get(binding.second)); } } s.paths.clear(); } if constexpr(mode == Mode::map) { for(auto& s : ino_shards_) { std::lock_guard<std::mutex> lk(s.mutex); for(auto& entry : s.inodes) { delete entry.second; } released_.fetch_add(s.inodes.size(), std::memory_order_relaxed); s.inodes.clear(); } } } /// Inodes allocated over the lifetime of the table, without root. std::uint64_t allocated() const { return allocated_.load(std::memory_order_relaxed); } /// Inodes destroyed over the lifetime of the table, without root. std::uint64_t released() const { return released_.load(std::memory_order_relaxed); } /// Inodes currently alive, without root. std::uint64_t live() const { return allocated() - released(); } private: static constexpr std::size_t shard_count = 32; struct PathShard { std::mutex mutex; std::unordered_map<std::string, node_id> paths; }; struct InodeShard { std::mutex mutex; std::unordered_map<node_id, Inode*> inodes; }; static std::size_t shard_index(const std::string& path) { return std::hash<std::string>{}(path) % shard_count; } PathShard& shard(const std::string& path) { return path_shards_[shard_index(path)]; } /// Subtracts nlookup if at least one reference remains. /// @return false if nothing was subtracted static bool drop_unless_last(Inode& inode, std::uint64_t nlookup) { auto current = inode.lookup_count_.load(std::memory_order_acquire); while(current > nlookup) { if(inode.lookup_count_.compare_exchange_weak( current, current - nlookup, std::memory_order_acq_rel)) { return true; } } return false; } /// Locks the path shard of an inode. Renames change the path under this /// lock, so the path is stable while it is held. std::unique_lock<std::mutex> lock_path_of(const Inode& inode) { for(;;) { const auto& path = inode.path(); std::unique_lock<std::mutex> lk(shard(path).mutex); if(&inode.path() == &path) { return lk; } } } node_id allocate(const std::string& path) { allocated_.fetch_add(1, std::memory_order_relaxed); auto* inode = new Inode(path, 1); if constexpr(mode == Mode::pointer) { return static_cast<node_id>( reinterpret_cast<std::uintptr_t>(inode)); } else { auto ino = next_ino_.fetch_add(1, std::memory_order_relaxed); auto& s = ino_shards_[ino % shard_count]; std::lock_guard<std::mutex> lk(s.mutex); s.inodes.emplace(ino, inode); return ino; } } void destroy(node_id ino, Inode* inode) { released_.fetch_add(1, std::memory_order_relaxed); if constexpr(mode == Mode::map) { auto& s = ino_shards_[ino % shard_count]; std::lock_guard<std::mutex> lk(s.mutex); s.inodes.erase(ino); } delete inode; } Inode root_; PathShard path_shards_[shard_count]; InodeShard ino_shards_[shard_count]; // Mode::map only std::atomic<node_id> next_ino_{root_ino + 1}; std::atomic<std::uint64_t> allocated_{0}; std::atomic<std::uint64_t> released_{0}; }; } // namespace gkfs::fuse #endif // GKFS_CLIENT_FUSE_INODE_TABLE_HPP