fix: return the error, not a short count, for page cache flushes

- 072b07b6 made a write that fails with ENOSPC after part of it landed
  return the bytes written. The kernel issues writes to flush its page
  cache (cache.writeback, dirty mmap pages) with the writepage flag
  and ignores the size in the reply, then drops the pages. The short
  count lost the rest of the data and hid the error: write, fsync,
  msync and close all succeeded with half the data on disk, where
  2.42.2 returned ENOSPC from fsync or msync.
- Keep the short count for ordinary writes, which the kernel does
  handle, and return the error when the request has writepage set.
- TEST_movefile_enospc covers a writeback flush and an mmap msync with
  moveonenospc=false; both fail without this change. Note the
  exception in moveonenospc.md.

Initialize the flush-test error list before the first I/O operation so
an unexpected error propagates and the descriptor is still closed.
Use the mapping as a context manager for the same reason.
This commit is contained in:
Antonio S. Musumeci 2026-11-22 08:27:04 -05:00
parent 6e9bdd9564
commit 64d3af02a3
3 changed files with 82 additions and 11 deletions

View file

@ -36,7 +36,10 @@ will:
If at any point something fails the progress so far will be cleaned up
as appropriate and the original error returned. If part of the write
already reached the file the bytes written are returned instead, as a
short write, as with a local filesystem.
short write, as with a local filesystem. The exception is data the
kernel is flushing from its page cache (`cache.writeback` or dirty
`mmap` pages). It can't act on a short write, so the error is returned
and reaches `fsync`, `msync` or `close`.
If `moveonenospc` is disabled the underlying error will be returned,
or the bytes written if part of the write succeeded.

View file

@ -48,12 +48,18 @@ _out_of_space(const ssize_t err_)
// Bytes already written stay written. Report them as a short write,
// as a local filesystem does, rather than the error: a caller that
// retries the whole buffer would write them twice.
//
// Not for writes the kernel issues to flush its page cache (writeback
// cache, dirty mmap pages). It ignores the size in the reply and
// drops the pages, so a short count would lose the rest and hide the
// error. Only the error reaches fsync, msync and close.
static
ssize_t
_written_or_err(const ssize_t written_,
const ssize_t err_)
const ssize_t err_,
const bool short_ok_)
{
return ((written_ > 0) ? written_ : err_);
return (((written_ > 0) && short_ok_) ? written_ : err_);
}
static
@ -93,13 +99,14 @@ _move_and_pwriten(char const *buf_,
off_t const offset_,
FileInfo *fi_,
ssize_t const err_,
ssize_t const written_)
ssize_t const written_,
bool const short_ok_)
{
int err;
ssize_t rv;
if(cfg.moveonenospc.enabled == false)
return ::_written_or_err(written_,err_);
return ::_written_or_err(written_,err_,short_ok_);
rv = fs::movefile_and_open_as_root(cfg.moveonenospc.policy,
cfg.branches,
@ -107,12 +114,12 @@ _move_and_pwriten(char const *buf_,
fi_->fusepath,
fi_->fd);
if(rv < 0)
return ::_written_or_err(written_,err_);
return ::_written_or_err(written_,err_,short_ok_);
err = fs::dup2(rv,fi_->fd);
fs::close(rv);
if(err < 0)
return ::_written_or_err(written_,err_);
return ::_written_or_err(written_,err_,short_ok_);
rv = fs::pwriten(fi_->fd,
buf_ + written_,
@ -120,7 +127,7 @@ _move_and_pwriten(char const *buf_,
offset_ + written_,
&err);
if(err < 0)
return ::_written_or_err(written_ + rv,err);
return ::_written_or_err(written_ + rv,err,short_ok_);
return (written_ + rv);
}
@ -157,6 +164,7 @@ _write(const fuse_req_ctx_t *ctx_,
// `direct_io=true`
ssize_t rv;
const bool short_ok = not ffi_->writepage;
if(fi->direct_io)
{
@ -190,7 +198,7 @@ _write(const fuse_req_ctx_t *ctx_,
if(err == 0)
return written;
if(not ::_out_of_space(err))
return ::_written_or_err(written,err);
return ::_written_or_err(written,err,short_ok);
std::unique_lock<std::shared_mutex> ulk(fi->mutex);
// Re-check the remaining suffix under exclusive lock as another move
@ -204,9 +212,9 @@ _write(const fuse_req_ctx_t *ctx_,
if(err == 0)
return written;
if(not ::_out_of_space(err))
return ::_written_or_err(written,err);
return ::_written_or_err(written,err,short_ok);
return ::_move_and_pwriten(buf_,count_,offset_,fi,err,written);
return ::_move_and_pwriten(buf_,count_,offset_,fi,err,written,short_ok);
}
}

View file

@ -2,6 +2,7 @@
import contextlib
import errno
import mmap
import os
import shutil
import subprocess
@ -140,6 +141,63 @@ def test_partial_without_move():
return 0
def flush_errors(calls):
"""Run each (name, function) and return the names that raised ENOSPC."""
failed = []
for name, call in calls:
try:
call()
except OSError as e:
if e.errno != errno.ENOSPC:
raise
failed.append(name)
return failed
def test_writeback_flush_reports_error():
with enospc_pool("minfreespace=0,cache.files=full,cache.writeback=true,moveonenospc=false") as (
branches, pool, initial,
):
# The kernel flushes its page cache with writes whose reply size
# it ignores. The tail that doesn't fit is dropped, so the
# error has to reach fsync or close: a short count would hide it.
payload = bytes(range(1, 256)) * (len(initial) // 32)
failed = []
fd = os.open(os.path.join(pool, "target"), os.O_WRONLY)
try:
failed += flush_errors([
("write", lambda: os.pwrite(fd, payload, len(initial))),
("fsync", lambda: os.fsync(fd)),
])
finally:
failed += flush_errors([("close", lambda: os.close(fd))])
if not failed:
return fail("write, fsync and close all succeeded but the data did not fit")
return 0
def test_mmap_flush_reports_error():
with enospc_pool("minfreespace=0,cache.files=full,cache.writeback=false,moveonenospc=false") as (
branches, pool, initial,
):
# Dirty shared mmap pages are flushed the same way even without
# writeback cache, so msync must report the error.
payload = bytes(range(1, 256)) * (len(initial) // 32)
total = len(initial) + len(payload)
failed = []
fd = os.open(os.path.join(pool, "target"), os.O_RDWR)
try:
os.ftruncate(fd, total)
with mmap.mmap(fd, total) as mapping:
mapping[len(initial):] = payload
failed += flush_errors([("msync", mapping.flush), ("fsync", lambda: os.fsync(fd))])
finally:
failed += flush_errors([("close", lambda: os.close(fd))])
if not failed:
return fail("msync, fsync and close all succeeded but the data did not fit")
return 0
def test_second_relocation():
# lfs over branches with at least minfreespace free picks `middle`
# first; once it fills too only `destination` is left. The same open
@ -217,6 +275,8 @@ def test_occupied_destination():
TESTS = (test_partial_append,
test_partial_without_move,
test_writeback_flush_reports_error,
test_mmap_flush_reports_error,
test_second_relocation,
test_occupied_destination)