2014-05-12 12:41:46 -04:00
|
|
|
/*
|
2026-02-23 13:22:21 -06:00
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
2016-01-14 16:50:22 -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.
|
2014-05-12 12:41:46 -04:00
|
|
|
*/
|
|
|
|
|
|
2026-01-19 15:16:01 -06:00
|
|
|
/*
|
2026-04-02 14:54:46 -05:00
|
|
|
RELEASE OF SHARED AND PER-HANDLE FILEINFO OBJECTS
|
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
|
|
The open_files map stores one canonical FileInfo per nodeid together with a
|
|
|
|
|
ref_count and optional passthrough backing_id. Overlapping opens of the same
|
|
|
|
|
file increment that ref_count but still allocate their own FileInfo objects
|
|
|
|
|
with duplicated file descriptors for the individual FUSE handles.
|
|
|
|
|
|
|
|
|
|
That means release() must handle two ownership cases:
|
|
|
|
|
- the canonical FileInfo stored in open_files
|
|
|
|
|
- a per-handle FileInfo that exists only in fuse_file_info_t.fh
|
|
|
|
|
|
|
|
|
|
On non-final closes, secondary handle FileInfo objects are freed
|
|
|
|
|
immediately after the ref_count decrement while the canonical map entry stays
|
|
|
|
|
alive.
|
|
|
|
|
|
|
|
|
|
On the final close, the map entry is erased atomically and cleanup happens
|
|
|
|
|
after the erase_if() callback returns so close() and delete do not execute
|
|
|
|
|
while the map bucket is locked. If the final handle is different from the
|
|
|
|
|
canonical map-owned FileInfo, both objects must be released.
|
2026-01-19 15:16:01 -06:00
|
|
|
*/
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
#include "fuse_release.hpp"
|
|
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
#include "state.hpp"
|
|
|
|
|
|
2017-02-18 13:45:43 -05:00
|
|
|
#include "config.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fileinfo.hpp"
|
2020-08-17 16:29:22 -04:00
|
|
|
#include "fs_fadvise.hpp"
|
2025-06-05 23:47:42 -05:00
|
|
|
#include "fuse_passthrough.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include "fuse.h"
|
2019-01-06 12:52:55 -05:00
|
|
|
|
2017-02-18 13:45:43 -05:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
static
|
|
|
|
|
int
|
2026-01-19 15:16:01 -06:00
|
|
|
_release(cu64 nodeid_,
|
|
|
|
|
FileInfo *fi_,
|
|
|
|
|
const bool dropcacheonclose_)
|
2025-06-05 23:47:42 -05:00
|
|
|
{
|
2026-04-02 14:54:46 -05:00
|
|
|
// The double fadvise is necessary insofar as according to nocache
|
|
|
|
|
// author (https://github.com/Feh/nocache#limitations) the first one
|
|
|
|
|
// doesn't always work due to timing issues.
|
2025-06-05 23:47:42 -05:00
|
|
|
if(dropcacheonclose_)
|
|
|
|
|
{
|
|
|
|
|
fs::fadvise_dontneed(fi_->fd);
|
|
|
|
|
fs::fadvise_dontneed(fi_->fd);
|
|
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2026-01-19 15:16:01 -06:00
|
|
|
FUSE::release(nodeid_,fi_);
|
2025-06-05 23:47:42 -05:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 15:16:01 -06:00
|
|
|
void
|
|
|
|
|
FUSE::release(cu64 nodeid_,
|
|
|
|
|
FileInfo * const fi_)
|
2025-06-05 23:47:42 -05:00
|
|
|
{
|
2026-04-02 14:54:46 -05:00
|
|
|
auto &of = state.open_files;
|
|
|
|
|
int backing_id = INVALID_BACKING_ID;
|
|
|
|
|
FileInfo *canonical_fi = nullptr;
|
|
|
|
|
FileInfo *fh_fi_to_free = nullptr;
|
2026-01-19 15:16:01 -06:00
|
|
|
|
2026-05-13 11:30:39 -05:00
|
|
|
// memory_order_acq_rel on the decrement gives us the canonical
|
|
|
|
|
// refcount-publication pattern:
|
|
|
|
|
// - release: prior writes to the OpenFile by this thread are
|
|
|
|
|
// visible to whichever thread observes the final 0.
|
|
|
|
|
// - acquire: the thread that drops the last ref acquires every
|
|
|
|
|
// prior ref holder's writes before tearing down the entry.
|
|
|
|
|
// The concurrent_flat_map bucket lock already serializes concurrent
|
|
|
|
|
// visitors of this key so the acq_rel is good for any observer that
|
|
|
|
|
// might examine ref_count outside a visit().
|
2026-01-19 15:16:01 -06:00
|
|
|
of.erase_if(nodeid_,
|
|
|
|
|
[&](auto &v_)
|
|
|
|
|
{
|
2026-05-13 11:30:39 -05:00
|
|
|
const int prev =
|
|
|
|
|
v_.second.ref_count.fetch_sub(1,std::memory_order_acq_rel);
|
|
|
|
|
if(prev > 1)
|
2026-01-19 15:16:01 -06:00
|
|
|
{
|
|
|
|
|
if(fi_ != v_.second.fi)
|
2026-04-02 14:54:46 -05:00
|
|
|
fh_fi_to_free = fi_;
|
2026-01-19 15:16:01 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 14:54:46 -05:00
|
|
|
canonical_fi = v_.second.fi;
|
2026-01-19 15:16:01 -06:00
|
|
|
backing_id = v_.second.backing_id;
|
2026-04-02 14:54:46 -05:00
|
|
|
if(fi_ != canonical_fi)
|
|
|
|
|
fh_fi_to_free = fi_;
|
2026-01-19 15:16:01 -06:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
FUSE::passthrough_close(backing_id);
|
fix: relocate every handle of a file together, after removing the source
- Relocating only the writer left other opens on the old inode. A later
write through one of them could succeed and disappear after close and
reopen through the pool. Every handle of a kernel node, including
opens made after a rename through mergerfs, now belongs to one group.
Prepare a replacement descriptor for each handle and switch the group
together. An open of the file while a relocation runs waits and gets
the moved file.
- Only a node that can be relocated gets a group: one first opened
while moveonenospc is enabled and without a kernel passthrough ID,
which stays on the original file and can't be switched. Every other
node's handles take no lock. Enabling moveonenospc at runtime
therefore applies to files opened afterwards.
- The switch dup2s each replacement onto the handle's own descriptor
number, so a handle's fd never changes while it is open. Operations
that change the file hold the group's lock shared and wait for a
move. Reads, flush and fsync take no lock and continue on the
original, which the copy matches. fgetattr and statx take a second
lock, which a move holds only from the source unlink until the
switch, so they never report the unlinked original.
- A copy is not a committed move. Prepare all bindings first, then
remove the source; if that fails keep every handle on the original,
clean up the provisional copy when it still denotes this copy, and
return only bytes actually written. A handle whose descriptor number
is at or above the descriptor limit refuses the move, the only way
dup2 could fail after the removal.
- The source was removed before its copy reached disk, so a crash could
leave the name without its data. fsync the copy and the directories
leading to it before removing the source, and the source's directory
after, so on a filesystem that can sync them a crash leaves the
original or a complete copy and never brings the original back over
the copy. A failed sync after the removal is logged; the move has
completed by then. fs::copyfile is split into stage and publish steps
for this; the staged descriptor pins the copy for preparation and
cleanup.
- chmod, chown, utimes and xattr changes reach mergerfs by path and
don't wait for a move. Compare ctime as well as size and mtime, so a
change during the copy makes it retry and one after it refuses the
move, rather than being lost with the source.
- The kernel can reach one file through more than one node: a hardlink
whose other name was removed while both were open, a rename made
directly on a branch while the file was open, or overlapping
branches. A move cannot switch another node's handles, which would
keep writing to the original, so it is refused while any other node
has the file open. The mover checks the canonical descriptor of every
open node. That check and the descriptor limit run after the cheaper
checks, so a full pool doesn't pay for them, and before the copy, so a
lasting refusal doesn't cost a copy per write; they run again just
before the unlink. An open through another node that races the move's
last step is not coordinated with it; moveonenospc.md documents this.
- Also refuse to relocate a file with more than one link or an entry
in the rename-exdev staging tree. Each handle keeps a valid path
after the move.
- New shared acquisitions wait while a relocation is pending, so a
stream of writes to allocated blocks can't starve the mover.
- That wait uses std::atomic wait and notify_all, which libstdc++
first provides in GCC 11, so GCC 11 is now the oldest GCC that
builds mergerfs. GCC 10 accepts -std=c++20 and concepts, so
install-build-pkgs and run-rpmbuild also probe for the atomic
operations: such a compiler is rejected up front, or run-rpmbuild
moves on to a newer toolset. build.md names the minimum.
- moveonenospc kept its enable bit and policy in two fields, which
setxattr changed separately while requests read them. Keep one
atomic policy pointer where null means disabled, and read it once
per relocation. tests.cpp covers enabling, disabling and an invalid
value keeping the previous setting.
- The relocation moves out of fuse_write.cpp into moveonenospc.cpp,
where one function holds every reason to refuse a move.
fs::movefile_and_open no longer opens anything and becomes
fs::movefile, which asks the caller's hooks whether to move and to
switch its handles.
- Document the group behaviour, durability and refusals in
moveonenospc.md.
- TEST_movefile_enospc covers preexisting, secondary and read-only
handles, a hardlinked file, a file open through another node, enabling
moveonenospc while a file is open, repeated relocation with a handle
opened between moves, steady writers, concurrent relocation, reads and
fstat during a copy, a mode change during the copy, and failed source
removal with and without a written prefix, checking held handles and
close/reopen contents. Its fixtures truncate the filler before
unlinking it: a scanner holding the file open kept its space
allocated, which made the hardlink case fail intermittently.
Performance: a file that can't be relocated, because moveonenospc was
disabled when it was first opened or it has a passthrough ID, takes no
locks and allocates nothing; writes no longer take the per-handle lock
they used to. For a relocatable file, reads, flush and fsync take no
lock. Writes and other operations that change the file take the
group's lock shared, about two uncontended atomic operations, and
fgetattr and statx take the second lock, on its own cache line, the
same way. An ENOSPC write takes the lock exclusively only when a move
is possible. Opening and closing files adds no syscall; the first open
of a relocatable file allocates its group. A relocation adds an fstat
per open file to find other nodes of the file. While a file is being
relocated, operations that change it and new opens of it wait for the
copy, as the writer already did; reads and fstat continue.
2026-11-25 13:51:33 -05:00
|
|
|
// Deleting a handle unregisters it before closing its descriptor.
|
|
|
|
|
delete fh_fi_to_free;
|
|
|
|
|
delete canonical_fi;
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
int
|
2025-10-13 00:21:36 -04:00
|
|
|
FUSE::release(const fuse_req_ctx_t *ctx_,
|
|
|
|
|
const fuse_file_info_t *ffi_)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2026-01-19 15:16:01 -06:00
|
|
|
FileInfo *fi = FileInfo::from_fh(ffi_->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 -EBADF;
|
|
|
|
|
|
2026-01-19 15:16:01 -06:00
|
|
|
return ::_release(ctx_->nodeid,
|
|
|
|
|
fi,
|
|
|
|
|
cfg.dropcacheonclose);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|