mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 21:00:13 -05:00
fix: relocate every handle of a file together, after removing the source
- Relocating only the writer left other opens on the old inode. A later write through one of them could succeed and disappear after close and reopen through the pool. Every handle of a kernel node, including opens made after a rename through mergerfs, now belongs to one group. Prepare a replacement descriptor for each handle and switch the group together. An open of the file while a relocation runs waits and gets the moved file. - Only a node that can be relocated gets a group: one first opened while moveonenospc is enabled and without a kernel passthrough ID, which stays on the original file and can't be switched. Every other node's handles take no lock. Enabling moveonenospc at runtime therefore applies to files opened afterwards. - The switch dup2s each replacement onto the handle's own descriptor number, so a handle's fd never changes while it is open. Operations that change the file hold the group's lock shared and wait for a move. Reads, flush and fsync take no lock and continue on the original, which the copy matches. fgetattr and statx take a second lock, which a move holds only from the source unlink until the switch, so they never report the unlinked original. - A copy is not a committed move. Prepare all bindings first, then remove the source; if that fails keep every handle on the original, clean up the provisional copy when it still denotes this copy, and return only bytes actually written. A handle whose descriptor number is at or above the descriptor limit refuses the move, the only way dup2 could fail after the removal. - The source was removed before its copy reached disk, so a crash could leave the name without its data. fsync the copy and the directories leading to it before removing the source, and the source's directory after, so on a filesystem that can sync them a crash leaves the original or a complete copy and never brings the original back over the copy. A failed sync after the removal is logged; the move has completed by then. fs::copyfile is split into stage and publish steps for this; the staged descriptor pins the copy for preparation and cleanup. - chmod, chown, utimes and xattr changes reach mergerfs by path and don't wait for a move. Compare ctime as well as size and mtime, so a change during the copy makes it retry and one after it refuses the move, rather than being lost with the source. - The kernel can reach one file through more than one node: a hardlink whose other name was removed while both were open, a rename made directly on a branch while the file was open, or overlapping branches. A move cannot switch another node's handles, which would keep writing to the original, so it is refused while any other node has the file open. The mover checks the canonical descriptor of every open node. That check and the descriptor limit run after the cheaper checks, so a full pool doesn't pay for them, and before the copy, so a lasting refusal doesn't cost a copy per write; they run again just before the unlink. An open through another node that races the move's last step is not coordinated with it; moveonenospc.md documents this. - Also refuse to relocate a file with more than one link or an entry in the rename-exdev staging tree. Each handle keeps a valid path after the move. - New shared acquisitions wait while a relocation is pending, so a stream of writes to allocated blocks can't starve the mover. - That wait uses std::atomic wait and notify_all, which libstdc++ first provides in GCC 11, so GCC 11 is now the oldest GCC that builds mergerfs. GCC 10 accepts -std=c++20 and concepts, so install-build-pkgs and run-rpmbuild also probe for the atomic operations: such a compiler is rejected up front, or run-rpmbuild moves on to a newer toolset. build.md names the minimum. - moveonenospc kept its enable bit and policy in two fields, which setxattr changed separately while requests read them. Keep one atomic policy pointer where null means disabled, and read it once per relocation. tests.cpp covers enabling, disabling and an invalid value keeping the previous setting. - The relocation moves out of fuse_write.cpp into moveonenospc.cpp, where one function holds every reason to refuse a move. fs::movefile_and_open no longer opens anything and becomes fs::movefile, which asks the caller's hooks whether to move and to switch its handles. - Document the group behaviour, durability and refusals in moveonenospc.md. - TEST_movefile_enospc covers preexisting, secondary and read-only handles, a hardlinked file, a file open through another node, enabling moveonenospc while a file is open, repeated relocation with a handle opened between moves, steady writers, concurrent relocation, reads and fstat during a copy, a mode change during the copy, and failed source removal with and without a written prefix, checking held handles and close/reopen contents. Its fixtures truncate the filler before unlinking it: a scanner holding the file open kept its space allocated, which made the hardlink case fail intermittently. Performance: a file that can't be relocated, because moveonenospc was disabled when it was first opened or it has a passthrough ID, takes no locks and allocates nothing; writes no longer take the per-handle lock they used to. For a relocatable file, reads, flush and fsync take no lock. Writes and other operations that change the file take the group's lock shared, about two uncontended atomic operations, and fgetattr and statx take the second lock, on its own cache line, the same way. An ENOSPC write takes the lock exclusively only when a move is possible. Opening and closing files adds no syscall; the first open of a relocatable file allocates its group. A relocation adds an fstat per open file to find other nodes of the file. While a file is being relocated, operations that change it and new opens of it wait for the copy, as the writer already did; reads and fstat continue.
This commit is contained in:
parent
93ccc160df
commit
c49989b75f
38 changed files with 2324 additions and 496 deletions
130
tests/tests.cpp
130
tests/tests.cpp
|
|
@ -5,7 +5,8 @@
|
|||
#include "fs_clonepath.hpp"
|
||||
#include "fs_copyfile.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_movefile_and_open.hpp"
|
||||
#include "fs_movefile.hpp"
|
||||
#include "fs_open_fd.hpp"
|
||||
#include "fs_statvfs_cache.hpp"
|
||||
#include "from_string.hpp"
|
||||
#include "fuse_link.hpp"
|
||||
|
|
@ -16,6 +17,7 @@
|
|||
#include "hashset.hpp"
|
||||
#include "num.hpp"
|
||||
#include "policies.hpp"
|
||||
#include "procfs.hpp"
|
||||
#include "rapidhash/rapidhash.h"
|
||||
#include "rnd.hpp"
|
||||
#include "scope_guard/scope_guard.hpp"
|
||||
|
|
@ -70,8 +72,11 @@ test_direct_handle_writepage_error()
|
|||
TEST_ASSERT(::setrlimit(RLIMIT_FSIZE,&limit) == 0);
|
||||
DEFER { ::setrlimit(RLIMIT_FSIZE,&saved); };
|
||||
|
||||
// The handle owns its descriptor; the file keeps its own.
|
||||
const int handle_fd = ::dup(fd);
|
||||
TEST_ASSERT(handle_fd >= 0);
|
||||
Branch branch;
|
||||
FileInfo fi(fd,branch,"file",true);
|
||||
FileInfo fi(handle_fd,branch,"file",true);
|
||||
fuse_req_ctx_t ctx = {};
|
||||
fuse_file_info_t ffi = {};
|
||||
ffi.fh = fi.to_fh();
|
||||
|
|
@ -1445,14 +1450,22 @@ test_config_moveonenospc()
|
|||
{
|
||||
MoveOnENOSPC m(false);
|
||||
|
||||
TEST_CHECK(!m.policy());
|
||||
TEST_CHECK(m.to_string() == "false");
|
||||
TEST_CHECK(m.from_string("mfs") == 0);
|
||||
TEST_CHECK(m.policy());
|
||||
TEST_CHECK(m.to_string() == "mfs");
|
||||
TEST_CHECK(m.from_string("mspmfs") == 0);
|
||||
TEST_CHECK(m.to_string() == "mspmfs");
|
||||
TEST_CHECK(m.from_string("true") == 0);
|
||||
TEST_CHECK(m.to_string() == "pfrd");
|
||||
TEST_CHECK(m.from_string("blah") == -EINVAL);
|
||||
// An invalid value must leave the previous setting in force.
|
||||
TEST_CHECK(m.policy());
|
||||
TEST_CHECK(m.to_string() == "pfrd");
|
||||
TEST_CHECK(m.from_string("false") == 0);
|
||||
TEST_CHECK(!m.policy());
|
||||
TEST_CHECK(m.to_string() == "false");
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -4036,6 +4049,61 @@ test_fs_copyfile_source_changes_cleanup_tmpfiles()
|
|||
std::filesystem::remove_all(tmp_dir);
|
||||
}
|
||||
|
||||
// Opens a read-write descriptor of the copy and switches nothing.
|
||||
struct ReopenCopyHooks final : public fs::MoveHooks
|
||||
{
|
||||
int
|
||||
check() final
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
prepare(const int dstfd_,
|
||||
const fs::path &) noexcept final
|
||||
{
|
||||
fd = fs::open_fd(dstfd_,O_RDWR);
|
||||
return ((fd < 0) ? fd : 0);
|
||||
}
|
||||
|
||||
void
|
||||
commit() noexcept final
|
||||
{
|
||||
}
|
||||
|
||||
int fd = -1;
|
||||
};
|
||||
|
||||
// Move the file fd_ describes and return a read-write descriptor of
|
||||
// the copy, or a negative errno.
|
||||
static
|
||||
int
|
||||
_movefile_fd(const Policy::Create &policy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
const int fd_)
|
||||
{
|
||||
int rv;
|
||||
ReopenCopyHooks hooks;
|
||||
|
||||
// fs::open_fd reopens through /proc/self/fd on Linux.
|
||||
#ifdef __linux__
|
||||
procfs::init();
|
||||
DEFER { procfs::shutdown(); };
|
||||
#endif
|
||||
|
||||
rv = fs::movefile(policy_,branches_,branchpath_,fusepath_,fd_,hooks);
|
||||
if(rv < 0)
|
||||
{
|
||||
if(hooks.fd >= 0)
|
||||
::close(hooks.fd);
|
||||
return rv;
|
||||
}
|
||||
|
||||
return hooks.fd;
|
||||
}
|
||||
|
||||
static
|
||||
std::string
|
||||
_file_contents(const fs::path &path_)
|
||||
|
|
@ -4048,6 +4116,51 @@ _file_contents(const fs::path &path_)
|
|||
return contents.str();
|
||||
}
|
||||
|
||||
void
|
||||
test_fs_movefile_direct_io()
|
||||
{
|
||||
// A disk-backed /tmp and a tmpfs destination exercise the read/write
|
||||
// fallback when the filesystems cannot clone or use copy_file_range.
|
||||
if(::access("/dev/shm",W_OK|X_OK) < 0)
|
||||
return;
|
||||
char src_dir[] = "/tmp/mergerfs-test-direct-XXXXXX";
|
||||
char dst_dir[] = "/dev/shm/mergerfs-test-direct-XXXXXX";
|
||||
if(!TEST_CHECK(::mkdtemp(src_dir) != nullptr))
|
||||
return;
|
||||
DEFER { std::filesystem::remove_all(src_dir); };
|
||||
if(!TEST_CHECK(::mkdtemp(dst_dir) != nullptr))
|
||||
return;
|
||||
DEFER { std::filesystem::remove_all(dst_dir); };
|
||||
|
||||
const fs::path src = fs::path(src_dir) / "file";
|
||||
const fs::path dst = fs::path(dst_dir) / "file";
|
||||
const std::string data = "direct I/O relocation\n";
|
||||
std::ofstream(src,std::ios::binary) << data;
|
||||
const int fd = ::open(src.c_str(),O_RDWR|O_DIRECT);
|
||||
if((fd < 0) && ((errno == EINVAL) || (errno == EOPNOTSUPP)))
|
||||
return;
|
||||
if(!TEST_CHECK(fd >= 0))
|
||||
return;
|
||||
DEFER { ::close(fd); };
|
||||
|
||||
Branches branches;
|
||||
branches.minfreespace = 0;
|
||||
if(!TEST_CHECK(branches.from_string(std::string(src_dir) + ":" + dst_dir) == 0))
|
||||
return;
|
||||
const Policy::Create ff(&Policies::Create::ff);
|
||||
const int moved = ::_movefile_fd(ff,branches,src_dir,"file",fd);
|
||||
if(!TEST_CHECK(moved >= 0))
|
||||
{
|
||||
TEST_MSG("movefile: %d",moved);
|
||||
return;
|
||||
}
|
||||
::close(moved);
|
||||
TEST_CHECK(!std::filesystem::exists(src));
|
||||
TEST_CHECK(_file_contents(dst) == data);
|
||||
const int flags = ::fcntl(fd,F_GETFL);
|
||||
TEST_CHECK((flags >= 0) && (flags & O_DIRECT));
|
||||
}
|
||||
|
||||
// moveonenospc must never pick the branch the file is already on, by
|
||||
// its own path or by another path to the same directory (a bind mount;
|
||||
// a symlink stands in for one here). The copy would be renamed over
|
||||
|
|
@ -4055,7 +4168,7 @@ _file_contents(const fs::path &path_)
|
|||
// create policy runs, so a policy that picks one branch (ff here)
|
||||
// chooses another one rather than nothing.
|
||||
void
|
||||
test_fs_movefile_and_open_same_branch()
|
||||
test_fs_movefile_same_branch()
|
||||
{
|
||||
char tmp_template[] = "/tmp/mergerfs-test-movefile-same-XXXXXX";
|
||||
if(::mkdtemp(tmp_template) == nullptr)
|
||||
|
|
@ -4115,7 +4228,7 @@ test_fs_movefile_and_open_same_branch()
|
|||
ptr->front().path = first_;
|
||||
ptr->back().path = second_;
|
||||
|
||||
return fs::movefile_and_open(ff,ptr,src,"file",fd);
|
||||
return ::_movefile_fd(ff,ptr,src,"file",fd);
|
||||
};
|
||||
|
||||
// Every branch is the source: refused, file left alone.
|
||||
|
|
@ -4158,7 +4271,7 @@ test_fs_movefile_and_open_same_branch()
|
|||
// directory but not the file yet. Given the file's own path they found
|
||||
// nothing, so the write kept failing with ENOSPC.
|
||||
void
|
||||
test_fs_movefile_and_open_existing_path_policies()
|
||||
test_fs_movefile_existing_path_policies()
|
||||
{
|
||||
char tmp_template[] = "/tmp/mergerfs-test-movefile-ep-XXXXXX";
|
||||
if(::mkdtemp(tmp_template) == nullptr)
|
||||
|
|
@ -4216,7 +4329,7 @@ test_fs_movefile_and_open_existing_path_policies()
|
|||
TEST_CHECK(branches.from_string(src.string() + ":" + other.string()) == 0);
|
||||
Branches::Ptr ptr = branches;
|
||||
|
||||
rv = fs::movefile_and_open(policy,ptr,src,relpath,fd);
|
||||
rv = ::_movefile_fd(policy,ptr,src,relpath,fd);
|
||||
TEST_CHECK(rv >= 0);
|
||||
TEST_MSG("policy: %s; path: %s; rv: %d",entry.name,relpath,rv);
|
||||
if(rv >= 0)
|
||||
|
|
@ -5080,8 +5193,9 @@ TEST_LIST =
|
|||
{"fuse_symlink_request_ownership",test_fuse_symlink_request_ownership},
|
||||
{"fs_copyfile_basic",test_fs_copyfile_basic},
|
||||
{"fs_copyfile_source_changes_cleanup_tmpfiles",test_fs_copyfile_source_changes_cleanup_tmpfiles},
|
||||
{"fs_movefile_and_open_same_branch",test_fs_movefile_and_open_same_branch},
|
||||
{"fs_movefile_and_open_existing_path_policies",test_fs_movefile_and_open_existing_path_policies},
|
||||
{"fs_movefile_same_branch",test_fs_movefile_same_branch},
|
||||
{"fs_movefile_direct_io",test_fs_movefile_direct_io},
|
||||
{"fs_movefile_existing_path_policies",test_fs_movefile_existing_path_policies},
|
||||
{"direct_handle_writepage_error",test_direct_handle_writepage_error},
|
||||
{"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts},
|
||||
{"statvfs_cache_failure_not_served_as_success",test_statvfs_cache_failure_not_served_as_success},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue