/* ISC License Copyright (c) 2026, Antonio SJ Musumeci 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" #include "config.hpp" #include "dirinfo.hpp" #include "error.hpp" #include "fuse_dirents.hpp" #include "hashset.hpp" #include #include FUSE::ReadDirCOR::ReadDirCOR(unsigned concurrency_, unsigned max_queue_depth_) : _tp(concurrency_,max_queue_depth_,"readdir.cor") { } FUSE::ReadDirCOR::~ReadDirCOR() { } #ifdef MERGERFS_SUPPORTED_GETDENTS64 #include "fuse_readdir_cor_getdents.icpp" #else #include "fuse_readdir_cor_readdir.icpp" #endif // 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(&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_) { int rv; int worker_error; Err err; HashSet names; bool fill_ok; std::vector branch_dirents; std::vector> futures; worker_error = 0; fill_ok = false; fuse_dirents_reset(dirents_); 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]() { // No queued task may still reference locals during cleanup. for(auto &future : futures) { 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; } // 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 rv; } int FUSE::ReadDirCOR::operator()(const fuse_req_ctx_t *ctx_, const fuse_file_info_t *ffi_, fuse_dirents_t *dirents_) { DirInfo *di = DirInfo::from_fh(ffi_->fh); if(not di) return -EBADF; return ::_concurrent_readdir(_tp, cfg.branches, di->fusepath, dirents_); }