2019-03-03 23:26:20 -05:00
|
|
|
/*
|
2026-02-23 13:22:21 -06:00
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2019-03-03 23:26:20 -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.
|
|
|
|
|
*/
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
#include "fuse_futimens.hpp"
|
|
|
|
|
|
2019-03-03 23:26:20 -05:00
|
|
|
#include "errno.hpp"
|
|
|
|
|
#include "fileinfo.hpp"
|
2020-08-17 16:29:22 -04:00
|
|
|
#include "fs_futimens.hpp"
|
2025-06-05 23:47:42 -05:00
|
|
|
#include "state.hpp"
|
2019-03-03 23:26:20 -05:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include "fuse.h"
|
2019-03-03 23:26:20 -05:00
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_futimens(const int fd_,
|
|
|
|
|
const struct timespec ts_[2])
|
2019-03-03 23:26:20 -05:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
int rv;
|
2019-03-03 23:26:20 -05:00
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
rv = fs::futimens(fd_,ts_);
|
2019-03-03 23:26:20 -05:00
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
return rv;
|
2019-03-03 23:26:20 -05:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 09:07:27 -05:00
|
|
|
// In cases where the node was opened but unlinked there will be no
|
|
|
|
|
// path and no guarentee of a `fh`. It could always do the lookup but
|
|
|
|
|
// why bother if the kernel has provided it to us? The reason the
|
|
|
|
|
// function doesn't need to run within the visit lambda is because
|
|
|
|
|
// since the request is outstanding the kernel won't be releasing the
|
|
|
|
|
// node and therefore the entry will be valid over the lifetime of
|
|
|
|
|
// this function.
|
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-08 22:00:13 -05:00
|
|
|
//
|
|
|
|
|
// With neither a `fh` nor an open file return ENOENT. See fgetattr in
|
|
|
|
|
// fuse.h and #1693.
|
2025-07-25 17:53:48 -05:00
|
|
|
int
|
2025-10-13 00:21:36 -04:00
|
|
|
FUSE::futimens(const fuse_req_ctx_t *ctx_,
|
2026-03-22 08:39:06 -05:00
|
|
|
cu64 fh_,
|
|
|
|
|
const struct timespec ts_[2])
|
2019-03-03 23:26:20 -05:00
|
|
|
{
|
2026-04-28 09:07:27 -05:00
|
|
|
FileInfo *fi;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
2026-04-28 09:07:27 -05:00
|
|
|
fi = state.get_fi(ctx_,fh_);
|
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 fi)
|
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-08 22:00:13 -05:00
|
|
|
return -ENOENT;
|
2025-07-25 17:53:48 -05:00
|
|
|
|
|
|
|
|
return ::_futimens(fi->fd,ts_);
|
2019-03-03 23:26:20 -05:00
|
|
|
}
|