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 b934e8bd59
5 changed files with 282 additions and 59 deletions

View file

@ -24,35 +24,44 @@
#include "fs_lstat.hpp"
#include "fs_path.hpp"
#include <string>
static
bool
_is_fd_file(const fs::path &basepath_,
const fs::path &fusepath_,
const struct stat &fdst_)
{
int rv;
struct stat st;
rv = fs::lstat(basepath_ / fusepath_,&st);
if(rv < 0)
return false;
return ((st.st_dev == fdst_.st_dev) &&
(st.st_ino == fdst_.st_ino));
}
static
int
_findonfs(const Branches::Ptr &branches_,
const fs::path &fusepath_,
const int fd_,
std::string *basepath_)
fs::path *basepath_)
{
int rv;
dev_t dev;
struct stat st;
fs::path fullpath;
rv = fs::fstat(fd_,&st);
if(rv < 0)
return rv;
dev = st.st_dev;
if(::_is_fd_file(*basepath_,fusepath_,st))
return 0;
for(const auto &branch : *branches_)
{
fullpath = branch.path / fusepath_;
rv = fs::lstat(fullpath,&st);
if(rv < 0)
continue;
if(st.st_dev != dev)
if(not ::_is_fd_file(branch.path,fusepath_,st))
continue;
*basepath_ = branch.path;
@ -66,9 +75,9 @@ _findonfs(const Branches::Ptr &branches_,
int
fs::findonfs(const Branches::Ptr &branches_,
const std::string &fusepath_,
const fs::path &fusepath_,
const int fd_,
std::string *basepath_)
fs::path *basepath_)
{
return ::_findonfs(branches_,fusepath_,fd_,basepath_);
}

View file

@ -19,15 +19,17 @@
#pragma once
#include "branches.hpp"
#include <string>
#include "fs_path.hpp"
namespace fs
{
// Find the branch where `fusepath` is the file open as `fd`, trying
// `*basepath` first. Device and inode must both match so branches
// sharing a filesystem are told apart.
int
findonfs(const Branches::Ptr &branches,
const std::string &fusepath,
const fs::path &fusepath,
const int fd,
std::string *basepath);
fs::path *basepath);
}

View file

@ -59,6 +59,37 @@ _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.
static
int
_drop_src_branch(const fs::path &src_branch_,
std::vector<Branch*> *dst_branches_)
{
int rv;
struct stat src_st;
rv = fs::stat(src_branch_,&src_st);
if(rv < 0)
return rv;
std::erase_if(*dst_branches_,
[&](const Branch *branch_)
{
struct stat st;
if(fs::stat(branch_->path,&st) < 0)
return true;
return ((st.st_dev == src_st.st_dev) &&
(st.st_ino == src_st.st_ino));
});
return 0;
}
static
int
_movefile_and_open(const Policy::Create &createFunc_,
@ -77,9 +108,17 @@ _movefile_and_open(const Policy::Create &createFunc_,
fs::path dst_filepath;
std::vector<Branch*> dst_branch;
// `branchpath_` is where the file was opened. An earlier move on the
// same handle leaves the fd on another branch.
src_branch = branchpath_;
rv = fs::findonfs(branches_,fusepath_,origfd_,&src_branch);
if(rv < 0)
return rv;
rv = createFunc_(branches_,fusepath_,dst_branch);
if(rv < 0)
return rv;
rv = ::_drop_src_branch(src_branch,&dst_branch);
if(rv < 0)
return rv;
if(dst_branch.empty())