mergerfs/src/fuse_readdir_cor.cpp

245 lines
6.4 KiB
C++
Raw Normal View History

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"
#include "supported_getdents64.hpp"
2023-08-02 18:15:36 -05:00
#include "config.hpp"
#include "dirinfo.hpp"
#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()
{
}
#ifdef MERGERFS_SUPPORTED_GETDENTS64
#include "fuse_readdir_cor_getdents.icpp"
#else
#include "fuse_readdir_cor_readdir.icpp"
#endif
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;
}
static
inline
int
_concurrent_readdir(ThreadPool &tp_,
const Branches::Ptr branches_,
const fs::path &rel_dirpath_,
fuse_dirents_t *dirents_)
{
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;
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;
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;
fuse_dirents_reset(dirents_);
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]()
{
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)
{
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;
}
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
FUSE::ReadDirCOR::operator()(const fuse_req_ctx_t *ctx_,
const fuse_file_info_t *ffi_,
fuse_dirents_t *dirents_)
2023-08-02 18:15:36 -05:00
{
DirInfo *di = DirInfo::from_fh(ffi_->fh);
2023-08-02 18:15:36 -05:00
if(not di)
return -EBADF;
return ::_concurrent_readdir(_tp,
cfg.branches,
di->fusepath,
dirents_);
2023-08-02 18:15:36 -05:00
}