fix: preserve source ownership in EXDEV symlink fallbacks

This commit is contained in:
Antonio SJ Musumeci 2026-10-28 12:12:38 -05:00 • committed by trapexit
parent 7b7e96bbc5
commit b10fb10590
9 changed files with 473 additions and 166 deletions

View file

@ -12,6 +12,14 @@ the newpath. The target value is determined by the value of
* `link-exdev=abs-pool-symlink`: An absolute value using the mergerfs
mount point.
* Defaults to `passthrough`.
Replacement symlinks retain the source inode's UID and GID, read without
following a source symlink. Relative and pool targets use the first
`getattr`-policy source branch; absolute branch targets use the source
selected by the `open` policy. Creation uses daemon credentials, then
sets the source ownership, rather than using the link request's UID/GID,
which may be invalid when `allow-idmap` is enabled. A newly created
replacement is removed if its ownership cannot be set.
**NOTE:** It is possible that some applications check the file they
link. In those cases, it is possible it will error or complain.

View file

@ -18,6 +18,15 @@ The staging directory is created on demand, including for files at the
pool root. Relative targets are calculated within the pool's relative
path namespace.
Replacement symlinks retain the UID and GID of the first source inode
selected by the rename action policy, read before moving the source and
without following source symlinks. Creation uses daemon credentials,
then sets the source ownership; it does not use the rename request's
UID/GID, which may be invalid when `allow-idmap` is enabled. A newly
created replacement is removed if its ownership cannot be set. If
replacement creation fails, mergerfs attempts to restore the original
source paths.
**NOTE:** It is possible that some applications check the file they
rename. In those cases it is possible it will error or complain.

View file

@ -261,6 +261,12 @@ As of v2.42.0 mergerfs now runs as root more generally. Either
changing credentials when creating files (Linux) or chown'ing them
after creation (FreeBSD).
The optional EXDEV link and rename fallbacks create replacement
symlinks under daemon credentials and then preserve the source inode's
UID and GID. They do not use the link or rename request's UID/GID:
these may be invalid with `allow-idmap`. Ordinary symlink creation
continues to use the creation request's credentials.
As a result of this change it is now necessary for the FUSE
`default_permissions` feature be used for proper entitlements
management. mergerfs does allow it to be disabled but it should only

View file

