mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-01 04:16:59 -05:00
Add getdents based readdir functions for Linux (#1533)
This commit is contained in:
parent
95ab37fdd2
commit
2f3e807e9d
33 changed files with 886 additions and 904 deletions
|
|
@ -21,6 +21,7 @@
|
|||
#include "int_types.h"
|
||||
#include "strvec.hpp"
|
||||
#include "tofrom_string.hpp"
|
||||
#include "fs_path.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
|
|
@ -44,7 +45,7 @@ public:
|
|||
public:
|
||||
std::variant<u64,const u64*> _minfreespace;
|
||||
Mode mode;
|
||||
std::string path;
|
||||
fs::path path;
|
||||
|
||||
public:
|
||||
Branch();
|
||||
|
|
|
|||
|
|
@ -449,7 +449,7 @@ Branches::find_and_set_mode_ro()
|
|||
continue;
|
||||
|
||||
SysLog::warning("branch `{}` found to be readonly - setting its mode=RO",
|
||||
branch.path);
|
||||
branch.path.string());
|
||||
|
||||
branch.mode = Branch::Mode::RO;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ public:
|
|||
if(!_err.has_value())
|
||||
_err = v_;
|
||||
else if(v_ >= 0)
|
||||
_err = v_;
|
||||
_err = 0;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@
|
|||
class FileInfo : public FH
|
||||
{
|
||||
public:
|
||||
static FileInfo *from_fh(const uint64_t fh);
|
||||
static FileInfo *from_fh(const uintptr_t fh);
|
||||
|
||||
public:
|
||||
FileInfo(const int fd_,
|
||||
|
|
@ -72,10 +72,10 @@ public:
|
|||
};
|
||||
|
||||
inline
|
||||
uint64_t
|
||||
uintptr_t
|
||||
FileInfo::to_fh() const
|
||||
{
|
||||
return reinterpret_cast<uint64_t>(this);
|
||||
return reinterpret_cast<uintptr_t>(this);
|
||||
}
|
||||
|
||||
inline
|
||||
|
|
|
|||
|
|
@ -26,13 +26,13 @@
|
|||
|
||||
namespace fs
|
||||
{
|
||||
int
|
||||
getdents64(unsigned int fd_,
|
||||
ssize_t
|
||||
getdents64(const int fd_,
|
||||
void *dirp_,
|
||||
unsigned int count_)
|
||||
const size_t count_)
|
||||
{
|
||||
#if defined SYS_getdents64
|
||||
int rv;
|
||||
ssize_t rv;
|
||||
|
||||
rv = ::syscall(SYS_getdents64,fd_,dirp_,count_);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
|
||||
Copyright (c) 2025, 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
|
||||
|
|
@ -18,11 +18,13 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
namespace fs
|
||||
{
|
||||
int
|
||||
getdents64(unsigned int fd,
|
||||
ssize_t
|
||||
getdents64(const int fd,
|
||||
void *dirp,
|
||||
unsigned int count);
|
||||
const size_t count);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,7 @@
|
|||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef STATX_TYPE
|
||||
#define MERGERFS_STATX_SUPPORTED
|
||||
#endif
|
||||
#include "supported_statx.hpp"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
|
|
@ -28,7 +26,7 @@ namespace fs
|
|||
const unsigned int mask_,
|
||||
struct fuse_statx *st_)
|
||||
{
|
||||
#ifdef MERGERFS_STATX_SUPPORTED
|
||||
#ifdef MERGERFS_SUPPORTED_STATX
|
||||
int rv;
|
||||
|
||||
rv = ::statx(dirfd_,
|
||||
|
|
@ -52,6 +50,10 @@ namespace fs
|
|||
const unsigned int mask_,
|
||||
struct fuse_statx *st_)
|
||||
{
|
||||
return fs::statx(dirfd_,pathname_.c_str(),flags_,mask_,st_);
|
||||
return fs::statx(dirfd_,
|
||||
pathname_.c_str(),
|
||||
flags_,
|
||||
mask_,
|
||||
st_);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
#include "fuse_readdir_factory.hpp"
|
||||
|
||||
#include "dirinfo.hpp"
|
||||
#include "fuse_dirents.h"
|
||||
#include "fuse_dirents.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
|
|
@ -82,7 +82,7 @@ _handle_ENOENT(const fuse_file_info_t *ffi_,
|
|||
dirent de;
|
||||
DirInfo *di = DirInfo::from_fh(ffi_->fh);
|
||||
|
||||
if(di->fusepath != "/")
|
||||
if(!di->fusepath.empty())
|
||||
return -ENOENT;
|
||||
|
||||
de = {0};
|
||||
|
|
|
|||
|
|
@ -16,29 +16,13 @@
|
|||
|
||||
#include "fuse_readdir_cor.hpp"
|
||||
|
||||
#include "supported_getdents64.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
#include "dirinfo.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_close.hpp"
|
||||
#include "fs_closedir.hpp"
|
||||
#include "fs_getdents64.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_opendir.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "fs_readdir.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
#include "error.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include "fuse_dirents.h"
|
||||
#include "linux_dirent64.h"
|
||||
|
||||
#include "int_types.h"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#define MAX_ENTRIES_PER_LOOP 64
|
||||
|
||||
|
||||
FUSE::ReadDirCOR::ReadDirCOR(unsigned concurrency_,
|
||||
unsigned max_queue_depth_)
|
||||
|
|
@ -52,113 +36,11 @@ FUSE::ReadDirCOR::~ReadDirCOR()
|
|||
|
||||
}
|
||||
|
||||
static
|
||||
u64
|
||||
_dirent_exact_namelen(const struct dirent *d_)
|
||||
{
|
||||
#ifdef _D_EXACT_NAMLEN
|
||||
return _D_EXACT_NAMLEN(d_);
|
||||
#elif defined _DIRENT_HAVE_D_NAMLEN
|
||||
return d_->d_namlen;
|
||||
#ifdef MERGERFS_SUPPORTED_GETDENTS64
|
||||
#include "fuse_readdir_cor_getdents.icpp"
|
||||
#else
|
||||
return strlen(d_->d_name);
|
||||
#include "fuse_readdir_cor_readdir.icpp"
|
||||
#endif
|
||||
}
|
||||
|
||||
struct Error
|
||||
{
|
||||
private:
|
||||
int _err;
|
||||
|
||||
public:
|
||||
Error()
|
||||
: _err(ENOENT)
|
||||
{
|
||||
}
|
||||
|
||||
operator int()
|
||||
{
|
||||
return _err;
|
||||
}
|
||||
|
||||
Error&
|
||||
operator=(int v_)
|
||||
{
|
||||
if(_err != 0)
|
||||
_err = v_;
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
static
|
||||
inline
|
||||
int
|
||||
_readdir(const fs::path &branch_path_,
|
||||
const fs::path &rel_dirpath_,
|
||||
HashSet &names_,
|
||||
fuse_dirents_t *buf_,
|
||||
std::mutex &mutex_)
|
||||
{
|
||||
int rv;
|
||||
DIR *dir;
|
||||
fs::path rel_filepath;
|
||||
fs::path abs_dirpath;
|
||||
|
||||
abs_dirpath = branch_path_ / rel_dirpath_;
|
||||
|
||||
errno = 0;
|
||||
dir = fs::opendir(abs_dirpath);
|
||||
if(dir == NULL)
|
||||
return errno;
|
||||
|
||||
DEFER{ fs::closedir(dir); };
|
||||
|
||||
// The default buffer size in glibc is 32KB. 64 is based on a
|
||||
// guess of an average size of 512B per entry. This is to limit
|
||||
// contention on the lock when needing to request more data from
|
||||
// the kernel. This could be handled better by using getdents but
|
||||
// due to the differences between Linux and FreeBSD a more
|
||||
// complicated abstraction needs to be created to handle them to
|
||||
// make it easier to use across the codebase.
|
||||
// * https://man7.org/linux/man-pages/man2/getdents.2.html
|
||||
// * https://man.freebsd.org/cgi/man.cgi?query=getdents
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
while(true)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mutex_);
|
||||
for(int i = 0; i < MAX_ENTRIES_PER_LOOP; i++)
|
||||
{
|
||||
dirent *de;
|
||||
u64 namelen;
|
||||
|
||||
de = fs::readdir(dir);
|
||||
if(de == NULL)
|
||||
return 0;
|
||||
|
||||
namelen = ::_dirent_exact_namelen(de);
|
||||
|
||||
rv = names_.put(de->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(de->d_name);
|
||||
|
||||
de->d_ino = fs::inode::calc(branch_path_,
|
||||
rel_filepath,
|
||||
DTTOIF(de->d_type),
|
||||
de->d_ino);
|
||||
|
||||
rv = fuse_dirents_add(buf_,de,namelen);
|
||||
if(rv >= 0)
|
||||
continue;
|
||||
|
||||
return ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
|
|
@ -166,7 +48,7 @@ int
|
|||
_concurrent_readdir(ThreadPool &tp_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *buf_,
|
||||
fuse_dirents_t *dirents_,
|
||||
const uid_t uid_,
|
||||
const gid_t gid_)
|
||||
{
|
||||
|
|
@ -174,20 +56,20 @@ _concurrent_readdir(ThreadPool &tp_,
|
|||
std::mutex mutex;
|
||||
std::vector<std::future<int>> futures;
|
||||
|
||||
fuse_dirents_reset(buf_);
|
||||
fuse_dirents_reset(dirents_);
|
||||
futures.reserve(branches_->size());
|
||||
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
auto func =
|
||||
[&,buf_,uid_,gid_]()
|
||||
[&,dirents_,uid_,gid_]()
|
||||
{
|
||||
const ugid::Set ugid(uid_,gid_);
|
||||
|
||||
return ::_readdir(branch.path,
|
||||
rel_dirpath_,
|
||||
names,
|
||||
buf_,
|
||||
dirents_,
|
||||
mutex);
|
||||
};
|
||||
|
||||
|
|
@ -196,16 +78,16 @@ _concurrent_readdir(ThreadPool &tp_,
|
|||
futures.emplace_back(std::move(rv));
|
||||
}
|
||||
|
||||
Error error;
|
||||
Err err;
|
||||
for(auto &future : futures)
|
||||
error = future.get();
|
||||
err = future.get();
|
||||
|
||||
return -error;
|
||||
return err;
|
||||
}
|
||||
|
||||
int
|
||||
FUSE::ReadDirCOR::operator()(const fuse_file_info_t *ffi_,
|
||||
fuse_dirents_t *buf_)
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
DirInfo *di = DirInfo::from_fh(ffi_->fh);
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
|
|
@ -213,7 +95,7 @@ FUSE::ReadDirCOR::operator()(const fuse_file_info_t *ffi_,
|
|||
return ::_concurrent_readdir(_tp,
|
||||
cfg.branches,
|
||||
di->fusepath,
|
||||
buf_,
|
||||
dirents_,
|
||||
fc->uid,
|
||||
fc->gid);
|
||||
}
|
||||
|
|
|
|||
75
src/fuse_readdir_cor_getdents.icpp
Normal file
75
src/fuse_readdir_cor_getdents.icpp
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#pragma message "using getdents"
|
||||
|
||||
#include "fs_close.hpp"
|
||||
#include "fs_getdents64.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_open.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
|
||||
#include "fuse_msgbuf.hpp"
|
||||
#include "fuse_dirents.hpp"
|
||||
|
||||
|
||||
static
|
||||
inline
|
||||
int
|
||||
_readdir(const fs::path &branch_path_,
|
||||
const fs::path &rel_dirpath_,
|
||||
HashSet &names_,
|
||||
fuse_dirents_t *dirents_,
|
||||
std::mutex &mutex_)
|
||||
{
|
||||
int fd;
|
||||
fuse_msgbuf_t *buf;
|
||||
fs::path rel_filepath;
|
||||
fs::path abs_dirpath;
|
||||
|
||||
abs_dirpath = branch_path_ / rel_dirpath_;
|
||||
|
||||
fd = fs::open_dir_ro(abs_dirpath);
|
||||
if(fd < 0)
|
||||
return fd;
|
||||
DEFER{ fs::close(fd); };
|
||||
|
||||
buf = msgbuf_alloc();
|
||||
DEFER{ msgbuf_free(buf); };
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
while(true)
|
||||
{
|
||||
ssize_t nread;
|
||||
|
||||
nread = fs::getdents64(fd,buf->mem,buf->size);
|
||||
if(nread <= 0)
|
||||
return nread;
|
||||
|
||||
std::lock_guard<std::mutex> lk(mutex_);
|
||||
for(ssize_t pos = 0; pos < nread;)
|
||||
{
|
||||
int rv;
|
||||
int namelen;
|
||||
fs::dirent64 *d = reinterpret_cast<fs::dirent64*>(&buf->mem[pos]);
|
||||
|
||||
pos += d->d_reclen;
|
||||
|
||||
namelen = d->d_namelen();
|
||||
|
||||
rv = names_.put(d->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(d->d_name);
|
||||
|
||||
d->d_ino = fs::inode::calc(branch_path_,
|
||||
rel_filepath,
|
||||
DTTOIF(d->d_type),
|
||||
d->d_ino);
|
||||
|
||||
fuse_dirents_add(dirents_,d,namelen);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
86
src/fuse_readdir_cor_readdir.icpp
Normal file
86
src/fuse_readdir_cor_readdir.icpp
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#pragma message "using readdir"
|
||||
|
||||
#define MAX_ENTRIES_PER_LOOP 64
|
||||
|
||||
#include "fs_opendir.hpp"
|
||||
#include "fs_closedir.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
#include "fs_readdir.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
|
||||
#include "fuse_dirents.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
|
||||
static
|
||||
u64
|
||||
_dirent_exact_namelen(const struct dirent *d_)
|
||||
{
|
||||
#ifdef _D_EXACT_NAMLEN
|
||||
return _D_EXACT_NAMLEN(d_);
|
||||
#elif defined _DIRENT_HAVE_D_NAMLEN
|
||||
return d_->d_namlen;
|
||||
#else
|
||||
return strlen(d_->d_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
int
|
||||
_readdir(const fs::path &branch_path_,
|
||||
const fs::path &rel_dirpath_,
|
||||
HashSet &names_,
|
||||
fuse_dirents_t *dirents_,
|
||||
std::mutex &mutex_)
|
||||
{
|
||||
DIR *dir;
|
||||
fs::path rel_filepath;
|
||||
fs::path abs_dirpath;
|
||||
|
||||
abs_dirpath = branch_path_ / rel_dirpath_;
|
||||
|
||||
errno = 0;
|
||||
dir = fs::opendir(abs_dirpath);
|
||||
if(dir == NULL)
|
||||
return -errno;
|
||||
DEFER{ fs::closedir(dir); };
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
while(true)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(mutex_);
|
||||
for(int i = 0; i < MAX_ENTRIES_PER_LOOP; i++)
|
||||
{
|
||||
int rv;
|
||||
int namelen;
|
||||
dirent *de;
|
||||
|
||||
errno = 0;
|
||||
de = fs::readdir(dir);
|
||||
if(de == NULL)
|
||||
return -errno;
|
||||
|
||||
namelen = ::_dirent_exact_namelen(de);
|
||||
|
||||
rv = names_.put(de->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(de->d_name);
|
||||
|
||||
de->d_ino = fs::inode::calc(branch_path_,
|
||||
rel_filepath,
|
||||
DTTOIF(de->d_type),
|
||||
de->d_ino);
|
||||
|
||||
fuse_dirents_add(dirents_,de,namelen);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -18,19 +18,7 @@
|
|||
|
||||
#include "config.hpp"
|
||||
#include "dirinfo.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_closedir.hpp"
|
||||
#include "fs_dirfd.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_opendir.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "fs_readdir.hpp"
|
||||
#include "fs_stat.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include "fuse_dirents.h"
|
||||
#include "supported_getdents64.hpp"
|
||||
|
||||
|
||||
FUSE::ReadDirCOSR::ReadDirCOSR(unsigned concurrency_,
|
||||
|
|
@ -45,143 +33,11 @@ FUSE::ReadDirCOSR::~ReadDirCOSR()
|
|||
|
||||
}
|
||||
|
||||
struct DirRV
|
||||
{
|
||||
const std::string *branch_path;
|
||||
DIR *dir;
|
||||
int err;
|
||||
};
|
||||
|
||||
struct Error
|
||||
{
|
||||
private:
|
||||
int _err;
|
||||
|
||||
public:
|
||||
Error()
|
||||
: _err(ENOENT)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
operator int()
|
||||
{
|
||||
return _err;
|
||||
}
|
||||
|
||||
Error&
|
||||
operator=(int const v_)
|
||||
{
|
||||
if(_err != 0)
|
||||
_err = v_;
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
static
|
||||
uint64_t
|
||||
_dirent_exact_namelen(const struct dirent *d_)
|
||||
{
|
||||
#ifdef _D_EXACT_NAMLEN
|
||||
return _D_EXACT_NAMLEN(d_);
|
||||
#elif defined _DIRENT_HAVE_D_NAMLEN
|
||||
return d_->d_namlen;
|
||||
#ifdef MERGERFS_SUPPORTED_GETDENTS64
|
||||
#include "fuse_readdir_cosr_getdents.icpp"
|
||||
#else
|
||||
return strlen(d_->d_name);
|
||||
#include "fuse_readdir_cosr_readdir.icpp"
|
||||
#endif
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
std::vector<std::future<DirRV>>
|
||||
_opendir(ThreadPool &tp_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
uid_t const uid_,
|
||||
gid_t const gid_)
|
||||
{
|
||||
std::vector<std::future<DirRV>> futures;
|
||||
|
||||
futures.reserve(branches_->size());
|
||||
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
auto func =
|
||||
[&branch,&rel_dirpath_,uid_,gid_]()
|
||||
{
|
||||
DIR *dir;
|
||||
fs::path abs_dirpath;
|
||||
const ugid::Set ugid(uid_,gid_);
|
||||
|
||||
abs_dirpath = branch.path / rel_dirpath_;
|
||||
|
||||
errno = 0;
|
||||
dir = fs::opendir(abs_dirpath);
|
||||
|
||||
return DirRV{&branch.path,dir,errno};
|
||||
};
|
||||
|
||||
auto rv = tp_.enqueue_task(std::move(func));
|
||||
|
||||
futures.emplace_back(std::move(rv));
|
||||
}
|
||||
|
||||
return futures;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
int
|
||||
_readdir(std::vector<std::future<DirRV>> &dh_futures_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *buf_)
|
||||
{
|
||||
Error error;
|
||||
HashSet names;
|
||||
fs::path rel_filepath;
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
for(auto &dh_future : dh_futures_)
|
||||
{
|
||||
int rv;
|
||||
DirRV dirrv;
|
||||
|
||||
dirrv = dh_future.get();
|
||||
|
||||
error = dirrv.err;
|
||||
if(dirrv.dir == NULL)
|
||||
continue;
|
||||
|
||||
DEFER { fs::closedir(dirrv.dir); };
|
||||
|
||||
rv = 0;
|
||||
for(dirent *de = fs::readdir(dirrv.dir); de && !rv; de = fs::readdir(dirrv.dir))
|
||||
{
|
||||
std::uint64_t namelen;
|
||||
|
||||
namelen = ::_dirent_exact_namelen(de);
|
||||
|
||||
rv = names.put(de->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(de->d_name);
|
||||
de->d_ino = fs::inode::calc(*dirrv.branch_path,
|
||||
rel_filepath,
|
||||
DTTOIF(de->d_type),
|
||||
de->d_ino);
|
||||
|
||||
rv = fuse_dirents_add(buf_,de,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
error = ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
return -error;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
|
|
@ -189,24 +45,24 @@ int
|
|||
_readdir(ThreadPool &tp_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *buf_,
|
||||
fuse_dirents_t *dirents_,
|
||||
uid_t const uid_,
|
||||
gid_t const gid_)
|
||||
{
|
||||
int rv;
|
||||
std::vector<std::future<DirRV>> futures;
|
||||
|
||||
fuse_dirents_reset(buf_);
|
||||
fuse_dirents_reset(dirents_);
|
||||
|
||||
futures = ::_opendir(tp_,branches_,rel_dirpath_,uid_,gid_);
|
||||
rv = ::_readdir(futures,rel_dirpath_,buf_);
|
||||
rv = ::_readdir(futures,rel_dirpath_,dirents_);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
int
|
||||
FUSE::ReadDirCOSR::operator()(fuse_file_info_t const *ffi_,
|
||||
fuse_dirents_t *buf_)
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
DirInfo *di = DirInfo::from_fh(ffi_->fh);
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
|
|
@ -214,7 +70,7 @@ FUSE::ReadDirCOSR::operator()(fuse_file_info_t const *ffi_,
|
|||
return ::_readdir(_tp,
|
||||
cfg.branches,
|
||||
di->fusepath,
|
||||
buf_,
|
||||
dirents_,
|
||||
fc->uid,
|
||||
fc->gid);
|
||||
}
|
||||
|
|
|
|||
125
src/fuse_readdir_cosr_getdents.icpp
Normal file
125
src/fuse_readdir_cosr_getdents.icpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#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.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_,
|
||||
uid_t const uid_,
|
||||
gid_t const gid_)
|
||||
{
|
||||
std::vector<std::future<DirRV>> futures;
|
||||
|
||||
futures.reserve(branches_->size());
|
||||
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
auto func =
|
||||
[&branch,&rel_dirpath_,uid_,gid_]()
|
||||
{
|
||||
int fd;
|
||||
fs::path abs_dirpath;
|
||||
const ugid::Set ugid(uid_,gid_);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
int
|
||||
_readdir(std::vector<std::future<DirRV>> &dh_futures_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
Err err;
|
||||
HashSet names;
|
||||
fs::path rel_filepath;
|
||||
fuse_msgbuf_t *buf;
|
||||
|
||||
buf = msgbuf_alloc();
|
||||
DEFER { msgbuf_free(buf); };
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
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); };
|
||||
|
||||
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->d_reclen;
|
||||
|
||||
namelen = d->d_namelen();
|
||||
|
||||
rv = names.put(d->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(d->d_name);
|
||||
|
||||
d->d_ino = fs::inode::calc(*dirrv.branch_path,
|
||||
rel_filepath,
|
||||
DTTOIF(d->d_type),
|
||||
d->d_ino);
|
||||
|
||||
fuse_dirents_add(dirents_,d,namelen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
121
src/fuse_readdir_cosr_readdir.icpp
Normal file
121
src/fuse_readdir_cosr_readdir.icpp
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
#pragma message "using readdir"
|
||||
|
||||
#include "error.hpp"
|
||||
#include "fs_closedir.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_opendir.hpp"
|
||||
#include "fs_readdir.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include "fuse_dirents.hpp"
|
||||
|
||||
|
||||
struct DirRV
|
||||
{
|
||||
const fs::path *branch_path;
|
||||
DIR *dir;
|
||||
int err;
|
||||
};
|
||||
|
||||
static
|
||||
uint64_t
|
||||
_dirent_exact_namelen(const struct dirent *d_)
|
||||
{
|
||||
#ifdef _D_EXACT_NAMLEN
|
||||
return _D_EXACT_NAMLEN(d_);
|
||||
#elif defined _DIRENT_HAVE_D_NAMLEN
|
||||
return d_->d_namlen;
|
||||
#else
|
||||
return strlen(d_->d_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
std::vector<std::future<DirRV>>
|
||||
_opendir(ThreadPool &tp_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
uid_t const uid_,
|
||||
gid_t const gid_)
|
||||
{
|
||||
std::vector<std::future<DirRV>> futures;
|
||||
|
||||
futures.reserve(branches_->size());
|
||||
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
auto func =
|
||||
[&branch,&rel_dirpath_,uid_,gid_]()
|
||||
{
|
||||
DIR *dir;
|
||||
fs::path abs_dirpath;
|
||||
const ugid::Set ugid(uid_,gid_);
|
||||
|
||||
abs_dirpath = branch.path / rel_dirpath_;
|
||||
|
||||
errno = 0;
|
||||
dir = fs::opendir(abs_dirpath);
|
||||
|
||||
return DirRV{&branch.path,dir,-errno};
|
||||
};
|
||||
|
||||
auto rv = tp_.enqueue_task(std::move(func));
|
||||
|
||||
futures.emplace_back(std::move(rv));
|
||||
}
|
||||
|
||||
return futures;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
int
|
||||
_readdir(const std::vector<std::future<DirRV>> &dh_futures_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
Err err;
|
||||
HashSet names;
|
||||
fs::path rel_filepath;
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
for(auto &dh_future : dh_futures_)
|
||||
{
|
||||
DirRV dirrv;
|
||||
|
||||
dirrv = dh_future.get();
|
||||
|
||||
err = dirrv.err;
|
||||
if(dirrv.dir == NULL)
|
||||
continue;
|
||||
DEFER { fs::closedir(dirrv.dir); };
|
||||
|
||||
for(dirent *de = fs::readdir(dirrv.dir);
|
||||
de;
|
||||
de = fs::readdir(dirrv.dir))
|
||||
{
|
||||
int rv;
|
||||
int namelen;
|
||||
|
||||
namelen = ::_dirent_exact_namelen(de);
|
||||
|
||||
rv = names.put(de->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(de->d_name);
|
||||
|
||||
de->d_ino = fs::inode::calc(*dirrv.branch_path,
|
||||
rel_filepath,
|
||||
DTTOIF(de->d_type),
|
||||
de->d_ino);
|
||||
|
||||
fuse_dirents_add(dirents_,de,namelen);
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
|
@ -18,99 +18,19 @@
|
|||
|
||||
#include "fuse_readdir_seq.hpp"
|
||||
|
||||
#include "error.hpp"
|
||||
#include "branches.hpp"
|
||||
#include "config.hpp"
|
||||
#include "dirinfo.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_closedir.hpp"
|
||||
#include "fs_dirfd.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_opendir.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "fs_readdir.hpp"
|
||||
#include "fs_stat.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
#include "supported_getdents64.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include "fuse.h"
|
||||
#include "fuse_dirents.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
static
|
||||
uint64_t
|
||||
_dirent_exact_namelen(const struct dirent *d_)
|
||||
{
|
||||
#ifdef _D_EXACT_NAMLEN
|
||||
return _D_EXACT_NAMLEN(d_);
|
||||
#elif defined _DIRENT_HAVE_D_NAMLEN
|
||||
return d_->d_namlen;
|
||||
#ifdef MERGERFS_SUPPORTED_GETDENTS64
|
||||
#include "fuse_readdir_seq_getdents.icpp"
|
||||
#else
|
||||
return strlen(d_->d_name);
|
||||
#include "fuse_readdir_seq_readdir.icpp"
|
||||
#endif
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_readdir(const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *buf_)
|
||||
{
|
||||
Err err;
|
||||
HashSet names;
|
||||
fs::path rel_filepath;
|
||||
fs::path abs_dirpath;
|
||||
|
||||
fuse_dirents_reset(buf_);
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
int rv;
|
||||
DIR *dh;
|
||||
|
||||
abs_dirpath = branch.path / rel_dirpath_;
|
||||
|
||||
errno = 0;
|
||||
dh = fs::opendir(abs_dirpath);
|
||||
err = -errno;
|
||||
if(!dh)
|
||||
continue;
|
||||
|
||||
DEFER{ fs::closedir(dh); };
|
||||
|
||||
rv = 0;
|
||||
for(dirent *de = fs::readdir(dh); de; de = fs::readdir(dh))
|
||||
{
|
||||
std::uint64_t namelen;
|
||||
|
||||
namelen = ::_dirent_exact_namelen(de);
|
||||
|
||||
rv = names.put(de->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(de->d_name);
|
||||
de->d_ino = fs::inode::calc(branch.path,
|
||||
rel_filepath,
|
||||
DTTOIF(de->d_type),
|
||||
de->d_ino);
|
||||
|
||||
rv = fuse_dirents_add(buf_,de,namelen);
|
||||
if(rv)
|
||||
return -ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int
|
||||
FUSE::ReadDirSeq::operator()(fuse_file_info_t const *ffi_,
|
||||
fuse_dirents_t *buf_)
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
DirInfo *di = DirInfo::from_fh(ffi_->fh);
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
|
|
@ -118,5 +38,5 @@ FUSE::ReadDirSeq::operator()(fuse_file_info_t const *ffi_,
|
|||
|
||||
return ::_readdir(cfg.branches,
|
||||
di->fusepath,
|
||||
buf_);
|
||||
dirents_);
|
||||
}
|
||||
|
|
|
|||
81
src/fuse_readdir_seq_getdents.icpp
Normal file
81
src/fuse_readdir_seq_getdents.icpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#pragma message "using getdents"
|
||||
|
||||
#include "dirinfo.hpp"
|
||||
#include "error.hpp"
|
||||
#include "fs_close.hpp"
|
||||
#include "fs_getdents64.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "fs_open.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
|
||||
#include "fuse_dirents.hpp"
|
||||
#include "fuse_msgbuf.hpp"
|
||||
|
||||
|
||||
static
|
||||
int
|
||||
_readdir(const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
Err err;
|
||||
HashSet names;
|
||||
fs::path rel_filepath;
|
||||
fs::path abs_dirpath;
|
||||
fuse_msgbuf_t *buf;
|
||||
|
||||
fuse_dirents_reset(dirents_);
|
||||
|
||||
buf = msgbuf_alloc();
|
||||
DEFER { msgbuf_free(buf); };
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
int fd;
|
||||
|
||||
abs_dirpath = branch.path / rel_dirpath_;
|
||||
|
||||
fd = fs::open_dir_ro(abs_dirpath);
|
||||
err = fd;
|
||||
if(fd < 0)
|
||||
continue;
|
||||
DEFER{ fs::close(fd); };
|
||||
|
||||
while(true)
|
||||
{
|
||||
ssize_t nread;
|
||||
|
||||
nread = fs::getdents64(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->d_reclen;
|
||||
|
||||
namelen = d->d_namelen();
|
||||
|
||||
rv = names.put(d->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(d->d_name);
|
||||
|
||||
d->d_ino = fs::inode::calc(branch.path,
|
||||
rel_filepath,
|
||||
DTTOIF(d->d_type),
|
||||
d->d_ino);
|
||||
|
||||
fuse_dirents_add(dirents_,d,namelen);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
81
src/fuse_readdir_seq_readdir.icpp
Normal file
81
src/fuse_readdir_seq_readdir.icpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#pragma message "using readdir"
|
||||
|
||||
#include "fs_opendir.hpp"
|
||||
#include "fs_closedir.hpp"
|
||||
#include "fs_readdir.hpp"
|
||||
#include "error.hpp"
|
||||
#include "hashset.hpp"
|
||||
#include "scope_guard.hpp"
|
||||
#include "fs_inode.hpp"
|
||||
#include "dirinfo.hpp"
|
||||
|
||||
#include "fuse_dirents.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
static
|
||||
uint64_t
|
||||
_dirent_exact_namelen(const struct dirent *d_)
|
||||
{
|
||||
#ifdef _D_EXACT_NAMLEN
|
||||
return _D_EXACT_NAMLEN(d_);
|
||||
#elif defined _DIRENT_HAVE_D_NAMLEN
|
||||
return d_->d_namlen;
|
||||
#else
|
||||
return strlen(d_->d_name);
|
||||
#endif
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_readdir(const Branches::Ptr &branches_,
|
||||
const fs::path &rel_dirpath_,
|
||||
fuse_dirents_t *dirents_)
|
||||
{
|
||||
Err err;
|
||||
HashSet names;
|
||||
fs::path rel_filepath;
|
||||
fs::path abs_dirpath;
|
||||
|
||||
fuse_dirents_reset(buf_);
|
||||
|
||||
rel_filepath = rel_dirpath_ / "dummy";
|
||||
for(const auto &branch : *branches_)
|
||||
{
|
||||
DIR *dh;
|
||||
|
||||
abs_dirpath = branch.path / rel_dirpath_;
|
||||
|
||||
errno = 0;
|
||||
dh = fs::opendir(abs_dirpath);
|
||||
err = -errno;
|
||||
if(!dh)
|
||||
continue;
|
||||
DEFER{ fs::closedir(dh); };
|
||||
|
||||
for(dirent *de = fs::readdir(dh);
|
||||
de;
|
||||
de = fs::readdir(dh))
|
||||
{
|
||||
int rv;
|
||||
int namelen;
|
||||
|
||||
namelen = ::_dirent_exact_namelen(de);
|
||||
|
||||
rv = names.put(de->d_name,namelen);
|
||||
if(rv == 0)
|
||||
continue;
|
||||
|
||||
rel_filepath.replace_filename(de->d_name);
|
||||
|
||||
de->d_ino = fs::inode::calc(branch.path,
|
||||
rel_filepath,
|
||||
DTTOIF(de->d_type),
|
||||
de->d_ino);
|
||||
|
||||
fuse_dirents_add(buf_,de,namelen);
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
#include "fs_statx.hpp"
|
||||
|
||||
#ifdef MERGERFS_STATX_SUPPORTED
|
||||
#ifdef MERGERFS_SUPPORTED_STATX
|
||||
#pragma message "statx supported"
|
||||
# include "fuse_statx_supported.icpp"
|
||||
#else
|
||||
|
|
|
|||
11
src/supported_getdents64.hpp
Normal file
11
src/supported_getdents64.hpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <sys/syscall.h>
|
||||
|
||||
#ifdef SYS_getdents64
|
||||
#define MERGERFS_SUPPORTED_GETDENTS64
|
||||
#endif
|
||||
|
||||
#endif
|
||||
12
src/supported_statx.hpp
Normal file
12
src/supported_statx.hpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef STATX_TYPE
|
||||
#define MERGERFS_SUPPORTED_STATX
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue