mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 21:00:13 -05:00
fix: pick the moveonenospc source and destination correctly
- moveonenospc copied from the branch stored when the file was opened, which a move never updates, so a second ENOSPC on the same handle tried to copy a path that no longer existed. Find the source branch from the fd's device and inode at the start of each move, trying the stored branch first. No match leaves the write's ENOSPC in place. - The source branch was dropped from the create policy's result, so a policy returning one branch (ff, mfs, lfs, lus, pfrd) left nothing when it picked the source, and a destination that was the source by another path (bind mount) was renamed over the file and unlinked. Remove every alias of the source before the policy runs, so it picks another branch; with no candidate left the move is refused. - Refuse relocation if a duplicate on another branch could become visible once the source is removed, and publish without replacing a destination that appeared meanwhile. When the no-replace rename is unsupported, publish with link(); a staging name that cannot be removed afterwards is logged instead of silently left behind. - Ask the create policy by the parent directory, as create does. The existing-path policies and newest only pick a branch that has the path they are given, and the destination has the directory but not the file yet. - TEST_movefile_enospc moves a handle twice and checks duplicates through the pool. tests.cpp covers the source by its own path and an alias, and every existing-path policy for nested and root files.
This commit is contained in:
parent
d1be8ac1db
commit
61597c8dfd
9 changed files with 558 additions and 78 deletions
198
tests/tests.cpp
198
tests/tests.cpp
|
|
@ -4,6 +4,7 @@
|
|||
#include "fs_clonepath.hpp"
|
||||
#include "fs_copyfile.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_movefile_and_open.hpp"
|
||||
#include "from_string.hpp"
|
||||
#include "fuse_link.hpp"
|
||||
#include "fuse_rename.hpp"
|
||||
|
|
@ -3666,6 +3667,201 @@ test_fs_copyfile_source_changes_cleanup_tmpfiles()
|
|||
std::filesystem::remove_all(tmp_dir);
|
||||
}
|
||||
|
||||
static
|
||||
std::string
|
||||
_file_contents(const fs::path &path_)
|
||||
{
|
||||
std::ifstream stream(path_,std::ios::binary);
|
||||
std::ostringstream contents;
|
||||
|
||||
contents << stream.rdbuf();
|
||||
|
||||
return contents.str();
|
||||
}
|
||||
|
||||
// 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
|
||||
// the file and then unlinked. Such branches are removed before the
|
||||
// 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()
|
||||
{
|
||||
char tmp_template[] = "/tmp/mergerfs-test-movefile-same-XXXXXX";
|
||||
if(::mkdtemp(tmp_template) == nullptr)
|
||||
{
|
||||
TEST_CHECK(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fs::path tmp_dir = tmp_template;
|
||||
const fs::path src = tmp_dir / "src";
|
||||
const fs::path alias = tmp_dir / "alias";
|
||||
const fs::path other = tmp_dir / "other";
|
||||
const std::string data = "moveonenospc same branch\n";
|
||||
const Policy::Create ff(&Policies::Create::ff);
|
||||
std::error_code ec;
|
||||
struct stat before;
|
||||
struct stat after;
|
||||
int fd;
|
||||
int rv;
|
||||
|
||||
std::filesystem::create_directory(src,ec);
|
||||
if(!ec)
|
||||
std::filesystem::create_directory(other,ec);
|
||||
if(!ec)
|
||||
std::filesystem::create_directory_symlink(src,alias,ec);
|
||||
TEST_CHECK(!ec);
|
||||
if(ec)
|
||||
{
|
||||
std::filesystem::remove_all(tmp_dir);
|
||||
return;
|
||||
}
|
||||
|
||||
auto reset = [&]()
|
||||
{
|
||||
std::filesystem::remove(other / "file",ec);
|
||||
std::ofstream(src / "file",std::ios::binary|std::ios::trunc) << data;
|
||||
fd = ::open((src / "file").c_str(),O_RDWR);
|
||||
if(!TEST_CHECK(fd >= 0))
|
||||
return false;
|
||||
if(!TEST_CHECK(::fstat(fd,&before) == 0))
|
||||
{
|
||||
::close(fd);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// Branches resolves symlinks while parsing, so the paths are set
|
||||
// afterwards.
|
||||
auto move = [&](const fs::path &first_,
|
||||
const fs::path &second_)
|
||||
{
|
||||
Branches branches;
|
||||
branches.minfreespace = 0;
|
||||
TEST_CHECK(branches.from_string(src.string() + ":" + other.string()) == 0);
|
||||
Branches::Ptr ptr = branches;
|
||||
ptr->front().path = first_;
|
||||
ptr->back().path = second_;
|
||||
|
||||
return fs::movefile_and_open(ff,ptr,src,"file",fd);
|
||||
};
|
||||
|
||||
// Every branch is the source: refused, file left alone.
|
||||
if(reset())
|
||||
{
|
||||
rv = move(src,alias);
|
||||
TEST_CHECK(rv == -ENOSPC);
|
||||
TEST_MSG("rv: %d",rv);
|
||||
if(rv >= 0)
|
||||
::close(rv);
|
||||
|
||||
if(TEST_CHECK(::stat((src / "file").c_str(),&after) == 0))
|
||||
TEST_CHECK(after.st_ino == before.st_ino);
|
||||
TEST_CHECK(_file_contents(src / "file") == data);
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
// ff would pick the source listed first; it picks `other` instead.
|
||||
for(const auto &first : {src,alias,other})
|
||||
{
|
||||
if(!reset())
|
||||
break;
|
||||
|
||||
rv = move(first,((first == other) ? src : other));
|
||||
TEST_CHECK(rv >= 0);
|
||||
TEST_MSG("first branch: %s; rv: %d",first.c_str(),rv);
|
||||
if(rv >= 0)
|
||||
::close(rv);
|
||||
TEST_CHECK(!std::filesystem::exists(src / "file"));
|
||||
TEST_CHECK(_file_contents(other / "file") == data);
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
std::filesystem::remove_all(tmp_dir);
|
||||
}
|
||||
|
||||
// The create policy is asked where the file goes, as create does, by
|
||||
// its parent directory. The existing path policies (ep*) only choose a
|
||||
// branch that already has the path, and the destination has the
|
||||
// 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()
|
||||
{
|
||||
char tmp_template[] = "/tmp/mergerfs-test-movefile-ep-XXXXXX";
|
||||
if(::mkdtemp(tmp_template) == nullptr)
|
||||
{
|
||||
TEST_CHECK(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fs::path tmp_dir = tmp_template;
|
||||
const fs::path src = tmp_dir / "src";
|
||||
const fs::path other = tmp_dir / "other";
|
||||
const std::string data = "moveonenospc existing path policy\n";
|
||||
const struct
|
||||
{
|
||||
const char *name;
|
||||
Policy::CreateImpl *policy;
|
||||
} policies[] = {
|
||||
{"epall",&Policies::Create::epall},
|
||||
{"epff",&Policies::Create::epff},
|
||||
{"eplfs",&Policies::Create::eplfs},
|
||||
{"eplus",&Policies::Create::eplus},
|
||||
{"epmfs",&Policies::Create::epmfs},
|
||||
{"eppfrd",&Policies::Create::eppfrd},
|
||||
{"eprand",&Policies::Create::eprand},
|
||||
{"newest",&Policies::Create::newest},
|
||||
};
|
||||
std::error_code ec;
|
||||
|
||||
std::filesystem::create_directories(src / "dir",ec);
|
||||
TEST_CHECK(!ec);
|
||||
if(ec)
|
||||
{
|
||||
std::filesystem::remove_all(tmp_dir);
|
||||
return;
|
||||
}
|
||||
|
||||
// A file in a directory, and one at the root, whose parent is empty.
|
||||
for(const char *relpath : {"dir/file","rootfile"})
|
||||
for(const auto &entry : policies)
|
||||
{
|
||||
int fd;
|
||||
int rv;
|
||||
Branches branches;
|
||||
const Policy::Create policy(entry.policy);
|
||||
|
||||
std::filesystem::remove_all(other,ec);
|
||||
std::filesystem::create_directories(other / "dir",ec);
|
||||
std::ofstream(src / relpath,std::ios::binary|std::ios::trunc) << data;
|
||||
fd = ::open((src / relpath).c_str(),O_RDWR);
|
||||
TEST_CHECK(fd >= 0);
|
||||
if(fd < 0)
|
||||
continue;
|
||||
|
||||
branches.minfreespace = 0;
|
||||
TEST_CHECK(branches.from_string(src.string() + ":" + other.string()) == 0);
|
||||
Branches::Ptr ptr = branches;
|
||||
|
||||
rv = fs::movefile_and_open(policy,ptr,src,relpath,fd);
|
||||
TEST_CHECK(rv >= 0);
|
||||
TEST_MSG("policy: %s; path: %s; rv: %d",entry.name,relpath,rv);
|
||||
if(rv >= 0)
|
||||
::close(rv);
|
||||
|
||||
TEST_CHECK(!std::filesystem::exists(src / relpath));
|
||||
TEST_CHECK(_file_contents(other / relpath) == data);
|
||||
TEST_MSG("policy: %s; path: %s",entry.name,relpath);
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
std::filesystem::remove_all(tmp_dir);
|
||||
}
|
||||
|
||||
void
|
||||
test_hashset_put_and_size()
|
||||
{
|
||||
|
|
@ -4410,6 +4606,8 @@ 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},
|
||||
{"str_eq_nullptr",test_str_eq_nullptr},
|
||||
{"str_startswith_char_nullptr",test_str_startswith_char_nullptr},
|
||||
{"str_from_u64_suffixes",test_str_from_u64_suffixes},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue