fix: keep readdir listings stable under fill errors

- func.readdir=cor appended entries in whichever order branch workers
  finished. Directory offsets are positions in that list, so a reader
  reopening an unchanged directory and seeking to a previously issued
  cookie (NFS does this) could see a duplicate and miss a later entry.
  Read branches concurrently into their own buffers, then merge and
  deduplicate them in configured branch order. The merge mutex is gone.
- Growing a dirents buffer overwrote the only pointer with realloc's
  result while recording the new capacity, so an allocation failure
  leaked the buffer and the next fill wrote through NULL. Grow both
  vectors without changing pointer, length or capacity until the
  allocation succeeds, and return ENOMEM.
- seq and cosr ignored that failure and served a listing with entries
  missing. Every policy now returns the error, and libfuse resets the
  handle's buffer whenever a fill fails, so a later request at a
  nonzero offset re-enumerates instead of serving a partial or stale
  listing as success. cosr closes the directories its workers opened
  when it gives up early.
- Document the fill-error contract, which doesn't promise that every
  branch was read: seq and cosr can treat a branch read error as the
  end of the directory. Document cor's memory use, and refer to
  func.getattr, not fuse.getattr.
- TEST_readdir_cor_cookie_replay compares cor listings, offsets and
  inodes with seq and replays cookies on fresh handles.
  TEST_dirents_allocation injects allocation failures into the dirents
  code. TEST_readdir_cor_enomem fails one allocation in a mounted
  daemon for cor and seq and checks the same handle then serves the
  exact suffix.

Performance: cor copies each entry twice and merges serially after the
parallel reads. Listing 100,000 entries over 4 tmpfs branches took
the same time before and after (81-83 ms per listing for cor:4 and
seq). Peak daemon memory for cor:4 dropped from 27.4 MB to 18.7 MB.
This commit is contained in:
Antonio SJ Musumeci 2026-05-13 10:58:01 -05:00
parent b367b593ba
commit eea1c1bc09
15 changed files with 1026 additions and 79 deletions

View file

@ -100,9 +100,7 @@ _handle_ENOENT(const fuse_file_info_t *ffi_,
strncpy(de.d_name,msg,(sizeof(msg) - 1));
de.d_reclen = sizeof(de);
fuse_dirents_add(buf_,&de,(sizeof(msg) - 1));
return 0;
return fuse_dirents_add(buf_,&de,(sizeof(msg) - 1));
}
int

View file

