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.
This commit is contained in:
Antonio S. Musumeci 2026-11-22 01:24:45 -05:00
parent 4434e3ba62
commit 1423e577a3
2 changed files with 69 additions and 41 deletions

View file

@ -35,6 +35,7 @@
#include "fs_unlink.hpp"
#include "policy.hpp"
#include <memory>
#include <string>
#include <vector>
@ -62,11 +63,15 @@ _cleanup_flags(const int flags_)
// 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.
// branch that can't be stat'd is left out as well. The source is
// removed before the create policy runs: most policies pick a single
// branch, and filtering their pick afterwards would leave nothing
// when they chose the source.
static
int
_drop_src_branch(const fs::path &src_branch_,
std::vector<Branch*> *dst_branches_)
_branches_without_src(const fs::path &src_branch_,
const Branches::Ptr &branches_,
Branches::Ptr *candidates_)
{
int rv;
struct stat src_st;
@ -75,17 +80,19 @@ _drop_src_branch(const fs::path &src_branch_,
if(rv < 0)
return rv;
std::erase_if(*dst_branches_,
[&](const Branch *branch_)
{
struct stat st;
*candidates_ = std::make_shared<Branches::Impl>(&branches_->minfreespace());
for(const Branch &branch : *branches_)
{
struct stat st;
if(fs::stat(branch_->path,&st) < 0)
return true;
if(fs::stat(branch.path,&st) < 0)
continue;
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));
});
(*candidates_)->push_back(branch);
}
return 0;
}
@ -106,6 +113,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 +123,13 @@ _movefile_and_open(const Policy::Create &createFunc_,
if(rv < 0)
return rv;
rv = createFunc_(branches_,fusepath_,dst_branch);
rv = ::_branches_without_src(src_branch,branches_,&candidates);
if(rv < 0)
return rv;
rv = ::_drop_src_branch(src_branch,&dst_branch);
if(candidates->empty())
return -ENOSPC;
rv = createFunc_(candidates,fusepath_,dst_branch);
if(rv < 0)
return rv;
if(dst_branch.empty())

View file

@ -3681,10 +3681,12 @@ _file_contents(const fs::path &path_)
return contents.str();
}
// moveonenospc's create policy can pick the branch the file is already
// on, by its own path or by another path to the same directory (a bind
// mount; a symlink stands in for one here). The copy would be renamed
// over the file and then unlinked, so such a move must be refused.
// moveonenospc must never pick the branch the file is already on, by
// its own path or by another path to the same directory (a bind mount;
// a symlink stands in for one here). The copy would be renamed over
// the file and then unlinked. Such branches are removed before the
// create policy runs, so a policy that picks one branch (ff here)
// chooses another one rather than nothing.
void
test_fs_movefile_and_open_same_branch()
{
@ -3712,54 +3714,69 @@ test_fs_movefile_and_open_same_branch()
std::filesystem::create_directory(other,ec);
if(!ec)
std::filesystem::create_directory_symlink(src,alias,ec);
if(!ec)
std::ofstream(src / "file",std::ios::binary) << data;
fd = ::open((src / "file").c_str(),O_RDWR);
TEST_CHECK(!ec && (fd >= 0) && (::fstat(fd,&before) == 0));
if(ec || (fd < 0))
TEST_CHECK(!ec);
if(ec)
{
std::filesystem::remove_all(tmp_dir);
return;
}
// ff picks the first branch. Branches resolves symlinks while
// parsing, so the first path is set afterwards.
auto move = [&](const std::string &list_,
const fs::path &first_)
auto reset = [&]()
{
std::filesystem::remove(other / "file",ec);
std::ofstream(src / "file",std::ios::binary|std::ios::trunc) << data;
fd = ::open((src / "file").c_str(),O_RDWR);
TEST_CHECK((fd >= 0) && (::fstat(fd,&before) == 0));
return (fd >= 0);
};
// Branches resolves symlinks while parsing, so the paths are set
// afterwards.
auto move = [&](const fs::path &first_,
const fs::path &second_)
{
Branches branches;
branches.minfreespace = 0;
TEST_CHECK(branches.from_string(list_) == 0);
TEST_CHECK(branches.from_string(src.string() + ":" + other.string()) == 0);
Branches::Ptr ptr = branches;
ptr->front().path = first_;
ptr->back().path = second_;
return fs::movefile_and_open(ff,ptr,src,"file",fd);
};
for(const auto &first : {src,alias})
// Every branch is the source: refused, file left alone.
if(reset())
{
rv = move(src.string() + ":" + other.string(),first);
rv = move(src,alias);
TEST_CHECK(rv == -ENOSPC);
TEST_MSG("first branch: %s; rv: %d",first.c_str(),rv);
TEST_MSG("rv: %d",rv);
if(rv >= 0)
::close(rv);
TEST_CHECK(::stat((src / "file").c_str(),&after) == 0);
TEST_CHECK(after.st_ino == before.st_ino);
TEST_CHECK(_file_contents(src / "file") == data);
TEST_CHECK(!std::filesystem::exists(other / "file"));
::close(fd);
}
// Control: a different first branch still relocates.
rv = move(other.string() + ":" + src.string(),other);
TEST_CHECK(rv >= 0);
TEST_MSG("rv: %d",rv);
if(rv >= 0)
::close(rv);
TEST_CHECK(!std::filesystem::exists(src / "file"));
TEST_CHECK(_file_contents(other / "file") == data);
// ff would pick the source listed first; it picks `other` instead.
for(const auto &first : {src,alias,other})
{
if(!reset())
break;
rv = move(first,((first == other) ? src : other));
TEST_CHECK(rv >= 0);
TEST_MSG("first branch: %s; rv: %d",first.c_str(),rv);
if(rv >= 0)
::close(rv);
TEST_CHECK(!std::filesystem::exists(src / "file"));
TEST_CHECK(_file_contents(other / "file") == data);
::close(fd);
}
::close(fd);
std::filesystem::remove_all(tmp_dir);
}