fix: make the statfs cache correct and its timeout configurable

- A failed statvfs was cached as a success: for the next cache.statfs
  seconds every caller got the entry's old or zeroed stats and create
  policies chose branches from them. Keep a failure as an error for the
  timeout, like a success, so a slow failing branch isn't retried under
  the lock by every caller in turn. Document that the branch then stays
  excluded from space-based create policies until the timeout expires.
- A new entry was told apart from a fresh one only by a time of 0, so a
  timeout of at least the current time in seconds returned the zeroed
  struct as success. Give entries a valid flag set by the first
  statvfs, and measure age with a steady clock from when statvfs
  returned.
- cache.statfs given at mount was stored but never applied. Bind the
  option directly to the atomic the cache reads, so mount options,
  config files and control-file updates follow one path and concurrent
  readback no longer races with updates.
- tests.cpp checks that a cached failure is never served as success,
  that failures are kept until the timeout, huge timeouts, an invalid
  update keeping a cached ENOENT, and a timeout of 0 exposing a newly
  created directory immediately.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:34:26 -05:00
parent 844e1acffd
commit b367b593ba
8 changed files with 152 additions and 48 deletions

View file

@ -5,6 +5,7 @@
#include "fs_copyfile.hpp"
#include "fs_inode.hpp"
#include "fs_movefile_and_open.hpp"
#include "fs_statvfs_cache.hpp"
#include "from_string.hpp"
#include "fuse_link.hpp"
#include "fuse_readdir_factory.hpp"
@ -3875,6 +3876,93 @@ test_readdir_factory_rejects_oversized_counts()
TEST_CHECK(FUSE::ReadDirFactory::make("cor:99999999999") == nullptr);
}
// A failed statvfs is cached for the timeout like a success, but the
// cache must never serve a cached failure as a success.
void
test_statvfs_cache_failure_not_served_as_success()
{
int rv;
struct statvfs st;
const std::string path = "/nonexistent/mergerfs-statvfs-cache-test";
TEST_CHECK(cfg.set("cache.statfs","60") == 0);
rv = fs::statvfs_cache(path,&st);
TEST_CHECK(rv == -ENOENT);
TEST_MSG("first rv: %d",rv);
rv = fs::statvfs_cache(path,&st);
TEST_CHECK(rv == -ENOENT);
TEST_MSG("second rv: %d",rv);
TEST_CHECK(cfg.set("cache.statfs","0") == 0);
}
// A failure is kept for the timeout like a success, so a branch that
// fails slowly isn't asked again on every call. Create the directory
// after the first failure: a cached failure still shows ENOENT, an
// uncached call would succeed.
void
test_statvfs_cache_keeps_failure_for_timeout()
{
int rv;
struct statvfs st;
char tmp_template[] = "/tmp/mergerfs-test-statvfs-cache-XXXXXX";
if(::mkdtemp(tmp_template) == nullptr)
{
TEST_CHECK(false);
return;
}
const std::string path = std::string(tmp_template) + "/later";
TEST_CHECK(cfg.set("cache.statfs","60") == 0);
rv = fs::statvfs_cache(path,&st);
TEST_CHECK(rv == -ENOENT);
TEST_MSG("first rv: %d",rv);
TEST_CHECK(::mkdir(path.c_str(),0700) == 0);
// An invalid update must leave the cached result in force.
TEST_CHECK(cfg.set("cache.statfs","invalid") == -EINVAL);
rv = fs::statvfs_cache(path,&st);
TEST_CHECK(rv == -ENOENT);
TEST_MSG("cached rv: %d",rv);
TEST_CHECK(cfg.set("cache.statfs","0") == 0);
rv = fs::statvfs_cache(path,&st);
TEST_CHECK(rv == 0);
TEST_MSG("uncached rv: %d",rv);
::rmdir(path.c_str());
::rmdir(tmp_template);
}
// A timeout of at least the current time in seconds made a new entry
// look fresh before any statvfs had run, so every branch reported no
// free space and a failure was reported as success.
void
test_statvfs_cache_huge_timeout()
{
int rv;
struct statvfs st;
for(const u64 timeout : {(u64)4294967295ULL,(u64)~0ULL})
{
TEST_CHECK(cfg.set("cache.statfs",std::to_string(timeout)) == 0);
memset(&st,0,sizeof(st));
rv = fs::statvfs_cache("/",&st);
TEST_CHECK(rv == 0);
TEST_CHECK(st.f_blocks > 0);
TEST_MSG("timeout: %llu; rv: %d; blocks: %lu",
(unsigned long long)timeout,rv,(unsigned long)st.f_blocks);
rv = fs::statvfs_cache("/nonexistent/mergerfs-statvfs-huge-timeout",&st);
TEST_CHECK(rv == -ENOENT);
TEST_MSG("timeout: %llu; missing rv: %d",(unsigned long long)timeout,rv);
}
TEST_CHECK(cfg.set("cache.statfs","0") == 0);
}
void
test_hashset_put_and_size()
{
@ -4622,6 +4710,9 @@ TEST_LIST =
{"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},
{"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},
{"statvfs_cache_huge_timeout",test_statvfs_cache_huge_timeout},
{"str_eq_nullptr",test_str_eq_nullptr},
{"str_startswith_char_nullptr",test_str_startswith_char_nullptr},
{"str_from_u64_suffixes",test_str_from_u64_suffixes},