@ -23,6 +23,11 @@
#include "config.hpp"
#include "dirinfo.hpp"
#include "error.hpp"
#include "fuse_dirents.hpp"
#include "hashset.hpp"
#include <cstddef>
#include <new>
FUSE::ReadDirCOR::ReadDirCOR(unsigned concurrency_,
@ -43,12 +48,56 @@ FUSE::ReadDirCOR::~ReadDirCOR()
#include "fuse_readdir_cor_readdir.icpp"
#endif
// Uses a per-call mutex. Lifetime is one readdir operation. It
// serializes the dirent-merge step (dedup'ing of names + append to
// dirents list) across at-most-branches-worth of parallel worker
// tasks. Per-branch opendir + getdents64 run fully outside the
// critical section, so the lock holds only long enough to merge each
// batch.
// Each branch is read concurrently into its own dirents buffer by a
// thread pool worker. The buffers are then merged into the shared
// list in branch order, making the enumeration order reproducible
// across re-reads. Reproducibility is required by FUSE readdir
// offsets, which are positional indices into the enumeration: NFS
// can drop and re-open a directory mid-stream and replay its cookie
// against a fresh handle, and a non-reproducible order splices that
// stale index into a differently-ordered list (duplicating and
// dropping entries). See GitHub issues #781 and #1589.
static
inline
uint64_t
_dirent_size(const uint64_t namelen_)
{
uint64_t rv;
rv = offsetof(fuse_dirent_t,name);
rv += namelen_;
rv = ((rv + 7) & ~7ULL);
return rv;
}
static
inline
int
_merge_branch_dirents(fuse_dirents_t *dst_,
HashSet &names_,
const fuse_dirents_t *src_)
{
size_t pos = 0;
while(pos < kv_size(src_->data))
{
int rv;
const fuse_dirent_t *d;
d = reinterpret_cast<const fuse_dirent_t*>(&kv_A(src_->data,pos));
if(names_.put(d->name,d->namelen) != 0)
{
rv = fuse_dirents_add(dst_,d,d->namelen);
if(rv != 0)
return rv;
}
pos += _dirent_size(d->namelen);
}
return 0;
}
static
inline
int
@ -57,35 +106,125 @@ _concurrent_readdir(ThreadPool &tp_,
const fs::path &rel_dirpath_,
fuse_dirents_t *dirents_)
{
int rv;
int worker_error;
Err err;
HashSet names;
Mutex mutex;
bool fill_ok;
std::vector<fuse_dirents_t> branch_dirents;
std::vector<std::future<int>> futures;
worker_error = 0;
fill_ok = false;
fuse_dirents_reset(dirents_);
futures.reserve(branches_->size());
for(const auto &branch : *branches_)
DEFER{
for(auto &bd : branch_dirents)
fuse_dirents_free(&bd);
};
// A failed fill must not leave a partial or stale listing cached:
// the next request would continue reading it at an offset belonging
// to a different enumeration, duplicating and dropping entries.
// Reset the destination on every unsuccessful exit so the next
// readdir re-enumerates from scratch. The reset above covers entry
// into a regeneration after a previous failure, including a request
// resuming at a nonzero offset against an emptied cache.
DEFER{
if(not fill_ok)
fuse_dirents_reset(dirents_);
};
auto wait_for_tasks = [&futures]()
{
auto func =
[&,dirents_]()
// No queued task may still reference locals during cleanup.
for(auto &future : futures)
{
return ::_readdir(branch.path,
rel_dirpath_,
names,
dirents_,
mutex);
};
if(not future.valid())
continue;
try
{
future.wait();
}
catch(...)
{
}
}
};
auto rv = tp_.enqueue_task(std::move(func));
try
{
branch_dirents.reserve(branches_->size());
for(size_t i = 0; i < branches_->size(); ++i)
{
fuse_dirents_t bd;
futures.emplace_back(std::move(rv));
// Per-branch scratch buffer: empty with just the zero
// offset sentinel and nothing pre-allocated. Checked so an
// allocation failure cannot leave a half-built buffer for
// a worker to fill.
rv = fuse_dirents_init_noreserve(&bd);
if(rv != 0)
return rv;
branch_dirents.emplace_back(bd);
}
futures.reserve(branches_->size());
size_t i = 0;
for(const auto &branch : *branches_)
{
const fs::path *branch_path = &branch.path;
auto func = [branch_path,i,&rel_dirpath_,&branch_dirents]()
{
return ::_readdir(*branch_path,
rel_dirpath_,
&branch_dirents[i]);
};
futures.emplace_back(tp_.enqueue_task(std::move(func)));
i++;
}
for(auto &future : futures)
{
rv = future.get();
if(rv == -ENOMEM)
worker_error = rv;
err = rv;
}
// Do not merge a buffer whose allocation failed partway through.
if(worker_error != 0)
return worker_error;
rv = 0;
for(auto &bd : branch_dirents)
{
rv = _merge_branch_dirents(dirents_,names,&bd);
if(rv != 0)
break;
}
}
catch(const std::bad_alloc&)
{
wait_for_tasks();
return -ENOMEM;
}
catch(...)
{
wait_for_tasks();
return -EIO;
}
Err err;
for(auto &future : futures)
err = future.get();
// A nonzero status means the caller gets an error reply, never the
// listing; keep the cache only for a fully successful fill. The
// DEFER above discards it in every other case.
rv = (rv != 0 ? rv : err);
if(rv == 0)
fill_ok = true;
return err;
return rv;
}
int

View file

