fix: report partial writes, but not partial writeback, after ENOSPC

- When ENOSPC recovery failed after part of a write reached the file
  (moveonenospc off, no branch to move to, or the move or retry
  failing), write returned the error and dropped the count although
  those bytes were on disk. An O_APPEND writer retrying the whole
  buffer duplicated them. Return the bytes written as a short write,
  as a local filesystem does; a write that wrote nothing still returns
  the error.
- The kernel flushes its page cache (cache.writeback, dirty mmap pages)
  with writepage requests, ignores the size in the reply and drops the
  pages. A short count there lost data while write, fsync, msync and
  close all succeeded. Use the request's writepage flag, not the
  handle's cache mode, so cached and direct-I/O handles both return the
  error for an incomplete flush.
- TEST_movefile_enospc covers a short append, writeback and mmap
  flushes and a direct shared mapping where the kernel allows one.
  tests.cpp calls the write callback directly so the direct-handle
  contract is covered on every kernel. Document both in moveonenospc.md.
This commit is contained in:
Antonio SJ Musumeci 2026-11-22 01:27:22 -05:00
parent 86d845e385
commit 93ccc160df
4 changed files with 264 additions and 15 deletions

View file

@ -1,6 +1,7 @@
#include "acutest/acutest.h"
#include "config.hpp"
#include "fileinfo.hpp"
#include "fs_clonepath.hpp"
#include "fs_copyfile.hpp"
#include "fs_inode.hpp"
@ -11,17 +12,21 @@
#include "fuse_readdir_factory.hpp"
#include "fuse_rename.hpp"
#include "fuse_symlink.hpp"
#include "fuse_write.hpp"
#include "hashset.hpp"
#include "num.hpp"
#include "policies.hpp"
#include "rapidhash/rapidhash.h"
#include "rnd.hpp"
#include "scope_guard/scope_guard.hpp"
#include "str.hpp"
#include "thread_pool.hpp"
#include <boost/unordered/concurrent_flat_map.hpp>
#include <atomic>
#include <chrono>
#include <csignal>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fcntl.h>
@ -35,10 +40,53 @@
#include <thread>
#include <errno.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
void
test_direct_handle_writepage_error()
{
// Exercise the real callback even on kernels without direct-I/O shared mmap.
// The file-size limit makes pwrite accept a prefix, then reject the suffix.
FILE *file = std::tmpfile();
TEST_ASSERT(file != nullptr);
DEFER { std::fclose(file); };
const int fd = ::fileno(file);
TEST_ASSERT(fd >= 0);
struct rlimit saved;
TEST_ASSERT(::getrlimit(RLIMIT_FSIZE,&saved) == 0);
constexpr size_t prefix = 4096;
TEST_ASSERT(saved.rlim_max >= prefix);
struct sigaction ignore = {}, prev;
ignore.sa_handler = SIG_IGN;
::sigemptyset(&ignore.sa_mask);
TEST_ASSERT(::sigaction(SIGXFSZ,&ignore,&prev) == 0);
DEFER { ::sigaction(SIGXFSZ,&prev,nullptr); };
struct rlimit limit = saved;
limit.rlim_cur = prefix;
TEST_ASSERT(::setrlimit(RLIMIT_FSIZE,&limit) == 0);
DEFER { ::setrlimit(RLIMIT_FSIZE,&saved); };
Branch branch;
FileInfo fi(fd,branch,"file",true);
fuse_req_ctx_t ctx = {};
fuse_file_info_t ffi = {};
ffi.fh = fi.to_fh();
char data[2 * prefix];
std::memset(data,'a',sizeof(data));
TEST_CHECK(FUSE::write(&ctx,&ffi,data,sizeof(data),0) == (int)prefix);
TEST_ASSERT(::ftruncate(fd,0) == 0);
ffi.writepage = 1;
TEST_CHECK(FUSE::write(&ctx,&ffi,data,sizeof(data),0) == -EFBIG);
char actual[sizeof(data)];
TEST_ASSERT(::pread(fd,actual,sizeof(actual),0) == (ssize_t)prefix);
TEST_CHECK(std::memcmp(data,actual,prefix) == 0);
}
template<typename Predicate>
bool
wait_until(Predicate pred_,
@ -5034,6 +5082,7 @@ TEST_LIST =
{"fs_copyfile_source_changes_cleanup_tmpfiles",test_fs_copyfile_source_changes_cleanup_tmpfiles},
{"fs_movefile_and_open_same_branch",test_fs_movefile_and_open_same_branch},
{"fs_movefile_and_open_existing_path_policies",test_fs_movefile_and_open_existing_path_policies},
{"direct_handle_writepage_error",test_direct_handle_writepage_error},
{"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts},
{"statvfs_cache_failure_not_served_as_success",test_statvfs_cache_failure_not_served_as_success},
{"statvfs_cache_keeps_failure_for_timeout",test_statvfs_cache_keeps_failure_for_timeout},