mergerfs/src/fuse_readdir_cosr_getdents.icpp
Antonio SJ Musumeci eea1c1bc09 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.
2026-11-29 23:11:07 -05:00

175 lines
4.1 KiB
Text

/*
ISC License
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma message "using getdents"
#include "fs_inode.hpp"
#include "fs_getdents64.hpp"
#include "hashset.hpp"
#include "branches.hpp"
#include "error.hpp"
#include "fs_close.hpp"
#include "fs_open.hpp"
#include "fs_path.hpp"
#include "ugid.hpp"
#include "scope_guard/scope_guard.hpp"
#include "fuse_msgbuf.hpp"
#include "fuse_dirents.hpp"
#include <dirent.h>
struct DirRV
{
const fs::path *branch_path;
int fd;
};
static
inline
std::vector<std::future<DirRV>>
_opendir(ThreadPool &tp_,
const Branches::Ptr &branches_,
const fs::path &rel_dirpath_)
{
std::vector<std::future<DirRV>> futures;
futures.reserve(branches_->size());
for(const auto &branch : *branches_)
{
auto func =
[&branch,&rel_dirpath_]()
{
int fd;
fs::path abs_dirpath;
abs_dirpath = branch.path / rel_dirpath_;
fd = fs::open_dir_ro(abs_dirpath);
return DirRV{&branch.path,fd};
};
auto rv = tp_.enqueue_task(std::move(func));
futures.emplace_back(std::move(rv));
}
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
_readdir(std::vector<std::future<DirRV>> &dh_futures_,
const fs::path &rel_dirpath_,
fuse_dirents_t *dirents_)
{
Err err;
HashSet names;
fuse_msgbuf_t *buf;
buf = msgbuf_alloc();
if(not buf)
{
::_drain_futures(dh_futures_);
return -ENOMEM;
}
DEFER { msgbuf_free(buf); };
for(auto &dh_future : dh_futures_)
{
DirRV dirrv;
dirrv = dh_future.get();
err = dirrv.fd;
if(dirrv.fd < 0)
continue;
DEFER { fs::close(dirrv.fd); };
fs::inode::ReaddirCalc inodecalc(*dirrv.branch_path,rel_dirpath_);
while(true)
{
ssize_t nread;
nread = fs::getdents64(dirrv.fd,buf->mem,buf->size);
if(nread <= 0)
break;
for(ssize_t pos = 0; pos < nread;)
{
int rv;
int namelen;
fs::dirent64 *d = reinterpret_cast<fs::dirent64*>(&buf->mem[pos]);
pos += d->reclen;
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);
rv = fuse_dirents_add(dirents_,d,namelen);
if(rv != 0)
{
::_drain_futures(dh_futures_);
return rv;
}
}
}
}
return err;
}