@ -23,9 +23,6 @@
#include "fs_inode.hpp"
#include "fs_open.hpp"
#include "fs_path.hpp"
#include "hashset.hpp"
#include "mutex.hpp"
#include "scope_guard/scope_guard.hpp"
#include "fuse_msgbuf.hpp"
@ -37,9 +34,7 @@ inline
int
_readdir(const fs::path &branch_path_,
const fs::path &rel_dirpath_,
HashSet &names_,
fuse_dirents_t *dirents_,
mutex_t &mutex_)
fuse_dirents_t *dirents_)
{
int fd;
fuse_msgbuf_t *buf;
@ -67,7 +62,6 @@ _readdir(const fs::path &branch_path_,
if(nread <= 0)
return nread;
LockGuard lk(mutex_);
for(ssize_t pos = 0; pos < nread;)
{
int rv;
@ -78,16 +72,14 @@ _readdir(const fs::path &branch_path_,
namelen = d->namelen();
rv = names_.put(d->name,namelen);
if(rv == 0)
continue;
d->ino = inodecalc.calc(d->name,
namelen,
DTTOIF(d->type),
d->ino);
d->ino = inodecalc.calc(d->name,
namelen,
DTTOIF(d->type),
d->ino);
fuse_dirents_add(dirents_,d,namelen);
rv = fuse_dirents_add(dirents_,d,namelen);
if(rv != 0)
return rv;
}
}

View file

@ -22,7 +22,6 @@
#include "fs_opendir.hpp"
#include "fs_closedir.hpp"
#include "hashset.hpp"
#include "scope_guard/scope_guard.hpp"
#include "fs_readdir.hpp"
#include "fs_inode.hpp"
@ -52,9 +51,7 @@ inline
int
_readdir(const fs::path &branch_path_,
const fs::path &rel_dirpath_,
HashSet &names_,
fuse_dirents_t *dirents_,
mutex_t &mutex_)
fuse_dirents_t *dirents_)
{
DIR *dir;
fs::path abs_dirpath;
@ -70,7 +67,6 @@ _readdir(const fs::path &branch_path_,
fs::inode::ReaddirCalc inodecalc(branch_path_,rel_dirpath_);
while(true)
{
LockGuard lk(mutex_);
for(int i = 0; i < MAX_ENTRIES_PER_LOOP; i++)
{
int rv;
@ -84,16 +80,14 @@ _readdir(const fs::path &branch_path_,
namelen = ::_dirent_exact_namelen(de);
rv = names_.put(de->d_name,namelen);
if(rv == 0)
continue;
de->d_ino = inodecalc.calc(de->d_name,
namelen,
DTTOIF(de->d_type),
de->d_ino);
de->d_ino = inodecalc.calc(de->d_name,
namelen,
DTTOIF(de->d_type),
de->d_ino);
fuse_dirents_add(dirents_,de,namelen);
rv = fuse_dirents_add(dirents_,de,namelen);
if(rv != 0)
return rv;
}
}

View file

@ -75,6 +75,34 @@ _opendir(ThreadPool &tp_,
return futures;
}
// A failed fill returns before every branch future has been consumed;
// drain the rest so their opened directory fds are closed instead of
// leaking. Futures consumed by the main loop are invalid and skipped.
// A worker exception is swallowed: cleanup must not throw.
static
inline
void
_drain_futures(std::vector<std::future<DirRV>> &dh_futures_)
{
DirRV dirrv;
for(auto &dh_future : dh_futures_)
{
if(not dh_future.valid())
continue;
try
{
dirrv = dh_future.get();
}
catch(...)
{
continue;
}
if(dirrv.fd >= 0)
fs::close(dirrv.fd);
}
}
static
inline
int
@ -88,7 +116,10 @@ _readdir(std::vector<std::future<DirRV>> &dh_futures_,
buf = msgbuf_alloc();
if(not buf)
return -ENOMEM;
{
::_drain_futures(dh_futures_);
return -ENOMEM;
}
DEFER { msgbuf_free(buf); };
for(auto &dh_future : dh_futures_)
@ -130,7 +161,12 @@ _readdir(std::vector<std::future<DirRV>> &dh_futures_,
DTTOIF(d->type),
d->ino);
fuse_dirents_add(dirents_,d,namelen);
rv = fuse_dirents_add(dirents_,d,namelen);
if(rv != 0)
{
::_drain_futures(dh_futures_);
return rv;
}
}
}
}

View file

@ -85,6 +85,34 @@ _opendir(ThreadPool &tp_,
return futures;
}
// A failed fill returns before every branch future has been consumed;
// drain the rest so their opened directory handles are closed instead
// of leaking. Futures consumed by the main loop are invalid and
// skipped. A worker exception is swallowed: cleanup must not throw.
static
inline
void
_drain_futures(std::vector<std::future<DirRV>> &dh_futures_)
{
DirRV dirrv;
for(auto &dh_future : dh_futures_)
{
if(not dh_future.valid())
continue;
try
{
dirrv = dh_future.get();
}
catch(...)
{
continue;
}
if(dirrv.dir != NULL)
fs::closedir(dirrv.dir);
}
}
static
inline
int
@ -125,7 +153,12 @@ _readdir(std::vector<std::future<DirRV>> &dh_futures_,
DTTOIF(de->d_type),
de->d_ino);
fuse_dirents_add(dirents_,de,namelen);
rv = fuse_dirents_add(dirents_,de,namelen);
if(rv != 0)
{
::_drain_futures(dh_futures_);
return rv;
}
}
}

View file

@ -89,7 +89,9 @@ _readdir(const Branches::Ptr &branches_,
DTTOIF(d->type),
d->ino);
fuse_dirents_add(dirents_,d,namelen);
rv = fuse_dirents_add(dirents_,d,namelen);
if(rv != 0)
return rv;
}
}
}

View file

@ -88,7 +88,9 @@ _readdir(const Branches::Ptr &branches_,
DTTOIF(de->d_type),
de->d_ino);
fuse_dirents_add(dirents_,de,namelen);
rv = fuse_dirents_add(dirents_,de,namelen);
if(rv != 0)
return rv;
}
}