fix: don't cache a failed statvfs as a success

- With cache.statfs=N, statvfs_cache set the entry's timestamp before
  calling statvfs. When that call failed, the failure was returned
  once and then every caller for the next N seconds got success with
  the entry's old or zeroed stats, and create policies chose
  branches from them.
- Refresh the entry only when statvfs succeeds. A failure is
  returned and retried on the next call.
- tests.cpp checks that a second lookup of a missing path still
  fails.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:34:26 -05:00
parent 7d677d1a37
commit 5e74822f91
2 changed files with 31 additions and 3 deletions

View file

@ -90,19 +90,27 @@ fs::statvfs_cache(const std::string &path_,
} }
} }
int rv = 0;
std::unique_lock<std::shared_mutex> lk(g_cache_mutex); std::unique_lock<std::shared_mutex> lk(g_cache_mutex);
// Only a successful statvfs refreshes the entry. A failure is
// returned to the caller and retried on the next call.
Element &e = g_cache[path_]; Element &e = g_cache[path_];
if((now - e.time) > g_timeout) if((now - e.time) > g_timeout)
{ {
int rv;
struct statvfs st;
rv = fs::statvfs(path_,&st);
if(rv < 0)
return rv;
e.time = now; e.time = now;
rv = fs::statvfs(path_,&e.st); e.st = st;
} }
*st_ = e.st; *st_ = e.st;
return rv; return 0;
} }
int int

View file

@ -5,6 +5,7 @@
#include "fs_copyfile.hpp" #include "fs_copyfile.hpp"
#include "fs_inode.hpp" #include "fs_inode.hpp"
#include "fs_movefile_and_open.hpp" #include "fs_movefile_and_open.hpp"
#include "fs_statvfs_cache.hpp"
#include "from_string.hpp" #include "from_string.hpp"
#include "fuse_link.hpp" #include "fuse_link.hpp"
#include "fuse_readdir_factory.hpp" #include "fuse_readdir_factory.hpp"
@ -3775,6 +3776,24 @@ test_readdir_factory_rejects_oversized_counts()
TEST_CHECK(FUSE::ReadDirFactory::make("cor:99999999999") == nullptr); TEST_CHECK(FUSE::ReadDirFactory::make("cor:99999999999") == nullptr);
} }
// A failed statvfs must not be served from the cache as a success.
void
test_statvfs_cache_does_not_cache_failure()
{
int rv;
struct statvfs st;
const std::string path = "/nonexistent/mergerfs-statvfs-cache-test";
fs::statvfs_cache_timeout(60);
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);
fs::statvfs_cache_timeout(0);
}
void void
test_hashset_put_and_size() test_hashset_put_and_size()
{ {
@ -4483,6 +4502,7 @@ TEST_LIST =
{"fs_copyfile_source_changes_cleanup_tmpfiles",test_fs_copyfile_source_changes_cleanup_tmpfiles}, {"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_same_branch",test_fs_movefile_and_open_same_branch},
{"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts}, {"readdir_factory_rejects_oversized_counts",test_readdir_factory_rejects_oversized_counts},
{"statvfs_cache_does_not_cache_failure",test_statvfs_cache_does_not_cache_failure},
{"str_eq_nullptr",test_str_eq_nullptr}, {"str_eq_nullptr",test_str_eq_nullptr},
{"str_startswith_char_nullptr",test_str_startswith_char_nullptr}, {"str_startswith_char_nullptr",test_str_startswith_char_nullptr},
{"str_from_u64_suffixes",test_str_from_u64_suffixes}, {"str_from_u64_suffixes",test_str_from_u64_suffixes},