Auto-commit fuse.cpp

This commit is contained in:
Antonio SJ Musumeci 2026-04-13 18:55:56 -05:00
parent d5a2187713
commit 02da8efbff
37 changed files with 374 additions and 267 deletions

View file

@ -34,7 +34,7 @@
#include <fnmatch.h>
Branches::Impl::Impl(const u64 &default_minfreespace_)
Branches::Impl::Impl(u64 default_minfreespace_)
: _default_minfreespace(default_minfreespace_)
{
}
@ -48,6 +48,12 @@ Branches::Impl::operator=(Branches::Impl &rval_)
*this_base = *rval_base;
for(auto &branch : *this)
{
if(std::holds_alternative<const u64*>(branch._minfreespace))
branch._minfreespace = &_default_minfreespace;
}
return *this;
}
@ -60,6 +66,12 @@ Branches::Impl::operator=(Branches::Impl &&rval_)
*this_base = std::move(*rval_base);
for(auto &branch : *this)
{
if(std::holds_alternative<const u64*>(branch._minfreespace))
branch._minfreespace = &_default_minfreespace;
}
return *this;
}
@ -148,7 +160,7 @@ namespace l
case 2:
*glob_ = v[0];
options = v[1];
v = str::split(options,',');
v = str::split(options,',');
switch(v.size())
{
case 2:
@ -440,7 +452,9 @@ Branches::to_string(void) const
void
Branches::find_and_set_mode_ro()
{
for(auto &branch : *_impl)
auto impl = std::atomic_load(&_impl);
for(auto &branch : *impl)
{
if(branch.mode != Branch::Mode::RW)
continue;

View file

@ -39,13 +39,13 @@ public:
public std::vector<Branch>
{
private:
const u64 &_default_minfreespace;
u64 _default_minfreespace;
public:
using Ptr = std::shared_ptr<Impl>;
public:
Impl(const u64 &default_minfreespace);
Impl(u64 default_minfreespace);
public:
int from_string(const std::string_view str) final;

View file

@ -26,7 +26,8 @@
LogFile::LogFile(const std::string &s_)
{
fuse_debug_set_output(s_);
if(!s_.empty())
fuse_debug_set_output(s_);
}
std::string

View file

@ -27,6 +27,7 @@
class DirInfo : public FH
{
public:
static u64 to_fh(const DirInfo*);
static DirInfo *from_fh(const u64 fh);
public:
@ -39,23 +40,23 @@ public:
}
};
inline
u64
DirInfo::to_fh(const DirInfo *di_)
{
return reinterpret_cast<u64>(di_);
}
inline
u64
DirInfo::to_fh() const
{
ASSERT_NOT_NULL(this);
return reinterpret_cast<u64>(this);
return to_fh(this);
}
inline
DirInfo*
DirInfo::from_fh(const u64 fh_)
{
DirInfo *rv;
rv = reinterpret_cast<DirInfo*>(fh_);
ASSERT_NOT_NULL(rv);
return rv;
return reinterpret_cast<DirInfo*>(fh_);
}

View file

@ -104,7 +104,6 @@ inline
u64
FileInfo::to_fh(const FileInfo *fi_)
{
ASSERT_NOT_NULL(fi_);
return reinterpret_cast<u64>(fi_);
}
@ -112,19 +111,12 @@ inline
u64
FileInfo::to_fh() const
{
ASSERT_NOT_NULL(this);
return reinterpret_cast<u64>(this);
return to_fh(this);
}
inline
FileInfo*
FileInfo::from_fh(const u64 fh_)
{
FileInfo *rv;
rv = reinterpret_cast<FileInfo*>(fh_);
ASSERT_NOT_NULL(rv);
return rv;
return reinterpret_cast<FileInfo*>(fh_);
}

View file

@ -292,6 +292,9 @@ _create(const fuse_req_ctx_t *ctx_,
FileInfo *fi = FileInfo::from_fh(ffi_->fh);
int backing_id = INVALID_BACKING_ID;
if(not fi)
return -EBADF;
switch(_(cfg.passthrough_io,ffi_->flags))
{
case _(PassthroughIO::ENUM::RO,O_RDONLY):

View file

@ -48,22 +48,10 @@ FUSE::fallocate(const fuse_req_ctx_t *ctx_,
off_t offset_,
off_t len_)
{
u64 fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
return -ENOENT;
FileInfo *fi = FileInfo::from_fh(fh);
if(not fi)
return -EBADF;
return ::_fallocate(fi->fd,
mode_,

View file

@ -43,22 +43,10 @@ FUSE::fchmod(const fuse_req_ctx_t *ctx_,
cu64 fh_,
const mode_t mode_)
{
u64 fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
return -ENOENT;
FileInfo *fi = FileInfo::from_fh(fh);
if(not fi)
return -EBADF;
return ::_fchmod(fi->fd,mode_);
}

View file

@ -47,22 +47,10 @@ FUSE::fchown(const fuse_req_ctx_t *ctx_,
const uid_t uid_,
const gid_t gid_)
{
u64 fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
return -ENOENT;
FileInfo *fi = FileInfo::from_fh(fh);
if(not fi)
return -EBADF;
return ::_fchown(fi->fd,uid_,gid_);
}

View file

@ -54,26 +54,14 @@ FUSE::fgetattr(const fuse_req_ctx_t *ctx_,
fuse_timeouts_t *timeout_)
{
int rv;
u64 fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](const auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
if(not fi)
{
timeout_->entry = cfg.cache_negative_entry;
return -ENOENT;
return -EBADF;
}
FileInfo *fi = FileInfo::from_fh(fh);
rv = ::_fgetattr(fi,st_);
timeout_->entry = ((rv >= 0) ?

View file

@ -43,22 +43,10 @@ FUSE::flock(const fuse_req_ctx_t *ctx_,
const fuse_file_info_t *ffi_,
int op_)
{
u64 fh;
FileInfo* fi = FileInfo::from_fh(ffi_->fh);
fh = ffi_->fh;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](const auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
return -ENOENT;
FileInfo* fi = FileInfo::from_fh(fh);
if(not fi)
return -EBADF;
return ::_flock(fi->fd,op_);
}

View file

@ -43,22 +43,10 @@ FUSE::ftruncate(const fuse_req_ctx_t *ctx_,
cu64 fh_,
off_t size_)
{
u64 fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
return -ENOENT;
FileInfo *fi = FileInfo::from_fh(fh);
if(not fi)
return -EBADF;
return ::_ftruncate(fi->fd,size_);
}

View file

@ -45,22 +45,10 @@ FUSE::futimens(const fuse_req_ctx_t *ctx_,
cu64 fh_,
const struct timespec ts_[2])
{
u64 fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](const auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(fh == 0)
return -ENOENT;
FileInfo *fi = FileInfo::from_fh(fh);
if(not fi)
return -EBADF;
return ::_futimens(fi->fd,ts_);
}

View file

@ -32,6 +32,7 @@
#include "fs_open.hpp"
#include "fs_path.hpp"
#include "mergerfs_ioctl.hpp"
#include "state.hpp"
#include "str.hpp"
#include <string>
@ -115,6 +116,9 @@ _ioctl_file(const fuse_req_ctx_t *ctx_,
{
FileInfo *fi = FileInfo::from_fh(ffi_->fh);
if(not fi)
return -EBADF;
return ::_ioctl(fi->fd,cmd_,data_,out_bufsz_);
}
@ -163,6 +167,9 @@ _ioctl_dir(const fuse_req_ctx_t *ctx_,
{
DirInfo *di = DirInfo::from_fh(ffi_->fh);
if(not di)
return -EBADF;
return ::_ioctl_dir_base(cfg.func.open.policy,
cfg.branches,
di->fusepath.c_str(),

View file

@ -25,9 +25,9 @@
simultaneously attempt to open the same file (same nodeid), and we
need to:
1. Share a single canonical FileInfo entry in the open_files map for all
opens of the same file while still giving each FUSE handle its own
FileInfo object and file descriptor
1. Share a single canonical FileInfo entry in the open_files map for
all opens of the same file while still giving each FUSE handle
its own FileInfo object and file descriptor
2. Properly reference count to know when to release resources
3. Handle race conditions without deadlocks or resource leaks
4. Support FUSE passthrough mode for performance
@ -45,8 +45,8 @@
FileInfo by re-opening the canonical file descriptor
3. If it doesn't exist, we create a new FileInfo and try to insert it
4. If insertion fails (another thread inserted first), we clean up
and retry (because to use passthrough it MUST be the same underlying
file as the backing id was created)
and retry (because to use passthrough it MUST be the same
underlying file as the backing id was created)
THE RETRY LOOP
--------------
@ -423,6 +423,8 @@ _open(const fuse_req_ctx_t *ctx_,
return rv;
fi = FileInfo::from_fh(ffi_->fh);
if(not fi)
return -EBADF;
switch(_(cfg.passthrough_io,ffi_->flags))
{

View file

@ -67,6 +67,9 @@ FUSE::read(const fuse_req_ctx_t *ctx_,
ioprio::SetFrom iop(ctx_->pid);
FileInfo *fi = FileInfo::from_fh(ffi_->fh);
if(not fi)
return -EBADF;
if(fi->direct_io)
return ::_read_direct_io(fi->fd,buf_,size_,offset_);

View file

@ -84,18 +84,24 @@ _handle_ENOENT(const fuse_file_info_t *ffi_,
dirent de;
DirInfo *di = DirInfo::from_fh(ffi_->fh);
if(not di)
return -EBADF;
if(!di->fusepath.empty())
return -ENOENT;
constexpr const char msg[] =
"error: no valid mergerfs branch found, check config";
de = {};
de.d_ino = 0;
de.d_off = 0;
de.d_type = DT_UNKNOWN;
strcpy(de.d_name,"error: no valid mergerfs branch found, check config");
strncpy(de.d_name,msg,(sizeof(msg) - 1));
de.d_reclen = sizeof(de);
fuse_dirents_add(buf_,&de,::strlen(de.d_name));
fuse_dirents_add(buf_,&de,(sizeof(msg) - 1));
return 0;
}

View file

@ -22,6 +22,7 @@
#include "tofrom_string.hpp"
#include "fuse_readdir_base.hpp"
#include <atomic>
#include <memory>
#include <mutex>
@ -58,7 +59,7 @@ namespace FUSE
void initialize();
private:
bool _initialized = false;
std::atomic<bool> _initialized = false;
std::shared_ptr<std::string> _str;
std::shared_ptr<FUSE::ReadDirBase> _impl;
};

View file

@ -89,6 +89,9 @@ FUSE::ReadDirCOR::operator()(const fuse_req_ctx_t *ctx_,
{
DirInfo *di = DirInfo::from_fh(ffi_->fh);
if(not di)
return -EBADF;
return ::_concurrent_readdir(_tp,
cfg.branches,
di->fusepath,

View file

@ -53,6 +53,9 @@ _readdir(const fs::path &branch_path_,
DEFER{ fs::close(fd); };
buf = msgbuf_alloc();
if(not buf)
return -ENOMEM;
DEFER{ msgbuf_free(buf); };
fs::inode::ReaddirCalc inodecalc(branch_path_,rel_dirpath_);

View file

@ -67,6 +67,9 @@ FUSE::ReadDirCOSR::operator()(const fuse_req_ctx_t *ctx_,
{
DirInfo *di = DirInfo::from_fh(ffi_->fh);
if(not di)
return -EBADF;
return ::_readdir(_tp,
cfg.branches,
di->fusepath,

View file

@ -87,6 +87,8 @@ _readdir(std::vector<std::future<DirRV>> &dh_futures_,
fuse_msgbuf_t *buf;
buf = msgbuf_alloc();
if(not buf)
return -ENOMEM;
DEFER { msgbuf_free(buf); };
for(auto &dh_future : dh_futures_)

View file

@ -36,6 +36,9 @@ FUSE::ReadDirSeq::operator()(const fuse_req_ctx_t *ctx_,
{
DirInfo *di = DirInfo::from_fh(ffi_->fh);
if(not di)
return -EBADF;
return ::_readdir(cfg.branches,
di->fusepath,
dirents_);

View file

@ -45,6 +45,8 @@ _readdir(const Branches::Ptr &branches_,
fuse_dirents_reset(dirents_);
buf = msgbuf_alloc();
if(not buf)
return -ENOMEM;
DEFER { msgbuf_free(buf); };
for(const auto &branch : *branches_)

View file

@ -40,12 +40,11 @@ _readlink_core_standard(const fs::path &fullpath_,
{
int rv;
rv = fs::readlink(fullpath_,buf_,bufsize_);
rv = fs::readlink(fullpath_,buf_,(bufsize_ - 1));
if(rv < 0)
return rv;
if((size_t)rv < bufsize_)
buf_[rv] = '\0';
buf_[rv] = '\0';
return 0;
}
@ -71,10 +70,9 @@ _readlink_core_symlinkify(const fs::path &fullpath_,
if(!symlinkify::can_be_symlink(st,symlinkify_timeout_))
return ::_readlink_core_standard(fullpath_,buf_,bufsize_);
n = std::min(fullpath_str.size(),bufsize_);
n = std::min(fullpath_str.size(),(bufsize_ - 1));
memcpy(buf_,fullpath_str.c_str(),n);
if(n < bufsize_)
buf_[n] = '\0';
buf_[n] = '\0';
return 0;
}

View file

@ -119,6 +119,9 @@ FUSE::release(const fuse_req_ctx_t *ctx_,
{
FileInfo *fi = FileInfo::from_fh(ffi_->fh);
if(not fi)
return -EBADF;
return ::_release(ctx_->nodeid,
fi,
cfg.dropcacheonclose);

View file

@ -39,5 +39,8 @@ FUSE::releasedir(const fuse_req_ctx_t *ctx_,
{
DirInfo *di = DirInfo::from_fh(ffi_->fh);
if(not di)
return -EBADF;
return ::_releasedir(di);
}

View file

@ -41,9 +41,13 @@ _normalize_statvfs(struct statvfs *fsstat_,
const unsigned long min_frsize_,
const unsigned long min_namemax_)
{
fsstat_->f_blocks = (fsblkcnt_t)((fsstat_->f_blocks * fsstat_->f_frsize) / min_frsize_);
fsstat_->f_bfree = (fsblkcnt_t)((fsstat_->f_bfree * fsstat_->f_frsize) / min_frsize_);
fsstat_->f_bavail = (fsblkcnt_t)((fsstat_->f_bavail * fsstat_->f_frsize) / min_frsize_);
long double scale;
scale = (((long double)fsstat_->f_frsize) / ((long double)min_frsize_));
fsstat_->f_blocks = (fsblkcnt_t)(fsstat_->f_blocks * scale);
fsstat_->f_bfree = (fsblkcnt_t)(fsstat_->f_bfree * scale);
fsstat_->f_bavail = (fsblkcnt_t)(fsstat_->f_bavail * scale);
fsstat_->f_bsize = min_bsize_;
fsstat_->f_frsize = min_frsize_;
fsstat_->f_namemax = min_namemax_;

View file

@ -253,27 +253,15 @@ FUSE::statx_fh(const fuse_req_ctx_t *ctx_,
fuse_timeouts_t *timeout_)
{
int rv;
uint64_t fh;
FileInfo *fi = FileInfo::from_fh(fh_);
fh = fh_;
if(fh == 0)
{
state.open_files.cvisit(ctx_->nodeid,
[&](auto &val_)
{
fh = val_.second.fi->to_fh();
});
}
if(not fi)
return -EBADF;
if(fh == 0)
return -ENOENT;
FileInfo *fi = FileInfo::from_fh(fh);
rv = ::_statx(fi,
flags_|AT_STATX_DONT_SYNC,
mask_,
st_);
rv = ::_statx(fi,
flags_|AT_STATX_DONT_SYNC,
mask_,
st_);
timeout_->entry = ((rv >= 0) ?
cfg.cache_entry :

View file

@ -168,6 +168,8 @@ _write(const fuse_file_info_t *ffi_,
FileInfo *fi;
fi = FileInfo::from_fh(ffi_->fh);
if(not fi)
return -EBADF;
// Concurrent writes can only happen if:
// 1) writeback-cache is enabled and using page caching