fix: report a short write when ENOSPC recovery fails after a prefix

- 78a67fd0 kept the bytes written before ENOSPC when the move worked.
  When it didn't (moveonenospc off, no branch to move to, the move or
  the retry failing, or a non-ENOSPC error), write still returned the
  error and dropped the count, though those bytes were on disk. An
  O_APPEND writer that retried the whole buffer duplicated them.
- Return the bytes written as a short write whenever some reached the
  file, as a local filesystem does. A write that wrote nothing still
  returns the error.
- TEST_movefile_enospc checks it with moveonenospc=false and a write
  bigger than the space left. Document it in moveonenospc.md.
This commit is contained in:
Antonio S. Musumeci 2026-11-22 01:27:22 -05:00
parent 0091523ce5
commit 072b07b6d5
3 changed files with 54 additions and 11 deletions

View file

@ -27,9 +27,12 @@ will:
completed append is not duplicated.
If at any point something fails the progress so far will be cleaned up
as appropriate and the original error returned.
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.
If `moveonenospc` is disabled the underlying error will be returned.
If `moveonenospc` is disabled the underlying error will be returned,
or the bytes written if part of the write succeeded.
NOTE: This feature has NO effect on policies. It ONLY applies to the
literal write function. If the create function returns `ENOSPC` or the

View file

@ -45,6 +45,17 @@ _out_of_space(const ssize_t err_)
(err_ == -EDQUOT));
}
// 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.
static
ssize_t
_written_or_err(const ssize_t written_,
const ssize_t err_)
{
return ((written_ > 0) ? written_ : err_);
}
static
int
_move_and_pwrite(const char *buf_,
@ -88,7 +99,7 @@ _move_and_pwriten(char const *buf_,
ssize_t rv;
if(cfg.moveonenospc.enabled == false)
return err_;
return ::_written_or_err(written_,err_);
rv = fs::movefile_and_open_as_root(cfg.moveonenospc.policy,
cfg.branches,
@ -96,12 +107,12 @@ _move_and_pwriten(char const *buf_,
fi_->fusepath,
fi_->fd);
if(rv < 0)
return err_;
return ::_written_or_err(written_,err_);
err = fs::dup2(rv,fi_->fd);
fs::close(rv);
if(err < 0)
return err_;
return ::_written_or_err(written_,err_);
rv = fs::pwriten(fi_->fd,
buf_ + written_,
@ -109,7 +120,7 @@ _move_and_pwriten(char const *buf_,
offset_ + written_,
&err);
if(err < 0)
return err;
return ::_written_or_err(written_ + rv,err);
return (written_ + rv);
}
@ -178,8 +189,8 @@ _write(const fuse_req_ctx_t *ctx_,
if(err == 0)
return written;
if(err && not ::_out_of_space(err))
return err;
if(not ::_out_of_space(err))
return ::_written_or_err(written,err);
std::unique_lock<std::shared_mutex> ulk(fi->mutex);
// Re-check the remaining suffix under exclusive lock as another move
@ -192,8 +203,8 @@ _write(const fuse_req_ctx_t *ctx_,
&err);
if(err == 0)
return written;
if(err && not ::_out_of_space(err))
return err;
if(not ::_out_of_space(err))
return ::_written_or_err(written,err);
return ::_move_and_pwriten(buf_,count_,offset_,fi,err,written);
}

View file

@ -110,6 +110,35 @@ def test_partial_append():
return check_relocated(branches, pool, initial + payload)
def test_partial_without_move():
with enospc_pool("minfreespace=0,cache.files=full,cache.writeback=false,moveonenospc=false") as (
branches, pool, initial,
):
# Only one block fits. The bytes written before ENOSPC stay on
# disk, so the write must report them as a short write, not
# fail: a caller that retries the whole buffer would append
# them twice.
block_size = len(initial)
payload = bytes(range(256)) * (block_size // 64)
fd = os.open(os.path.join(pool, "target"), os.O_WRONLY | os.O_APPEND)
try:
try:
written = os.write(fd, payload)
except OSError as e:
return fail(f"append failed with {e.strerror} after writing a prefix")
finally:
os.close(fd)
if not 0 < written < len(payload):
return fail(f"append returned {written}, expected a short write")
with open(os.path.join(branches["source"], "target"), "rb") as stream:
actual = stream.read()
if actual != initial + payload[:written]:
return fail(f"source holds {len(actual)} bytes, expected {len(initial) + written}")
if holders(branches) != ["source"]:
return fail(f"target on {holders(branches)}, expected only source")
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
@ -142,7 +171,7 @@ def test_second_relocation():
return check_relocated(branches, pool, bytes(expected))
TESTS = (test_partial_append, test_second_relocation)
TESTS = (test_partial_append, test_partial_without_move, test_second_relocation)
def main():