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

@ -1,6 +1,7 @@
#!/usr/bin/env python3
import contextlib
import errno
import os
import shutil
import subprocess
@ -142,7 +143,52 @@ def test_second_relocation():
return check_relocated(branches, pool, bytes(expected))
TESTS = (test_partial_append, test_second_relocation)
def test_occupied_destination():
options = "minfreespace=0,cache.files=off,func.open=ff,moveonenospc=ff"
with enospc_pool(options, extra_tmpfs=[("occupied", "1m")]) as (branches, pool, initial):
occupied = os.path.join(branches["occupied"], "target")
original = b"independent file\n"
with open(occupied, "wb") as stream:
stream.write(original)
payload = b"A" * (2 * len(initial))
fd = os.open(os.path.join(pool, "target"), os.O_RDWR | os.O_APPEND)
try:
if os.read(fd, len(initial)) != initial:
return fail("first-found open did not select the source target")
# Consume the fixture's last block. The target already ends on
# a block boundary, so even a short append requires relocation.
with open(os.path.join(branches["source"], "filler"), "ab") as stream:
stream.write(initial)
if os.statvfs(branches["source"]).f_bavail != 0:
return fail("source still has space before append")
try:
written = os.write(fd, payload)
except OSError as e:
if e.errno != errno.ENOSPC:
raise
else:
return fail(f"append returned {written}, expected ENOSPC")
finally:
os.close(fd)
# A held handle can mask a change in what the pool name resolves to.
with open(os.path.join(pool, "target"), "rb") as stream:
if stream.read() != initial:
return fail("pool reopen did not return the original source contents")
with open(os.path.join(branches["source"], "target"), "rb") as stream:
if stream.read() != initial:
return fail("refused append changed the source target")
with open(occupied, "rb") as stream:
if stream.read() != original:
return fail("relocation overwrote an independent target")
if os.listdir(branches["destination"]):
return fail("refused relocation left a copy on the empty branch")
return 0
TESTS = (test_partial_append,
test_second_relocation,
test_occupied_destination)
def main():

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,55 +3714,74 @@ 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);
if(!TEST_CHECK(!ec && (fd >= 0) && (::fstat(fd,&before) == 0)))
TEST_CHECK(!ec);
if(ec)
{
if(fd >= 0)
::close(fd);
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);
if(!TEST_CHECK(fd >= 0))
return false;
if(!TEST_CHECK(::fstat(fd,&before) == 0))
{
::close(fd);
return false;
}
return true;
};
// 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);
if(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);
}