@ -194,11 +194,10 @@ _link(const fs::path &oldpath_,
static
int
_link(const fuse_req_ctx_t *ctx_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
_link(const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
@ -211,11 +210,11 @@ _link(const fuse_req_ctx_t *ctx_,
static
int
_link_exdev_rel_symlink(const fuse_req_ctx_t *ctx_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
_link_exdev_rel_symlink(const ugid_t owner_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
fs::path target(oldpath_);
@ -223,9 +222,9 @@ _link_exdev_rel_symlink(const fuse_req_ctx_t *ctx_,
target = target.lexically_relative(linkpath.parent_path());
rv = FUSE::symlink(ctx_,target.c_str(),linkpath);
rv = FUSE::symlink_owned(owner_,target.c_str(),linkpath);
if(rv == 0)
rv = FUSE::getattr(ctx_,oldpath_,st_,timeouts_);
rv = FUSE::getattr(oldpath_,st_,timeouts_);
// Disable caching since we created a symlink but should be a regular.
timeouts_->attr = 0;
@ -236,29 +235,18 @@ _link_exdev_rel_symlink(const fuse_req_ctx_t *ctx_,
static
int
_link_exdev_abs_base_symlink(const fuse_req_ctx_t *ctx_,
const Policy::Search &openPolicy_,
const Branches::Ptr ibranches_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
_link_exdev_abs_base_symlink(const ugid_t owner_,
const fs::path &target_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
fs::path target;
std::vector<Branch*> obranches;
rv = openPolicy_(ibranches_,oldpath_,obranches);
if(rv < 0)
return rv;
if(obranches.empty())
return -ENOENT;
target = obranches[0]->path / oldpath_;
rv = FUSE::symlink(ctx_,target.c_str(),newpath_);
rv = FUSE::symlink_owned(owner_,target_.c_str(),newpath_);
if(rv == 0)
rv = FUSE::getattr(ctx_,oldpath_,st_,timeouts_);
rv = FUSE::getattr(oldpath_,st_,timeouts_);
// Disable caching since we created a symlink but should be a regular.
timeouts_->attr = 0;
@ -269,22 +257,21 @@ _link_exdev_abs_base_symlink(const fuse_req_ctx_t *ctx_,
static
int
_link_exdev_abs_pool_symlink(const fuse_req_ctx_t *ctx_,
const fs::path &mount_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
_link_exdev_abs_pool_symlink(const ugid_t owner_,
const fs::path &mount_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
StrVec basepaths;
fs::path target;
target = mount_ / oldpath_;
rv = FUSE::symlink(ctx_,target.c_str(),newpath_);
rv = FUSE::symlink_owned(owner_,target.c_str(),newpath_);
if(rv == 0)
rv = FUSE::getattr(ctx_,oldpath_,st_,timeouts_);
rv = FUSE::getattr(oldpath_,st_,timeouts_);
// Disable caching since we created a symlink but should be a regular.
timeouts_->attr = 0;
@ -295,32 +282,62 @@ _link_exdev_abs_pool_symlink(const fuse_req_ctx_t *ctx_,
static
int
_link_exdev(const fuse_req_ctx_t *ctx_,
const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
_link_exdev(const fs::path &oldpath_,
const fs::path &newpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
struct stat source;
fs::path oldfullpath;
std::vector<Branch*> oldbranches;
const Policy::Search *sourcePolicy;
switch(cfg.link_exdev)
{
case LinkEXDEV::ENUM::REL_SYMLINK:
case LinkEXDEV::ENUM::ABS_POOL_SYMLINK:
sourcePolicy = &cfg.func.getattr.policy;
break;
case LinkEXDEV::ENUM::ABS_BASE_SYMLINK:
sourcePolicy = &cfg.func.open.policy;
break;
default:
return -EXDEV;
}
rv = (*sourcePolicy)(cfg.branches,oldpath_,oldbranches);
if(rv < 0)
return rv;
if(oldbranches.empty())
return -ENOENT;
oldfullpath = oldbranches[0]->path / oldpath_;
rv = fs::lstat(oldfullpath,&source);
if(rv < 0)
return rv;
const ugid_t owner{source.st_uid,source.st_gid};
switch(cfg.link_exdev)
{
case LinkEXDEV::ENUM::PASSTHROUGH:
return -EXDEV;
case LinkEXDEV::ENUM::REL_SYMLINK:
return ::_link_exdev_rel_symlink(ctx_,
return ::_link_exdev_rel_symlink(owner,
oldpath_,
newpath_,
st_,
timeouts_);
case LinkEXDEV::ENUM::ABS_BASE_SYMLINK:
return ::_link_exdev_abs_base_symlink(ctx_,
cfg.func.open.policy,
cfg.branches,
return ::_link_exdev_abs_base_symlink(owner,
oldfullpath,
oldpath_,
newpath_,
st_,
timeouts_);
case LinkEXDEV::ENUM::ABS_POOL_SYMLINK:
return ::_link_exdev_abs_pool_symlink(ctx_,
return ::_link_exdev_abs_pool_symlink(owner,
cfg.mountpoint,
oldpath_,
newpath_,
@ -342,9 +359,9 @@ FUSE::link(const fuse_req_ctx_t *ctx_,
const fs::path oldpath{oldpath_};
const fs::path newpath{newpath_};
rv = ::_link(ctx_,oldpath,newpath,st_,timeouts_);
rv = ::_link(oldpath,newpath,st_,timeouts_);
if(rv == -EXDEV)
rv = ::_link_exdev(ctx_,oldpath,newpath,st_,timeouts_);
rv = ::_link_exdev(oldpath,newpath,st_,timeouts_);
return rv;
}

View file

@ -23,6 +23,7 @@
#include "errno.hpp"
#include "fs_clonepath.hpp"
#include "fs_link.hpp"
#include "fs_lstat.hpp"
#include "fs_mkdir_as.hpp"
#include "fs_path.hpp"
#include "fs_remove.hpp"
@ -204,13 +205,20 @@ int
_rename_exdev_rename_target(const Policy::Action &actionPolicy_,
const Branches::Ptr ibranches_,
const fs::path &oldfusepath_,
std::vector<Branch*> &obranches_)
std::vector<Branch*> &obranches_,
struct stat &source_)
{
int rv;
fs::path clonesrc;
fs::path clonedst;
rv = actionPolicy_(ibranches_,oldfusepath_,obranches_);
if(rv < 0)
return rv;
if(obranches_.empty())
return -ENOENT;
rv = fs::lstat(obranches_[0]->path / oldfusepath_,&source_);
if(rv < 0)
return rv;
@ -247,18 +255,22 @@ _rename_exdev_rename_target(const Policy::Action &actionPolicy_,
static
int
_rename_exdev_rel_symlink(const fuse_req_ctx_t *ctx_,
const Policy::Action &actionPolicy_,
_rename_exdev_rel_symlink(const Policy::Action &actionPolicy_,
const Branches::Ptr branches_,
const fs::path &oldfusepath_,
const fs::path &newfusepath_)
{
int rv;
struct stat source;
fs::path target;
fs::path linkpath;
std::vector<Branch*> branches;
rv = ::_rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
rv = ::_rename_exdev_rename_target(actionPolicy_,
branches_,
oldfusepath_,
branches,
source);
if(rv < 0)
return rv;
@ -267,7 +279,7 @@ _rename_exdev_rel_symlink(const fuse_req_ctx_t *ctx_,
target /= oldfusepath_;
target = target.lexically_relative(linkpath.parent_path());
rv = FUSE::symlink(ctx_,target.c_str(),linkpath);
rv = FUSE::symlink_owned({source.st_uid,source.st_gid},target.c_str(),linkpath);
if(rv < 0)
::_rename_exdev_rename_back(branches,oldfusepath_);
@ -276,19 +288,23 @@ _rename_exdev_rel_symlink(const fuse_req_ctx_t *ctx_,
static
int
_rename_exdev_abs_symlink(const fuse_req_ctx_t *ctx_,
const Policy::Action &actionPolicy_,
_rename_exdev_abs_symlink(const Policy::Action &actionPolicy_,
const Branches::Ptr branches_,
const fs::path &mount_,
const fs::path &oldfusepath_,
const fs::path &newfusepath_)
{
int rv;
struct stat source;
fs::path target;
fs::path linkpath;
std::vector<Branch*> branches;
rv = ::_rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,branches);
rv = ::_rename_exdev_rename_target(actionPolicy_,
branches_,
oldfusepath_,
branches,
source);
if(rv < 0)
return rv;
@ -297,7 +313,7 @@ _rename_exdev_abs_symlink(const fuse_req_ctx_t *ctx_,
target /= ".mergerfs_rename_exdev";
target /= oldfusepath_;
rv = FUSE::symlink(ctx_,target.c_str(),linkpath);
rv = FUSE::symlink_owned({source.st_uid,source.st_gid},target.c_str(),linkpath);
if(rv < 0)
::_rename_exdev_rename_back(branches,oldfusepath_);
@ -306,23 +322,20 @@ _rename_exdev_abs_symlink(const fuse_req_ctx_t *ctx_,
static
int
_rename_exdev(const fuse_req_ctx_t *ctx_,
const fs::path &oldfusepath_,
const fs::path &newfusepath_)
_rename_exdev(const fs::path &oldfusepath_,
const fs::path &newfusepath_)
{
switch(cfg.rename_exdev)
{
case RenameEXDEV::ENUM::PASSTHROUGH:
return -EXDEV;
case RenameEXDEV::ENUM::REL_SYMLINK:
return ::_rename_exdev_rel_symlink(ctx_,
cfg.func.rename.policy,
return ::_rename_exdev_rel_symlink(cfg.func.rename.policy,
cfg.branches,
oldfusepath_,
newfusepath_);
case RenameEXDEV::ENUM::ABS_SYMLINK:
return ::_rename_exdev_abs_symlink(ctx_,
cfg.func.rename.policy,
return ::_rename_exdev_abs_symlink(cfg.func.rename.policy,
cfg.branches,
cfg.mountpoint,
oldfusepath_,
@ -361,7 +374,7 @@ FUSE::rename(const fuse_req_ctx_t *ctx_,
rv = ::_rename(oldfusepath,newfusepath);
if(rv == -EXDEV)
return ::_rename_exdev(ctx_,oldfusepath,newfusepath);
return ::_rename_exdev(oldfusepath,newfusepath);
return rv;
}

View file

@ -22,10 +22,13 @@
#include "errno.hpp"
#include "error.hpp"
#include "fs_clonepath.hpp"
#include "fs_lchown.hpp"
#include "fs_lstat.hpp"
#include "fs_path.hpp"
#include "fs_inode.hpp"
#include "fs_symlink.hpp"
#include "fs_symlink_as.hpp"
#include "fs_unlink.hpp"
#include "fuse_getattr.hpp"
#include "ugid.hpp"
@ -34,21 +37,48 @@
#include <sys/types.h>
#include <unistd.h>
enum class SymlinkMode
{
CREATE_AS,
PRESERVE_OWNER
};
static
int
_symlink_loop_core(const ugid_t ugid_,
const fs::path &newbranch_,
const char *target_,
const fs::path &linkpath_,
struct stat *st_)
_symlink_loop_core(const ugid_t ugid_,
const SymlinkMode mode_,
const fs::path &newbranch_,
const char *target_,
const fs::path &linkpath_,
struct stat *st_)
{
int rv;
fs::path fullnewpath;
fullnewpath = newbranch_ / linkpath_;
rv = fs::symlink_as(ugid_,target_,fullnewpath);
if(mode_ == SymlinkMode::PRESERVE_OWNER)
{
struct stat owner{};
owner.st_uid = ugid_.uid;
owner.st_gid = ugid_.gid;
rv = fs::symlink(target_,fullnewpath.native());
if(rv < 0)
return rv;
rv = fs::lchown_check_on_error(fullnewpath.native(),owner);
if(rv < 0)
{
fs::unlink(fullnewpath.native());
return rv;
}
}
else
rv = fs::symlink_as(ugid_,target_,fullnewpath);
if((rv >= 0) && (st_ != NULL) && (st_->st_ino == 0))
{
fs::lstat(fullnewpath,st_);
@ -64,6 +94,7 @@ _symlink_loop_core(const ugid_t ugid_,
static
int
_symlink_loop(const ugid_t ugid_,
const SymlinkMode mode_,
const fs::path &existingbranch_,
const std::vector<Branch*> &newbranches_,
const char *target_,
@ -83,6 +114,7 @@ _symlink_loop(const ugid_t ugid_,
err = rv;
else
err = ::_symlink_loop_core(ugid_,
mode_,
newbranch->path,
target_,
linkpath_,
@ -95,6 +127,7 @@ _symlink_loop(const ugid_t ugid_,
static
int
_symlink(const ugid_t ugid_,
const SymlinkMode mode_,
const Policy::Search &searchFunc_,
const Policy::Create &createFunc_,
const Branches::Ptr branches_,
@ -122,6 +155,7 @@ _symlink(const ugid_t ugid_,
return -ENOENT;
return ::_symlink_loop(ugid_,
mode_,
existingbranches[0]->path,
newbranches,
target_,
@ -130,16 +164,19 @@ _symlink(const ugid_t ugid_,
st_);
}
static
int
FUSE::symlink(const fuse_req_ctx_t *ctx_,
const char *target_,
const fs::path &linkpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
_symlink_with_config(const ugid_t ugid_,
const SymlinkMode mode_,
const char *target_,
const fs::path &linkpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
int rv;
rv = ::_symlink(ctx_,
rv = ::_symlink(ugid_,
mode_,
cfg.func.getattr.policy,
cfg.func.symlink.policy,
cfg.branches,
@ -149,7 +186,8 @@ FUSE::symlink(const fuse_req_ctx_t *ctx_,
if(rv == -EROFS)
{
cfg.branches.find_and_set_mode_ro();
rv = ::_symlink(ctx_,
rv = ::_symlink(ugid_,
mode_,
cfg.func.getattr.policy,
cfg.func.symlink.policy,
cfg.branches,
@ -178,6 +216,21 @@ FUSE::symlink(const fuse_req_ctx_t *ctx_,
return rv;
}
int
FUSE::symlink_owned(const ugid_t owner_,
const char *target_,
const fs::path &linkpath_,
struct stat *st_,
fuse_timeouts_t *timeouts_)
{
return ::_symlink_with_config(owner_,
SymlinkMode::PRESERVE_OWNER,
target_,
linkpath_,
st_,
timeouts_);
}
int
FUSE::symlink(const fuse_req_ctx_t *ctx_,
const char *target_,
@ -187,9 +240,10 @@ FUSE::symlink(const fuse_req_ctx_t *ctx_,
{
const fs::path linkpath{linkpath_};
return FUSE::symlink(ctx_,
target_,
linkpath,
st_,
timeouts_);
return ::_symlink_with_config(ctx_,
SymlinkMode::CREATE_AS,
target_,
linkpath,
st_,
timeouts_);
}

View file

@ -21,6 +21,7 @@
#include "fuse.h"
#include "fs_path.hpp"
#include "ugid.hpp"
#include <string>
@ -36,9 +37,9 @@ namespace FUSE
fuse_timeouts_t *timeouts = NULL);
int
symlink(const fuse_req_ctx_t *ctx,
const char *target,
const fs::path &linkpath,
struct stat *st = NULL,
fuse_timeouts_t *timeouts = NULL);
symlink_owned(const ugid_t owner,
const char *target,
const fs::path &linkpath,
struct stat *st = NULL,
fuse_timeouts_t *timeouts = NULL);
}

View file

@ -4,11 +4,10 @@ import errno
import os
import sys
import uuid
from os.path import join
from posix_parity import cleanup_dir
from posix_parity import fail
from posix_parity import join
from posix_parity import mergerfs_branches
from posix_parity import mergerfs_get_option
from posix_parity import mergerfs_mount
from posix_parity import mergerfs_set_option
@ -26,123 +25,134 @@ def get_errno(func):
return exc.errno
def symlink_resolves(path):
target = os.readlink(path)
if os.path.isabs(target):
return os.path.realpath(target)
return os.path.realpath(join(os.path.dirname(path), target))
def require_symlink(path, label):
def require_symlink(path, expected, label):
# Inspect the backend: rename/link can leave a cached regular pool inode.
if not os.path.islink(path):
return f"{label}: expected symlink fallback"
if not os.path.exists(path):
return f"{label}: symlink target does not exist: {os.readlink(path)!r}"
return f"{label}: expected backend symlink fallback"
target = os.readlink(path)
if target != expected:
return f"{label}: target {target!r} != {expected!r}"
return None
def setup_paths(branches, mount, rel_base, name):
src_rel = join(rel_base, f"{name}.src")
dst_rel = join(rel_base, "dst", name)
def require_payload(path, label):
with open(path, "rb") as fp:
if fp.read() != b"src":
return f"{label}: backend payload mismatch"
return None
def setup_paths(branches, mount, rel_base, name, root=False):
src_rel = f"{rel_base}_{name}.src" if root else join(rel_base, f"{name}.src")
dst_rel = join(rel_base, "dst", "nested", name)
src = join(mount, src_rel)
dst = join(mount, dst_rel)
cleanup_dir(join(branches[0], rel_base))
cleanup_dir(join(branches[1], rel_base))
os.makedirs(join(branches[0], rel_base), exist_ok=True)
os.makedirs(join(branches[1], rel_base, "dst"), exist_ok=True)
os.makedirs(join(branches[1], rel_base, "dst", "nested"), exist_ok=True)
touch(join(branches[0], src_rel), b"src")
return src, dst, join(branches[0], src_rel)
return src, dst, src_rel, dst_rel
def main():
try:
with mergerfs_mount(num_branches=2) as (mount, branches):
rel_base = f".mergerfs_exdev_test_{os.getpid()}_{uuid.uuid4().hex}"
root_sources = []
if len(branches) < 2:
print("EXDEV fallback test requires at least 2 branches", end="")
return SKIP
branches = branches[:2]
if os.stat(branches[0]).st_dev == os.stat(branches[1]).st_dev:
print("EXDEV fallback test requires branches on different devices", end="")
return SKIP
try:
try:
orig_create = mergerfs_get_option(mount, "category.create")
orig_link = mergerfs_get_option(mount, "link-exdev")
orig_rename = mergerfs_get_option(mount, "rename-exdev")
except (PermissionError, FileNotFoundError, OSError):
print("EXDEV fallback runtime options unavailable", end="")
return SKIP
orig_create = mergerfs_get_option(mount, "category.create")
orig_link = mergerfs_get_option(mount, "link-exdev")
orig_rename = mergerfs_get_option(mount, "rename-exdev")
try:
mergerfs_set_option(mount, "category.create", "epmfs")
src, dst, src_base = setup_paths(branches, mount, rel_base, "link_pass")
src, dst, src_rel, _ = setup_paths(branches, mount, rel_base, "link_pass")
mergerfs_set_option(mount, "link-exdev", "passthrough")
err = get_errno(lambda: os.link(src, dst + ".link.pass"))
if err != errno.EXDEV:
print(f"link-exdev passthrough did not exercise EXDEV, errno={err}", end="")
return SKIP
return fail(f"link-exdev passthrough expected EXDEV, errno={err}")
for mode in ("rel-symlink", "abs-base-symlink", "abs-pool-symlink"):
src, dst, src_base = setup_paths(branches, mount, rel_base, f"link_{mode}")
src, dst, src_rel, dst_rel = setup_paths(branches, mount, rel_base, f"link_{mode}")
mergerfs_set_option(mount, "link-exdev", mode)
link_path = dst + ".link"
err = get_errno(lambda: os.link(src, link_path))
err = get_errno(lambda: os.link(src, dst))
if err != 0:
return fail(f"link-exdev={mode} returned errno={err}")
err_msg = require_symlink(link_path, f"link-exdev={mode}")
src_base = join(branches[0], src_rel)
if mode == "rel-symlink":
expected = os.path.relpath(src, os.path.dirname(dst))
elif mode == "abs-base-symlink":
expected = src_base
else:
expected = src
err_msg = require_symlink(join(branches[1], dst_rel), expected, f"link-exdev={mode}")
if err_msg:
return fail(err_msg)
resolved = symlink_resolves(link_path)
if mode == "abs-base-symlink" and resolved != os.path.realpath(src_base):
return fail(f"link-exdev={mode}: target {resolved!r} != {os.path.realpath(src_base)!r}")
if mode != "abs-base-symlink" and resolved != os.path.realpath(src):
return fail(f"link-exdev={mode}: target {resolved!r} != {os.path.realpath(src)!r}")
err_msg = require_payload(src_base, f"link-exdev={mode}")
if err_msg:
return fail(err_msg)
if os.path.lexists(join(branches[0], dst_rel)):
return fail(f"link-exdev={mode}: destination unexpectedly created on source branch")
src_ren, dst_ren, _ = setup_paths(branches, mount, rel_base, "rename_pass")
src, dst, _, _ = setup_paths(branches, mount, rel_base, "rename_pass")
mergerfs_set_option(mount, "rename-exdev", "passthrough")
err = get_errno(lambda: os.rename(src_ren, dst_ren + ".ren.pass"))
err = get_errno(lambda: os.rename(src, dst))
if err != errno.EXDEV:
print(f"rename-exdev passthrough did not exercise EXDEV, errno={err}", end="")
return SKIP
return fail(f"rename-exdev passthrough expected EXDEV, errno={err}")
for mode in ("rel-symlink", "abs-symlink"):
src_ren, dst_ren, _ = setup_paths(branches, mount, rel_base, f"rename_{mode}")
mergerfs_set_option(mount, "rename-exdev", mode)
ren_path = dst_ren + ".ren"
err = get_errno(lambda: os.rename(src_ren, ren_path))
if err != 0:
return fail(f"rename-exdev={mode} returned errno={err}")
err_msg = require_symlink(ren_path, f"rename-exdev={mode}")
if err_msg:
return fail(err_msg)
target = symlink_resolves(ren_path)
if ".mergerfs_rename_exdev" not in target:
return fail(f"rename-exdev={mode}: target not moved under .mergerfs_rename_exdev: {target!r}")
with open(ren_path, "rb") as fp:
if fp.read() != b"src":
return fail(f"rename-exdev={mode}: symlink target content mismatch")
except (PermissionError, FileNotFoundError, OSError):
return SKIP
# Run root sources first, before any fallback creates staging.
for root in (True, False):
for mode in ("rel-symlink", "abs-symlink"):
# Each root case must start without a staging directory.
if root:
cleanup_dir(join(branches[0], ".mergerfs_rename_exdev"))
name = f"rename_{mode}_{root}"
if root:
root_sources.append(f"{rel_base}_{name}.src")
src, dst, src_rel, dst_rel = setup_paths(branches, mount, rel_base, name, root=root)
mergerfs_set_option(mount, "rename-exdev", mode)
err = get_errno(lambda: os.rename(src, dst))
if err != 0:
return fail(f"rename-exdev={mode}, root={root} returned errno={err}")
staged_rel = join(".mergerfs_rename_exdev", src_rel)
pool_target = join(mount, staged_rel)
expected = os.path.relpath(pool_target, os.path.dirname(dst)) if mode == "rel-symlink" else pool_target
label = f"rename-exdev={mode}, root={root}"
err_msg = require_symlink(join(branches[1], dst_rel), expected, label)
if err_msg:
return fail(err_msg)
if os.path.lexists(join(branches[0], src_rel)):
return fail(f"{label}: source was not moved")
err_msg = require_payload(join(branches[0], staged_rel), label)
if err_msg:
return fail(err_msg)
finally:
try:
mergerfs_set_option(mount, "category.create", orig_create)
mergerfs_set_option(mount, "link-exdev", orig_link)
mergerfs_set_option(mount, "rename-exdev", orig_rename)
except OSError:
pass
mergerfs_set_option(mount, "category.create", orig_create)
mergerfs_set_option(mount, "link-exdev", orig_link)
mergerfs_set_option(mount, "rename-exdev", orig_rename)
return 0
finally:
for branch in branches:
cleanup_dir(join(branch, rel_base))
cleanup_dir(join(branch, ".mergerfs_rename_exdev", rel_base))
for src_rel in root_sources:
for path in (join(branch, src_rel), join(branch, ".mergerfs_rename_exdev", src_rel)):
if os.path.lexists(path):
os.unlink(path)
except RuntimeError as exc:
print(str(exc), end="")
return 77
if str(exc) in ("mergerfs binary not found", "fusermount3/fusermount not found"):
print(str(exc), end="")
return SKIP
return fail(f"EXDEV fallback test failed: {exc}")
except OSError as exc:
return fail(f"EXDEV fallback test failed: {exc}")
if __name__ == "__main__":

View file

@ -3,8 +3,10 @@
#include "config.hpp"
#include "fs_copyfile.hpp"
#include "fs_inode.hpp"
#include "fuse_rename.hpp"
#include "from_string.hpp"
#include "fuse_link.hpp"
#include "fuse_rename.hpp"
#include "fuse_symlink.hpp"
#include "hashset.hpp"
#include "num.hpp"
#include "policies.hpp"
@ -29,6 +31,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
template<typename Predicate>
@ -3320,6 +3323,62 @@ test_rename_exdev_case(const char *mode_,
check_exdev_symlink(fixture.second / fixture.destination,expected,source_st);
}
static
void
test_link_exdev_case(const char *mode_,
const unsigned invalid_fields_,
const bool occupied_ = false,
const bool distinct_owner_ = false)
{
ExdevFixture fixture;
if(!fixture.ready)
return;
TEST_CASE(mode_);
cfg.link_exdev.from_string(mode_);
const auto ctx = exdev_context(invalid_fields_);
if(distinct_owner_)
{
TEST_CHECK(::chown((fixture.first / fixture.source).c_str(),12345,23456) == 0);
TEST_CHECK(::chown((fixture.second / fixture.destination.parent_path()).c_str(),0,34567) == 0);
TEST_CHECK(::chmod((fixture.second / fixture.destination.parent_path()).c_str(),02770) == 0);
}
struct stat source_st = {};
TEST_CHECK(::lstat((fixture.first / fixture.source).c_str(),&source_st) == 0);
check_exdev_absent(fixture.second / fixture.source);
check_exdev_absent(fixture.first / fixture.destination.parent_path());
if(occupied_)
{
std::ofstream output(fixture.second / fixture.destination);
output << "existing destination";
}
struct stat output_st = {};
fuse_timeouts_t timeouts = {};
const int rv = FUSE::link(&ctx,fixture.source.c_str(),fixture.destination.c_str(),
&output_st,&timeouts);
TEST_CHECK(rv == (occupied_ ? -EEXIST : 0));
check_exdev_payload(fixture.first / fixture.source);
check_exdev_absent(fixture.first / ".mergerfs_rename_exdev");
check_exdev_absent(fixture.first / fixture.destination);
if(occupied_)
{
check_exdev_payload(fixture.second / fixture.destination,"existing destination");
return;
}
if(rv != 0)
return;
TEST_CHECK(S_ISREG(output_st.st_mode));
TEST_CHECK(timeouts.attr == 0);
TEST_CHECK(timeouts.entry == 0);
fs::path expected;
if(std::string(mode_) == "rel-symlink")
expected = fixture.source.lexically_relative(fixture.destination.parent_path());
else if(std::string(mode_) == "abs-base-symlink")
expected = fixture.first / fixture.source;
else
expected = cfg.mountpoint / fixture.source;
check_exdev_symlink(fixture.second / fixture.destination,expected,source_st);
}
void
test_fuse_rename_exdev_relative_nested()
{
@ -3333,6 +3392,130 @@ test_fuse_rename_exdev_root_source()
test_rename_exdev_case(mode,true,0);
}
void
test_fuse_rename_exdev_invalid_context()
{
for(const auto mode : {"rel-symlink","abs-symlink"})
for(unsigned fields = 1; fields <= 3; ++fields)
test_rename_exdev_case(mode,false,fields);
}
void
test_fuse_link_exdev_variants()
{
for(const auto mode : {"rel-symlink","abs-base-symlink","abs-pool-symlink"})
test_link_exdev_case(mode,0);
}
void
test_fuse_link_exdev_invalid_context()
{
for(const auto mode : {"rel-symlink","abs-base-symlink","abs-pool-symlink"})
for(unsigned fields = 1; fields <= 3; ++fields)
test_link_exdev_case(mode,fields);
}
void
test_fuse_exdev_existing_destination()
{
for(const auto mode : {"rel-symlink","abs-symlink"})
test_rename_exdev_case(mode,false,3,true);
for(const auto mode : {"rel-symlink","abs-base-symlink","abs-pool-symlink"})
test_link_exdev_case(mode,3,true);
}
static
bool
exdev_distinct_ownership_available()
{
if(::geteuid() != 0)
return false;
char tmp_template[] = "/tmp/mergerfs-test-exdev-ownership-XXXXXX";
const int fd = ::mkstemp(tmp_template);
TEST_CHECK(fd >= 0);
if(fd < 0)
return false;
// Namespace root may lack the mappings or capabilities for these IDs.
// Probe outside production callbacks, in a child so credentials cannot leak.
const pid_t child = ::fork();
if(child == 0)
{
if((::fchown(fd,12345,23456) != 0) ||
(::fchown(fd,0,34567) != 0) ||
(::setregid(-1,23456) != 0) ||
(::setreuid(-1,12345) != 0) ||
(::setreuid(-1,0) != 0) ||
(::setregid(-1,0) != 0))
::_exit(77);
::_exit(0);
}
TEST_CHECK(child > 0);
int status = 0;
pid_t waited = -1;
if(child > 0)
{
do
waited = ::waitpid(child,&status,0);
while((waited < 0) && (errno == EINTR));
TEST_CHECK(waited == child);
if(waited == child)
TEST_CHECK(WIFEXITED(status));
}
TEST_CHECK(::close(fd) == 0);
TEST_CHECK(::unlink(tmp_template) == 0);
return (child > 0) && (waited == child) &&
WIFEXITED(status) && (WEXITSTATUS(status) == 0);
}
void
test_fuse_exdev_source_ownership()
{
// Run only when distinct IDs and credential switching are really available.
if(!exdev_distinct_ownership_available())
return;
for(const auto mode : {"rel-symlink","abs-symlink"})
for(const unsigned fields : {0u,3u})
test_rename_exdev_case(mode,false,fields,false,true);
for(const auto mode : {"rel-symlink","abs-base-symlink","abs-pool-symlink"})
for(const unsigned fields : {0u,3u})
test_link_exdev_case(mode,fields,false,true);
}
void
test_fuse_symlink_request_ownership()
{
if(!exdev_distinct_ownership_available())
return;
ExdevFixture fixture;
if(!fixture.ready)
return;
// Unlike internal EXDEV fallbacks, normal SYMLINK uses the valid request UID
// and lets the destination directory's setgid bit choose the native group.
TEST_CHECK(::chmod(fixture.root.c_str(),0755) == 0);
const auto parent = fixture.second / fixture.destination.parent_path();
TEST_CHECK(::chmod(fixture.second.c_str(),0755) == 0);
TEST_CHECK(::chmod((fixture.second / "destination").c_str(),0755) == 0);
TEST_CHECK(::chown(parent.c_str(),0,34567) == 0);
TEST_CHECK(::chmod(parent.c_str(),02777) == 0);
auto ctx = exdev_context(0);
ctx.uid = 12345;
ctx.gid = 23456;
struct stat output_st = {};
fuse_timeouts_t timeouts = {};
const int rv = FUSE::symlink(&ctx,"target",fixture.destination.c_str(),&output_st,&timeouts);
TEST_CHECK(rv == 0);
if(rv == 0)
{
struct stat expected = {};
expected.st_uid = ctx.uid;
expected.st_gid = 34567;
check_exdev_symlink(fixture.second / fixture.destination,"target",expected);
}
}
void
test_fs_copyfile_basic()
{
@ -4129,6 +4312,12 @@ TEST_LIST =
{"config_prune_cmd_xattr",test_config_prune_cmd_xattr},
{"fuse_rename_exdev_relative_nested",test_fuse_rename_exdev_relative_nested},
{"fuse_rename_exdev_root_source",test_fuse_rename_exdev_root_source},
{"fuse_rename_exdev_invalid_context",test_fuse_rename_exdev_invalid_context},
{"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_exdev_source_ownership",test_fuse_exdev_source_ownership},
{"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},
{"str_eq_nullptr",test_str_eq_nullptr},