mergerfs/src/fuse_statx_supported.icpp
Antonio S. Musumeci 9d12bcf235 Return ENOENT, not EBADF, for removed nodes with no open file
Once a file or directory is removed (unlink, rmdir, rename over it)
libfuse has no path for its node and falls back to the fh based
getattr/setattr/statx with fh=0. If mergerfs had no open file for the
node (a directory, an O_PATH fd, a cwd, or a dentry resolved just
before the removal) it returned EBADF. The kernel sends GETATTR for
such nodes in the permission checks of a path walk, so lstat() of a
directory, or of a path below it, could fail with EBADF while rm -rf
removed it. Two rm -rf at once, one on a directory and one on the
directory above it, met it when one removed a directory the other
still had open: the other's unlinkat() through its dirfd failed, rm
reported "Bad file descriptor" and left entries behind.

mergerfs now returns ENOENT. That is what a local filesystem gives
*at() calls in a removed directory, and what rm -f ignores. ESTALE
would make the kernel redo a path based lookup, which helps a stat
racing a rename over the path. But a call through a dirfd or cwd of a
removed directory meets the same node again and fails anyway, and the
concurrent rm -rf still did. Calls on the removed object itself, which
succeed on a local filesystem, get ENOENT instead of EBADF.

TEST_rm_rf_concurrent runs the two cases reported. The rm -rf one is
a narrow race; TEST_use_dirfd_after_rmdir covers its cause without
one, with cache.attr=0 so the kernel sends GETATTR every time.

See #1693.
2026-11-17 12:25:50 -06:00

285 lines
6.7 KiB
Text

