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"
|
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
|
|
|
|
2026-05-13 10:58:01 -05:00
|
|
|
// 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.
|
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
|
|
|
{
|
|
|
|
|
HashSet names;
|
2026-02-20 22:27:55 -06:00
|
|
|
Mutex mutex;
|
2025-09-17 23:51:16 -04:00
|
|
|
std::vector<std::future<int>> futures;
|
2023-08-02 18:15:36 -05:00
|
|
|
|
2025-09-24 00:51:21 -04:00
|
|
|
fuse_dirents_reset(dirents_);
|
2025-09-17 23:51:16 -04:00
|
|
|
futures.reserve(branches_->size());
|
2023-08-20 23:06:05 -05:00
|
|
|
|
2025-09-17 23:51:16 -04:00
|
|
|
for(const auto &branch : *branches_)
|
|
|
|
|
{
|
|
|
|
|
auto func =
|
2025-12-05 21:03:48 -06:00
|
|
|
[&,dirents_]()
|
2025-09-17 23:51:16 -04:00
|
|
|
{
|
|
|
|
|
return ::_readdir(branch.path,
|
|
|
|
|
rel_dirpath_,
|
|
|
|
|
names,
|
2025-09-24 00:51:21 -04:00
|
|
|
dirents_,
|
2025-09-17 23:51:16 -04:00
|
|
|
mutex);
|
|
|
|
|
};
|
2023-08-02 18:15:36 -05:00
|
|
|
|
2025-09-17 23:51:16 -04:00
|
|
|
auto rv = tp_.enqueue_task(std::move(func));
|
2023-08-02 18:15:36 -05:00
|
|
|
|
2025-09-17 23:51:16 -04:00
|
|
|
futures.emplace_back(std::move(rv));
|
|
|
|
|
}
|
2023-08-02 18:15:36 -05:00
|
|
|
|
2025-09-24 00:51:21 -04:00
|
|
|
Err err;
|
2025-09-17 23:51:16 -04:00
|
|
|
for(auto &future : futures)
|
2025-09-24 00:51:21 -04:00
|
|
|
err = future.get();
|
2023-08-02 18:15:36 -05:00
|
|
|
|
2025-09-24 00:51:21 -04:00
|
|
|
return err;
|
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
|
|
|
}
|