fix: leave the source branch out before moveonenospc picks a branch

- The source branch was dropped from the create policy's result. ff,
  mfs, lfs, lus and pfrd return a single branch, so when that branch
  was the source nothing was left and the write kept ENOSPC even with
  room elsewhere. That happens when the source has free space by
  statvfs but still returns ENOSPC or EDQUOT: a quota, exhausted
  inodes, full btrfs metadata, or a cached statfs.
- Build the candidate list without the source (matched by device and
  inode, as before) and run the policy on that, so it picks another
  branch. With no candidate left the move is still refused.
- test_fs_movefile_and_open_same_branch now checks that ff relocates
  past a source listed first, by its own path or an alias, and that a
  list made only of the source is refused.

Refuse relocation if a duplicate on another branch could become visible
when the source is removed. Keep non-replacing publication to protect
against a late arrival at the destination. The mounted test checks the
original and duplicate contents, then reopens the file through the pool.
This commit is contained in:
Antonio S. Musumeci 2026-11-22 01:24:45 -05:00
parent db9e991ebd
commit cd0679b01f
6 changed files with 189 additions and 50 deletions

View file

@ -33,11 +33,20 @@
#include "fs_rename.hpp"
#include "fs_unlink.hpp"
#include "fs_xattr.hpp"
#include "to_neg_errno.hpp"
#include "scope_guard/scope_guard.hpp"
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#ifdef __linux__
#include <linux/fs.h>
#endif
#if defined(__linux__) || defined(__FreeBSD__)
#include <sys/syscall.h>
#endif
static
@ -57,6 +66,36 @@ _ignorable_error(const s64 err_)
return false;
}
static
int
_publish_noreplace(const char *tmppath_,
const char *dstpath_)
{
int rv;
#if defined(SYS_renameat2) && defined(RENAME_NOREPLACE)
rv = ::to_neg_errno(::syscall(SYS_renameat2,AT_FDCWD,tmppath_,
AT_FDCWD,dstpath_,RENAME_NOREPLACE));
#elif defined(__APPLE__) && defined(RENAME_EXCL)
rv = ::to_neg_errno(::renamex_np(tmppath_,dstpath_,RENAME_EXCL));
#else
rv = -ENOSYS;
#endif
if((rv != -ENOSYS) && (rv != -EINVAL) &&
(rv != -ENOTSUP) && (rv != -EOPNOTSUPP))
return rv;
// Some filesystems reject the rename flag. The staging file is a
// regular file in the same directory, so link can publish it atomically
// without replacement. Never fall back to a replacing rename.
rv = ::to_neg_errno(::link(tmppath_,dstpath_));
if(rv < 0)
return rv;
// The destination is now published. A cleanup error cannot undo that.
fs::unlink(tmppath_);
return 0;
}
s64
fs::copyfile(const int src_fd_,
const struct stat &src_st_,
@ -155,7 +194,10 @@ fs::copyfile(const int src_fd_,
continue;
}
rv = fs::rename(dst_tmppath,dst_filepath_);
if(flags_.no_replace)
rv = ::_publish_noreplace(dst_tmppath.c_str(),dst_filepath_.c_str());
else
rv = fs::rename(dst_tmppath.c_str(),dst_filepath_.c_str());
if((rv < 0) && (flags_.cleanup_failure))
fs::unlink(dst_tmppath);
break;

View file

@ -31,6 +31,7 @@ namespace fs
struct CopyFileFlags
{
int cleanup_failure:1;
bool no_replace:1 = false;
};
s64

View file

@ -27,6 +27,7 @@
#include "fs_findonfs.hpp"
#include "fs_getfl.hpp"
#include "fs_has_space.hpp"
#include "fs_lstat.hpp"
#include "fs_mktemp.hpp"
#include "fs_open.hpp"
#include "fs_path.hpp"
@ -35,6 +36,7 @@
#include "fs_unlink.hpp"
#include "policy.hpp"
#include <memory>
#include <string>
#include <vector>
@ -59,14 +61,14 @@ _cleanup_flags(const int flags_)
return rv;
}
// Copying onto the source directory would rename the copy over the
// file and then unlink it. Match by device and inode so another path
// to the same directory, such as a bind mount, is caught too. A
// destination that can't be stat'd is dropped as well.
// A duplicate on any branch could be exposed when the source is unlinked.
// Check even branches that the create policy cannot select.
static
int
_drop_src_branch(const fs::path &src_branch_,
std::vector<Branch*> *dst_branches_)
_destination_candidates(const fs::path &src_branch_,
const fs::path &fusepath_,
const Branches::Ptr &branches_,
Branches::Ptr *candidates_)
{
int rv;
struct stat src_st;
@ -75,17 +77,32 @@ _drop_src_branch(const fs::path &src_branch_,
if(rv < 0)
return rv;
std::erase_if(*dst_branches_,
[&](const Branch *branch_)
{
struct stat st;
for(const Branch &branch : *branches_)
{
struct stat st;
if(fs::stat(branch_->path,&st) < 0)
return true;
rv = fs::stat(branch.path.c_str(),&st);
if(rv < 0)
return rv;
if((st.st_dev == src_st.st_dev) &&
(st.st_ino == src_st.st_ino))
continue;
return ((st.st_dev == src_st.st_dev) &&
(st.st_ino == src_st.st_ino));
});
rv = fs::lstat((branch.path / fusepath_).c_str(),&st);
if(rv == 0)
return -EEXIST;
if(rv != -ENOENT)
return rv;
if(branch.ro_or_nc())
continue;
if(!*candidates_)
{
*candidates_ = std::make_shared<Branches::Impl>(&branches_->minfreespace());
(*candidates_)->reserve(branches_->size());
}
(*candidates_)->push_back(branch);
}
return 0;
}
@ -106,6 +123,7 @@ _movefile_and_open(const Policy::Create &createFunc_,
fs::path src_branch;
fs::path src_filepath;
fs::path dst_filepath;
Branches::Ptr candidates;
std::vector<Branch*> dst_branch;
// `branchpath_` is where the file was opened. An earlier move on the
@ -115,10 +133,13 @@ _movefile_and_open(const Policy::Create &createFunc_,
if(rv < 0)
return rv;
rv = createFunc_(branches_,fusepath_,dst_branch);
rv = ::_destination_candidates(src_branch,fusepath_,branches_,&candidates);
if(rv < 0)
return rv;
rv = ::_drop_src_branch(src_branch,&dst_branch);
if(!candidates)
return -ENOSPC;
rv = createFunc_(candidates,fusepath_,dst_branch);
if(rv < 0)
return rv;
if(dst_branch.empty())
@ -144,7 +165,8 @@ _movefile_and_open(const Policy::Create &createFunc_,
src_filepath = src_branch / fusepath_;
dst_filepath = dst_branch[0]->path / fusepath_;
rv = fs::copyfile(src_filepath,dst_filepath,{.cleanup_failure = true});
rv = fs::copyfile(src_filepath,dst_filepath,
{.cleanup_failure = true, .no_replace = true});
if(rv < 0)
return -ENOSPC;