From bc7b7ffeae01a354688d97bdb73147ba2a8c5eab Mon Sep 17 00:00:00 2001 From: "Antonio S. Musumeci" Date: Sun, 22 Nov 2026 01:27:22 -0500 Subject: [PATCH] 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. --- mkdocs/docs/config/moveonenospc.md | 7 +++++-- src/fuse_write.cpp | 27 +++++++++++++++++++-------- tests/TEST_movefile_enospc | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/mkdocs/docs/config/moveonenospc.md b/mkdocs/docs/config/moveonenospc.md index a1b54d75..1f20afaa 100644 --- a/mkdocs/docs/config/moveonenospc.md +++ b/mkdocs/docs/config/moveonenospc.md @@ -34,9 +34,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 diff --git a/src/fuse_write.cpp b/src/fuse_write.cpp index d881d1e7..85625d87 100644 --- a/src/fuse_write.cpp +++ b/src/fuse_write.cpp @@ -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 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); } diff --git a/tests/TEST_movefile_enospc b/tests/TEST_movefile_enospc index 3677de7f..5517b4b3 100755 --- a/tests/TEST_movefile_enospc +++ b/tests/TEST_movefile_enospc @@ -111,6 +111,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 @@ -187,6 +216,7 @@ def test_occupied_destination(): TESTS = (test_partial_append, + test_partial_without_move, test_second_relocation, test_occupied_destination)