fix: relocate from the fd's current branch on repeated ENOSPC

- 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 and failed with ENOSPC.
- Find the source branch from the fd at the start of each move, as
  before e216a81a. findonfs now matches inode as well as device, so
  branches on one filesystem are told apart, and tries the stored
  branch first. No match leaves the write's ENOSPC in place.
- Drop destinations that are the source directory, matched by device
  and inode so another path to it counts. The copy was renamed over
  the file and then unlinked, deleting it while the write reported
  success. A first move could already pick the source (EDQUOT with
  free space); finding the real source let later moves do it too.
  With no destination left the write keeps its error.
- TEST_movefile_enospc adds a handle that has to move twice.
  tests.cpp covers a move onto the source branch by its own path
  and by an alias.
This commit is contained in:
Antonio SJ Musumeci 2026-11-18 23:45:00 -05:00
parent 9d12bcf235
commit b885f1ee39
5 changed files with 281 additions and 59 deletions

View file

@ -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,100 @@ 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's create policy can 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, so such a move must be refused.
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);
if(!ec)
std::ofstream(src / "file",std::ios::binary) << data;
fd = ::open((src / "file").c_str(),O_RDWR);
TEST_CHECK(!ec && (fd >= 0) && (::fstat(fd,&before) == 0));
if(ec || (fd < 0))
{
std::filesystem::remove_all(tmp_dir);
return;
}
// ff picks the first branch. Branches resolves symlinks while
// parsing, so the first path is set afterwards.
auto move = [&](const std::string &list_,
const fs::path &first_)
{
Branches branches;
branches.minfreespace = 0;
TEST_CHECK(branches.from_string(list_) == 0);
Branches::Ptr ptr = branches;
ptr->front().path = first_;
return fs::movefile_and_open(ff,ptr,src,"file",fd);
};
for(const auto &first : {src,alias})
{
rv = move(src.string() + ":" + other.string(),first);
TEST_CHECK(rv == -ENOSPC);
TEST_MSG("first branch: %s; rv: %d",first.c_str(),rv);
if(rv >= 0)
::close(rv);
TEST_CHECK(::stat((src / "file").c_str(),&after) == 0);
TEST_CHECK(after.st_ino == before.st_ino);
TEST_CHECK(_file_contents(src / "file") == data);
TEST_CHECK(!std::filesystem::exists(other / "file"));
}
// Control: a different first branch still relocates.
rv = move(other.string() + ":" + src.string(),other);
TEST_CHECK(rv >= 0);
TEST_MSG("rv: %d",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);
}
void
test_hashset_put_and_size()
{
@ -4372,6 +4467,7 @@ 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},
{"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},