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

@ -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);
}