/*
ISC License
Copyright (c) 2026, 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
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_statx.hpp"
#include "config.hpp"
#include "errno.hpp"
#include "fileinfo.hpp"
#include "fs_inode.hpp"
#include "fs_path.hpp"
#include "fs_statx.hpp"
#include "state.hpp"
#include "str.hpp"
#include "symlinkify.hpp"
#include "ugid.hpp"
#include "fmt/core.h"
#include "fuse.h"
#include <string>
static
void
_set_stat_if_leads_to_dir(const fs::path &path_,
struct fuse_statx *st_)
{
int rv;
struct fuse_statx st;
rv = fs::statx(AT_FDCWD,path_,AT_SYMLINK_FOLLOW,STATX_TYPE,&st);
if(rv < 0)
return;
if(S_ISDIR(st.mode))
*st_ = st;
return;
}
static
void
_set_stat_if_leads_to_reg(const fs::path &path_,
struct fuse_statx *st_)
{
int rv;
struct fuse_statx st;
rv = fs::statx(AT_FDCWD,path_,AT_SYMLINK_FOLLOW,STATX_TYPE,&st);
if(rv < 0)
return;
if(S_ISREG(st.mode))
*st_ = st;
return;
}
static
int
_statx_fake_root(struct fuse_statx *st_,
fuse_timeouts_t *timeout_)
{
st_->ino = 0;
st_->mode = (S_IFDIR|S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
st_->nlink = 2;
st_->uid = 0;
st_->gid = 0;
st_->size = 0;
st_->blocks = 0;
st_->atime.tv_sec = 0;
st_->mtime.tv_sec = 0;
st_->ctime.tv_sec = 0;
timeout_->entry = cfg.cache_entry;
timeout_->attr = cfg.cache_attr;
return 0;
}
static
int
_statx_controlfile(struct fuse_statx *st_,
fuse_timeouts_t *timeout_)
{
static const uid_t uid = ::getuid();
static const gid_t gid = ::getgid();
static const time_t now = ::time(NULL);
st_->ino = 0;
st_->mode = (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
st_->nlink = 1;
st_->uid = uid;
st_->gid = gid;
st_->size = 0;
st_->blocks = 0;
st_->atime.tv_sec = now;
st_->mtime.tv_sec = now;
st_->ctime.tv_sec = now;
timeout_->entry = cfg.cache_entry;
timeout_->attr = cfg.cache_attr;
return 0;
}
static
int
_statx(const Policy::Search &searchFunc_,
const Branches::Ptr branches_,
const fs::path &fusepath_,
const uint32_t flags_,
const uint32_t mask_,
struct fuse_statx *st_,
const bool symlinkify_,
const time_t symlinkify_timeout_,
FollowSymlinks followsymlinks_)
{
int rv;
fs::path fullpath;
std::vector<Branch*> branches;
rv = searchFunc_(branches_,fusepath_,branches);
if(rv < 0)
return rv;
if(branches.empty())
return -ENOENT;
fullpath = branches[0]->path / fusepath_;
switch(followsymlinks_)
{
case FollowSymlinks::ENUM::NEVER:
rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_);
break;
case FollowSymlinks::ENUM::DIRECTORY:
rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_);
if((rv >= 0) && S_ISLNK(st_->mode))
::_set_stat_if_leads_to_dir(fullpath,st_);
break;
case FollowSymlinks::ENUM::REGULAR:
rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_);
if((rv >= 0) && S_ISLNK(st_->mode))
::_set_stat_if_leads_to_reg(fullpath,st_);
break;
case FollowSymlinks::ENUM::ALL:
rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_FOLLOW,mask_,st_);
if(rv < 0)
rv = fs::statx(AT_FDCWD,fullpath,flags_|AT_SYMLINK_NOFOLLOW,mask_,st_);
break;
}
if(rv < 0)
return rv;
if(symlinkify_ && symlinkify::can_be_symlink(*st_,symlinkify_timeout_))
symlinkify::convert(fullpath,st_);
fs::inode::calc(branches[0]->path,
fusepath_,
st_);
return 0;
}
static
int
_statx(const fs::path &fusepath_,
const uint32_t flags_,
const uint32_t mask_,
struct fuse_statx *st_,
fuse_timeouts_t *timeout_)
{
int rv;
rv = ::_statx(cfg.func.getattr.policy,
cfg.branches,
fusepath_,
flags_,
mask_,
st_,
cfg.symlinkify,
cfg.symlinkify_timeout,
cfg.follow_symlinks);
if((rv < 0) && Config::is_rootdir(fusepath_))
return ::_statx_fake_root(st_,timeout_);
timeout_->entry = ((rv >= 0) ?
cfg.cache_entry :
cfg.cache_negative_entry);
timeout_->attr = cfg.cache_attr;
return rv;
}
static
int
_statx(const FileInfo *fi_,
const uint32_t flags_,
const uint32_t mask_,
struct fuse_statx *st_)
{
int rv;
rv = fs::statx(fi_->fd,
"",
flags_|AT_EMPTY_PATH,
mask_,
st_);
if(rv < 0)
return rv;
fs::inode::calc(fi_->branch.path,
fi_->fusepath,
st_);
return rv;
}
int
FUSE::statx(const fuse_req_ctx_t *ctx_,
const char *fusepath_,
const uint32_t flags_,
const uint32_t mask_,
struct fuse_statx *st_,
fuse_timeouts_t *timeout_)
{
const fs::path fusepath{fusepath_};
if(Config::is_ctrl_file(fusepath))
return ::_statx_controlfile(st_,timeout_);
return ::_statx(fusepath,
flags_|AT_STATX_DONT_SYNC,
mask_,
st_,
timeout_);
}
// See fgetattr in fuse.h for why a node with no `fh` and no open file
// returns ENOENT.
int
FUSE::statx_fh(const fuse_req_ctx_t *ctx_,
const uint64_t fh_,
const uint32_t flags_,
const uint32_t mask_,
struct fuse_statx *st_,
fuse_timeouts_t *timeout_)
{
int rv;
FileInfo *fi;
fi = state.get_fi(ctx_,fh_);
if(not fi)
return -ENOENT;
rv = ::_statx(fi,
flags_|AT_STATX_DONT_SYNC,
mask_,
st_);
timeout_->entry = ((rv >= 0) ?
cfg.cache_entry :
cfg.cache_negative_entry);
timeout_->attr = cfg.cache_attr;
return rv;
}