2023-08-02 18:15:36 -05:00
|
|
|
/*
|
2026-02-23 13:22:21 -06:00
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2023-08-02 18:15:36 -05:00
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "fuse_readdir_cor.hpp"
|
|
|
|
|
|
2025-09-24 00:51:21 -04:00
|
|
|
#include "supported_getdents64.hpp"
|
|
|
|
|
|
2023-08-02 18:15:36 -05:00
|
|
|
#include "config.hpp"
|
|
|
|
|
#include "dirinfo.hpp"
|
2025-09-24 00:51:21 -04:00
|
|
|
#include "error.hpp"
|
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-05-13 10:58:01 -05:00
|
|
|
#include "fuse_dirents.hpp"
|
|
|
|
|
#include "hashset.hpp"
|
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <new>
|
2023-08-02 18:15:36 -05:00
|
|
|
|
|
|
|
|
|
2023-09-24 17:55:09 -05:00
|
|
|
FUSE::ReadDirCOR::ReadDirCOR(unsigned concurrency_,
|
|
|
|
|
unsigned max_queue_depth_)
|
|
|
|
|
: _tp(concurrency_,max_queue_depth_,"readdir.cor")
|
2023-08-02 18:15:36 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FUSE::ReadDirCOR::~ReadDirCOR()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-24 00:51:21 -04:00
|
|
|
#ifdef MERGERFS_SUPPORTED_GETDENTS64
|
|
|
|
|
#include "fuse_readdir_cor_getdents.icpp"
|
2025-06-24 19:31:40 -05:00
|
|
|
#else
|
2025-09-24 00:51:21 -04:00
|
|
|
#include "fuse_readdir_cor_readdir.icpp"
|
2025-06-24 19:31:40 -05:00
|
|
|
#endif
|
2025-09-17 23:51:16 -04:00
|
|
|
|
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-05-13 10:58:01 -05:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 23:51:16 -04:00
|
|
|
static
|
|
|
|
|
inline
|
|
|
|
|
int
|
|
|
|
|
_concurrent_readdir(ThreadPool &tp_,
|
FUSE: pass Branches::Ptr by value with empty-branches guards
Previously most FUSE ops took a reference to the live cfg.branches
object. With atomic_load now producing a shared_ptr snapshot, pass
it by value: the op works against a single stable view of the
configuration for its whole lifetime, immune to concurrent
reconfiguration.
Also bail out with -ENOENT when the policy returns an empty branch
set rather than dereferencing branches[0]. This was reachable if a
policy rejected every branch (e.g. everything readonly while
looking for a writable target).
Covers access, chmod, chown, create, getattr, getxattr, ioctl,
link, listxattr, mkdir, mknod, open, readdir (cor/cosr),
readlink, removexattr, rename, rmdir, setxattr, statfs, statx,
symlink, truncate, unlink, utimens.
While here, in _getattr only dereference st_->st_mode when lstat
succeeded (it is otherwise uninitialized).
2026-04-18 00:56:15 -04:00
|
|
|
const Branches::Ptr branches_,
|
2025-09-17 23:51:16 -04:00
|
|
|
const fs::path &rel_dirpath_,
|
2025-12-05 21:03:48 -06:00
|
|
|
fuse_dirents_t *dirents_)
|
2025-09-17 23:51:16 -04:00
|
|
|
{
|
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-05-13 10:58:01 -05:00
|
|
|
int rv;
|
|
|
|
|
int worker_error;
|
|
|
|
|
Err err;
|
2025-09-17 23:51:16 -04:00
|
|
|
HashSet names;
|
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-05-13 10:58:01 -05:00
|
|
|
bool fill_ok;
|
|
|
|
|
std::vector<fuse_dirents_t> branch_dirents;
|
2025-09-17 23:51:16 -04:00
|
|
|
std::vector<std::future<int>> futures;
|
2023-08-02 18:15:36 -05:00
|
|
|
|
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-05-13 10:58:01 -05:00
|
|
|
worker_error = 0;
|
|
|
|
|
fill_ok = false;
|
2025-09-24 00:51:21 -04:00
|
|
|
fuse_dirents_reset(dirents_);
|
2023-08-20 23:06:05 -05:00
|
|
|
|
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-05-13 10:58:01 -05:00
|
|
|
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]()
|
2025-09-17 23:51:16 -04:00
|
|
|
{
|
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-05-13 10:58:01 -05:00
|
|
|
// No queued task may still reference locals during cleanup.
|
|
|
|
|
for(auto &future : futures)
|
2025-09-17 23:51:16 -04:00
|
|
|
{
|
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-05-13 10:58:01 -05:00
|
|
|
if(not future.valid())
|
|
|
|
|
continue;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
future.wait();
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
branch_dirents.reserve(branches_->size());
|
|
|
|
|
for(size_t i = 0; i < branches_->size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
fuse_dirents_t bd;
|
|
|
|
|
|
|
|
|
|
// 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;
|
2025-09-17 23:51:16 -04:00
|
|
|
}
|
2023-08-02 18:15:36 -05:00
|
|
|
|
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-05-13 10:58:01 -05:00
|
|
|
// 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;
|
2023-08-02 18:15:36 -05:00
|
|
|
|
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-05-13 10:58:01 -05:00
|
|
|
return rv;
|
2023-08-02 18:15:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2025-10-13 00:21:36 -04:00
|
|
|
FUSE::ReadDirCOR::operator()(const fuse_req_ctx_t *ctx_,
|
|
|
|
|
const fuse_file_info_t *ffi_,
|
2025-09-24 00:51:21 -04:00
|
|
|
fuse_dirents_t *dirents_)
|
2023-08-02 18:15:36 -05:00
|
|
|
{
|
2025-10-13 00:21:36 -04:00
|
|
|
DirInfo *di = DirInfo::from_fh(ffi_->fh);
|
2023-08-02 18:15:36 -05:00
|
|
|
|
FUSE: require valid file/dir handle; drop open_files fallback
Several FUSE ops fell back to looking up a FileInfo via
state.open_files when the kernel-provided fh was zero, returning
-ENOENT if the lookup failed. This path papered over handle-tracking
bugs and added cost on every call. Use the handle directly and
return -EBADF when it is invalid - the kernel should always hand us
a valid fh for an open resource.
Also simplify FileInfo::from_fh / DirInfo::from_fh and their to_fh
helpers: callers now handle the null-pointer case explicitly so the
ASSERT_NOT_NULL checks are redundant.
Covers fallocate, fchmod, fchown, fgetattr, flock, ftruncate,
futimens, read, release, releasedir, write, statx, ioctl (file +
dir), readdir (sequential, concurrent-on-read,
concurrent-on-search), and the passthrough fast paths in
create/open.
2026-04-18 00:53:11 -04:00
|
|
|
if(not di)
|
|
|
|
|
return -EBADF;
|
|
|
|
|
|
2025-09-17 23:51:16 -04:00
|
|
|
return ::_concurrent_readdir(_tp,
|
2025-09-06 12:03:51 -04:00
|
|
|
cfg.branches,
|
2025-04-06 01:03:26 -05:00
|
|
|
di->fusepath,
|
2025-12-05 21:03:48 -06:00
|
|
|
dirents_);
|
2023-08-02 18:15:36 -05:00
|
|
|
}
|