diff --git a/mkdocs/docs/config/moveonenospc.md b/mkdocs/docs/config/moveonenospc.md index 154733cf..fdceb55d 100644 --- a/mkdocs/docs/config/moveonenospc.md +++ b/mkdocs/docs/config/moveonenospc.md @@ -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 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 060b10bc..ebbcc6d2 100755 --- a/tests/TEST_movefile_enospc +++ b/tests/TEST_movefile_enospc @@ -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():