fix: make the rename-exdev fallback safe and transactional

rename-exdev (opt-in; the default is passthrough) moves a source it
cannot rename across branches into a staging tree and publishes a
symlink at the destination. This reworks that fallback so a rename
either completes or leaves both names as they were.

- Staging opens every component relative to a pinned parent with
  O_NOFOLLOW, requires a daemon-owned staging root, and refuses when
  the tree can't be trusted.
- Staged names are taken without replacement and must be free on
  every configured branch, not only those holding the source, so a
  later lookup cannot reach another rename's payload. Over-long names
  are shortened. Fallback renames are serialized.
- Destinations: a compatible existing destination is replaced, kept
  under a temporary name until every branch is published, and
  restored if a later branch fails. A destination on a read-only
  branch, or a no-create one where the link can't be created, is
  refused before anything moves. Temporary and restore names never
  replace an entry, and a restore that fails is reported.
- An ordinary rename (rename-exdev=passthrough) also refuses with
  EXDEV when the destination exists on a read-only branch that does
  not hold the source: that copy could neither be replaced nor
  removed and would shadow the renamed file there. It is never
  scheduled for removal either.
- The replacement symlink is prepared in a daemon-private directory,
  its UID and GID are verified through a pinned descriptor, and it is
  published without replacement, before any source moves. A sticky,
  world-writable destination directory under Linux protected symlinks
  is refused. Each prepared link is tracked by identity for cleanup.
- Symlink sources are recreated with their original text, owner and
  times instead of staged, since staging changes where a relative
  target resolves. Sources of different types on different branches
  are refused. A hidden source that cannot be removed fails the rename.
- libfuse re-parents the kernel's node onto the staged inode, using a
  name allocated before publication, so open handles and cached
  dentries keep working and nothing can fail after the rename commits.
- Without renameat2 (FreeBSD 14.4 and 15.0, or seccomp), regular
  files and directories use a portable layout: a fresh daemon-owned
  wrapper, access gates that mirror the source's parents with the
  source's GID and its read and execute bits only, and the payload
  directory inside the last gate. Foreign-owned parents, unsettable
  group ownership and non-trivial ACLs are refused before the source
  moves. A namespace the source never entered is removed. A portable
  directory that can't be restored stays staged with its path logged,
  and an existing directory destination is refused before a portable
  source moves: keeping it aside would need the no-replace rename the
  portable layout lacks.
- Document the fallback, its refusals and FreeBSD limits.
- TEST_rename_exdev_publication, TEST_rename_exdev_portable and
  tests.cpp cover publication rollback, shadows, collisions,
  ownership, sticky directories, mixed source types, a refused portable
  directory destination and the portable layout through a mounted
  pool. TEST_rename_exdev_publication keeps its fixture if its tmpfs
  can't be detached.

Performance: an ordinary rename only adds an lstat per read-only
branch, and only when one is configured. The fallback adds about two
lstats per branch plus the staging work, and fallback renames run one
at a time.
This commit is contained in:
Antonio SJ Musumeci 2026-11-25 13:45:42 -05:00
parent 036246140b
commit c9854f236f
16 changed files with 3962 additions and 202 deletions

View file

@ -3309,31 +3309,30 @@ test_rename_exdev_case(const char *mode_,
output << "existing destination";
}
const int rv = FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str());
TEST_CHECK(rv == (occupied_ ? -EEXIST : 0));
if(occupied_)
{
check_exdev_payload(fixture.first / fixture.source);
check_exdev_payload(fixture.second / fixture.destination,"existing destination");
check_exdev_absent(fixture.first / fixture.staged());
struct stat restored = {};
TEST_CHECK(::lstat((fixture.first / fixture.source).c_str(),&restored) == 0);
TEST_CHECK(restored.st_ino == source_st.st_ino);
return;
}
if(rv != 0)
// Like rename(2) an existing destination is replaced.
char *relocated = NULL;
const int rv = FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str(),
&relocated);
TEST_CHECK(rv == 0);
// libfuse needs to know where the source went (see relocate_node()).
TEST_CHECK(relocated != NULL);
const fs::path staged(relocated ? relocated : "");
::free(relocated);
if((rv != 0) || staged.empty())
return;
TEST_CHECK(staged.is_relative());
TEST_CHECK(*staged.begin() == ".mergerfs_rename_exdev");
check_exdev_absent(fixture.first / fixture.source);
check_exdev_absent(fixture.first / fixture.destination);
check_exdev_payload(fixture.first / fixture.staged());
check_exdev_payload(fixture.first / staged);
struct stat staged_st = {};
TEST_CHECK(::lstat((fixture.first / fixture.staged()).c_str(),&staged_st) == 0);
TEST_CHECK(::lstat((fixture.first / staged).c_str(),&staged_st) == 0);
TEST_CHECK(staged_st.st_ino == source_st.st_ino);
TEST_CHECK(staged_st.st_uid == source_st.st_uid);
TEST_CHECK(staged_st.st_gid == source_st.st_gid);
const fs::path expected = (std::string(mode_) == "rel-symlink") ?
fixture.staged().lexically_relative(fixture.destination.parent_path()) :
cfg.mountpoint / fixture.staged();
staged.lexically_relative(fixture.destination.parent_path()) :
cfg.mountpoint / staged;
check_exdev_symlink(fixture.second / fixture.destination,expected,source_st);
}
@ -3429,15 +3428,191 @@ test_fuse_link_exdev_invalid_context()
test_link_exdev_case(mode,fields);
}
static
void
test_rename_exdev_incompatible_destination(const char *mode_,
const bool nonempty_)
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode_);
cfg.rename_exdev.from_string(mode_);
const auto ctx = exdev_context(0);
const auto dst = fixture.second / fixture.destination;
TEST_CHECK(::mkdir(dst.c_str(),0755) == 0);
if(nonempty_)
{
std::ofstream output(dst / "child");
output << "child";
}
// A file can't replace a directory. The fallback must say so before
// touching the source.
const int rv = FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str());
TEST_CHECK(rv == -EISDIR);
check_exdev_payload(fixture.first / fixture.source);
check_exdev_absent(fixture.first / ".mergerfs_rename_exdev");
struct stat st = {};
TEST_CHECK(::lstat(dst.c_str(),&st) == 0);
TEST_CHECK(S_ISDIR(st.st_mode));
}
static
void
test_rename_exdev_dir_over_nonempty_dir(const char *mode_)
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode_);
cfg.rename_exdev.from_string(mode_);
const auto ctx = exdev_context(0);
const fs::path srcdir = fixture.source.parent_path();
const fs::path dstdir = fixture.destination.parent_path() / "dir";
TEST_CHECK(::mkdir((fixture.second / dstdir).c_str(),0755) == 0);
{
std::ofstream output(fixture.second / dstdir / "child");
output << "child";
}
const int rv = FUSE::rename(&ctx,srcdir.c_str(),dstdir.c_str());
TEST_CHECK(rv == -ENOTEMPTY);
check_exdev_payload(fixture.first / fixture.source);
check_exdev_payload(fixture.second / dstdir / "child","child");
check_exdev_absent(fixture.first / ".mergerfs_rename_exdev");
}
void
test_fuse_exdev_existing_destination()
{
for(const auto mode : {"rel-symlink","abs-symlink"})
test_rename_exdev_case(mode,false,3,true);
{
test_rename_exdev_case(mode,false,3,true);
test_rename_exdev_incompatible_destination(mode,false);
test_rename_exdev_incompatible_destination(mode,true);
test_rename_exdev_dir_over_nonempty_dir(mode);
}
for(const auto mode : {"rel-symlink","abs-base-symlink","abs-pool-symlink"})
test_link_exdev_case(mode,3,true);
}
static
fs::path
exdev_link_branch_target(const ExdevFixture &fixture_,
const fs::path &link_)
{
std::error_code ec;
const auto target = std::filesystem::read_symlink(fixture_.second / link_,ec);
TEST_CHECK(!ec);
if(target.is_absolute())
return fixture_.first / target.lexically_relative(cfg.mountpoint);
return fixture_.first / (link_.parent_path() / target).lexically_normal();
}
void
test_fuse_rename_exdev_staging_collision()
{
// A second EXDEV rename of the same source path must not replace the
// file staged by the first one.
for(const auto mode : {"rel-symlink","abs-symlink"})
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode);
cfg.rename_exdev.from_string(mode);
const auto ctx = exdev_context(0);
const fs::path second_destination = "destination/nested/second";
TEST_CHECK(FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str()) == 0);
{
std::ofstream output(fixture.first / fixture.source);
output << "second payload";
}
TEST_CHECK(FUSE::rename(&ctx,fixture.source.c_str(),second_destination.c_str()) == 0);
const auto first_staged = exdev_link_branch_target(fixture,fixture.destination);
const auto second_staged = exdev_link_branch_target(fixture,second_destination);
TEST_CHECK(first_staged != second_staged);
check_exdev_payload(first_staged);
check_exdev_payload(second_staged,"second payload");
check_exdev_absent(fixture.first / fixture.source);
}
}
void
test_fuse_rename_exdev_symlink_source()
{
// Renaming a symlink keeps its content. Staging it one directory
// deeper would break a relative target, so it is recreated instead.
for(const auto mode : {"rel-symlink","abs-symlink"})
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode);
cfg.rename_exdev.from_string(mode);
const auto ctx = exdev_context(0);
const auto src = fixture.first / fixture.source;
TEST_CHECK(::unlink(src.c_str()) == 0);
TEST_CHECK(::symlink("../relative/target",src.c_str()) == 0);
struct stat source_st = {};
TEST_CHECK(::lstat(src.c_str(),&source_st) == 0);
char *relocated = NULL;
const int rv = FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str(),
&relocated);
TEST_CHECK(rv == 0);
// The symlink was recreated at newpath, so the node moves normally.
TEST_CHECK(relocated == NULL);
::free(relocated);
check_exdev_absent(src);
check_exdev_absent(fixture.first / ".mergerfs_rename_exdev");
check_exdev_symlink(fixture.second / fixture.destination,
"../relative/target",
source_st);
struct stat st = {};
TEST_CHECK(::lstat((fixture.second / fixture.destination).c_str(),&st) == 0);
TEST_CHECK(st.st_mtim.tv_sec == source_st.st_mtim.tv_sec);
TEST_CHECK(st.st_mtim.tv_nsec == source_st.st_mtim.tv_nsec);
}
}
void
test_fuse_rename_exdev_symlink_source_unremovable()
{
// A source symlink whose name can't be removed must fail the rename
// rather than leave both the old and the new name in place.
if(::geteuid() == 0)
return; // Directory permissions don't restrict root.
for(const auto mode : {"rel-symlink","abs-symlink"})
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode);
cfg.rename_exdev.from_string(mode);
const auto ctx = exdev_context(0);
const auto src = fixture.first / fixture.source;
const auto srcdir = src.parent_path();
const auto dstdir = fixture.second / fixture.destination.parent_path();
TEST_CHECK(::unlink(src.c_str()) == 0);
TEST_CHECK(::symlink("../relative/target",src.c_str()) == 0);
TEST_CHECK(::chmod(srcdir.c_str(),0555) == 0);
DEFER { ::chmod(srcdir.c_str(),0755); };
const int rv = FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str());
TEST_CHECK(rv == -EACCES);
TEST_MSG("rv=%d",rv);
std::error_code ec;
TEST_CHECK(std::filesystem::read_symlink(src,ec) == "../relative/target");
TEST_CHECK(!ec);
TEST_CHECK(std::filesystem::is_empty(dstdir,ec));
TEST_CHECK(!ec);
}
}
static
bool
exdev_distinct_ownership_available()
@ -3498,6 +3673,150 @@ test_fuse_exdev_source_ownership()
test_link_exdev_case(mode,fields,false,true);
}
void
test_fuse_exdev_unavailable_owner_staging()
{
ExdevFixture fixture;
if(!fixture.ready)
return;
cfg.link_exdev.from_string("rel-symlink");
const auto ctx = exdev_context(0);
const auto blocked = fixture.second / ".mergerfs_symlink_exdev";
std::ofstream(blocked) << "occupied";
struct stat st = {};
fuse_timeouts_t timeouts = {};
TEST_CHECK(FUSE::link(&ctx,fixture.source.c_str(),fixture.destination.c_str(),
&st,&timeouts) == -EXDEV);
check_exdev_absent(fixture.second / fixture.destination);
check_exdev_payload(blocked,"occupied");
check_exdev_payload(fixture.first / fixture.source);
}
void
test_fuse_exdev_sticky_destination()
{
// A replacement symlink owned by the source owner in a sticky,
// world-writable directory owned by someone else could not be
// followed by anyone else (fs.protected_symlinks), so the fallbacks
// must not make one.
if(!exdev_distinct_ownership_available())
return;
{
std::ifstream sysctl("/proc/sys/fs/protected_symlinks");
char c = '1';
if(sysctl.is_open() && sysctl.get(c) && (c == '0'))
return;
}
for(const auto mode : {"rel-symlink","abs-symlink"})
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode);
cfg.rename_exdev.from_string(mode);
const auto ctx = exdev_context(0);
const auto parent = fixture.second / fixture.destination.parent_path();
TEST_CHECK(::chown((fixture.first / fixture.source).c_str(),12345,23456) == 0);
TEST_CHECK(::chown(parent.c_str(),0,0) == 0);
TEST_CHECK(::chmod(parent.c_str(),01777) == 0);
TEST_CHECK(FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str()) == -EXDEV);
check_exdev_payload(fixture.first / fixture.source);
// Refused before the source branch is touched at all.
check_exdev_absent(fixture.first / ".mergerfs_rename_exdev");
check_exdev_absent(fixture.second / fixture.destination);
}
for(const auto mode : {"rel-symlink","abs-base-symlink","abs-pool-symlink"})
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode);
cfg.link_exdev.from_string(mode);
const auto ctx = exdev_context(0);
const auto parent = fixture.second / fixture.destination.parent_path();
TEST_CHECK(::chown((fixture.first / fixture.source).c_str(),12345,23456) == 0);
TEST_CHECK(::chown(parent.c_str(),0,0) == 0);
TEST_CHECK(::chmod(parent.c_str(),01777) == 0);
struct stat output_st = {};
fuse_timeouts_t timeouts = {};
TEST_CHECK(FUSE::link(&ctx,fixture.source.c_str(),fixture.destination.c_str(),
&output_st,&timeouts) == -EXDEV);
check_exdev_absent(fixture.second / fixture.destination);
}
}
void
test_fuse_exdev_sticky_destination_all_targets()
{
// The source lives on a third branch so its missing destination parent
// still forces EXDEV. The first two branches both have destination
// parents: a sticky one followed by a viable one. Preflight must refuse
// the whole operation rather than publish on the later branch.
if(!exdev_distinct_ownership_available())
return;
{
std::ifstream sysctl("/proc/sys/fs/protected_symlinks");
char c = '1';
if(sysctl.is_open() && sysctl.get(c) && (c == '0'))
return;
}
ExdevFixture fixture;
if(!fixture.ready)
return;
const fs::path third = fixture.root / "third";
std::error_code ec;
std::filesystem::create_directories(third / fixture.source.parent_path(),ec);
if(!TEST_CHECK(!ec))
return;
std::filesystem::create_directories(fixture.first / fixture.destination.parent_path(),ec);
if(!TEST_CHECK(!ec))
return;
TEST_CHECK(::rename((fixture.first / fixture.source).c_str(),
(third / fixture.source).c_str()) == 0);
TEST_CHECK(::chown((third / fixture.source).c_str(),12345,23456) == 0);
TEST_CHECK(cfg.branches.from_string(fixture.first.string() + ":" +
fixture.second.string() + ":" +
third.string()) == 0);
cfg.func.symlink.from_string("epall");
const auto sticky = fixture.first / fixture.destination.parent_path();
const auto viable = fixture.second / fixture.destination.parent_path();
TEST_CHECK(::chown(sticky.c_str(),0,0) == 0);
TEST_CHECK(::chmod(sticky.c_str(),01777) == 0);
const auto ctx = exdev_context(0);
cfg.rename_exdev.from_string("rel-symlink");
int rv = FUSE::rename(&ctx,fixture.source.c_str(),fixture.destination.c_str());
TEST_CHECK(rv == -EXDEV);
if(rv != -EXDEV)
return;
check_exdev_payload(third / fixture.source);
check_exdev_absent(third / ".mergerfs_rename_exdev");
check_exdev_absent(fixture.first / fixture.destination);
check_exdev_absent(fixture.second / fixture.destination);
TEST_CHECK(std::filesystem::is_empty(sticky,ec));
TEST_CHECK(!ec);
TEST_CHECK(std::filesystem::is_empty(viable,ec));
TEST_CHECK(!ec);
cfg.link_exdev.from_string("rel-symlink");
struct stat output_st = {};
fuse_timeouts_t timeouts = {};
rv = FUSE::link(&ctx,fixture.source.c_str(),fixture.destination.c_str(),
&output_st,&timeouts);
TEST_CHECK(rv == -EXDEV);
if(rv != -EXDEV)
return;
check_exdev_absent(fixture.first / fixture.destination);
check_exdev_absent(fixture.second / fixture.destination);
TEST_CHECK(std::filesystem::is_empty(sticky,ec));
TEST_CHECK(!ec);
TEST_CHECK(std::filesystem::is_empty(viable,ec));
TEST_CHECK(!ec);
}
void
test_fuse_symlink_request_ownership()
{
@ -4703,7 +5022,13 @@ TEST_LIST =
{"fuse_link_exdev_variants",test_fuse_link_exdev_variants},
{"fuse_link_exdev_invalid_context",test_fuse_link_exdev_invalid_context},
{"fuse_exdev_existing_destination",test_fuse_exdev_existing_destination},
{"fuse_rename_exdev_staging_collision",test_fuse_rename_exdev_staging_collision},
{"fuse_rename_exdev_symlink_source",test_fuse_rename_exdev_symlink_source},
{"fuse_rename_exdev_symlink_source_unremovable",test_fuse_rename_exdev_symlink_source_unremovable},
{"fuse_exdev_source_ownership",test_fuse_exdev_source_ownership},
{"fuse_exdev_unavailable_owner_staging",test_fuse_exdev_unavailable_owner_staging},
{"fuse_exdev_sticky_destination",test_fuse_exdev_sticky_destination},
{"fuse_exdev_sticky_destination_all_targets",test_fuse_exdev_sticky_destination_all_targets},
{"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},