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.
This commit is contained in:
Antonio SJ Musumeci 2026-11-25 13:51:33 -05:00 • committed by Antonio S. Musumeci
parent 93ccc160df
commit c49989b75f
38 changed files with 2324 additions and 496 deletions

View file

@ -29,18 +29,74 @@ will:
2. ["Move"](../faq/technical_behavior_and_limitations.md#how-does-mergerfs-handle-moving-and-copying-of-files)
the file from the source branch to the target branch. If another entry
appears at the destination during the copy, the move fails without
overwriting that entry or removing the source.
overwriting that entry or removing the source. The copy, its name and
any directories created for it are flushed to disk before the source
is removed, and the removal is flushed too. On a filesystem that can
sync files and directories, a crash therefore leaves either the
original or a complete copy; a removal that could not be synced is
logged. Before source removal, mergerfs opens the destination
separately for every open handle with that handle's access flags.
Writes and other changes made through an open handle (truncation,
allocation, `ioctl`) and new opens of the file wait while the move is
prepared. Reads and `fsync` continue against the original, which the
copy matches, until each handle switches. Changes made by path, which
is how the kernel sends `chmod`, `chown`, `utimes` and xattr changes
even for an open file, do not wait: if one reaches the original after
it was copied, the move is refused and the write keeps its error.
Once the original name has been removed successfully, every handle
switches to the new copy together.
Every handle opened through the file's name, including opens made
after a rename through mergerfs, is switched together, and an open of
the file while a move is in flight waits and gets the moved file.
After the move each handle adopts the path the relocation used, so a
later move can be triggered by any of them. A handle whose own
recorded path no longer resolves to the file on any branch cannot
itself trigger a relocation; its write keeps the original error
until another handle of the file relocates it.
3. Retry only the unwritten portion of the request, so a partially
completed append is not duplicated.
If at any point something fails the progress so far will be cleaned up
as appropriate and the original error returned. If part of the write
already reached the file the bytes written are returned instead, as a
short write, as with a local filesystem. The exception is data the
kernel is flushing from its page cache (`cache.writeback` or dirty
`mmap` pages), including writeback on a direct-I/O handle. The kernel
cannot act on a short write for those pages, so the error is returned
and reaches `fsync`, `msync` or `close`.
If at any point something fails the provisional destination will be
cleaned up, provided it still identifies the object created by this move.
The original error is returned and every handle remains on the original.
In particular, failure to remove the source name does not switch even the
writing handle and does not acknowledge the unwritten suffix. If part of
the write already reached the file the bytes written are returned
instead, as a short write, as with a local filesystem. The exception is
data the kernel is flushing from its page cache (`cache.writeback` or
dirty `mmap` pages), including writeback on a direct-I/O handle. The
kernel cannot act on a short write for those pages, so the error is
returned and reaches `fsync`, `msync` or `close`.
A file is never relocated when it has more than one link: a copy plus
source removal would leave the other names on the original inode, so the
names would describe different files afterwards. The move is also
refused while the file is open through another name, which the kernel
treats as another file: a hardlink whose other name was removed while
both were open, a rename made directly on a branch while the file was
open, or branches that reach the same file by two paths. Those handles
could not be switched and would keep writing to the original. An open
through such another name that happens during the last step of a move
is not coordinated with it, and can end up on the original, losing its
writes. With `moveonenospc`, avoid presenting one file under two names:
[follow-symlinks](follow-symlinks.md) and branches that reach the same
file by two paths both do that. Entries
inside the `rename-exdev` staging namespace (the targets of
published replacement symlinks, `.mergerfs_rename_exdev/`) are also never
relocated: moving one would re-home the symlink's target and strand the
published name. In all of these cases the write keeps its original error.
Whether a file can be relocated is decided when it is first opened, and
holds until every handle to it has been closed. A file opened while
`moveonenospc` is disabled, or opened with a kernel passthrough backing
ID (see [passthrough.io](passthrough.md)), is never relocated, and its
handles skip the coordination a relocation needs. Enabling
`moveonenospc` at runtime therefore applies to files opened afterwards.
Source and destination identity checks reject replacements observed
before cleanup or source removal. Removal itself is a pathname operation
and cannot be atomic with those earlier checks; they do not synchronize
with external changes to the branches racing the final unlink syscall.
If `moveonenospc` is disabled the underlying error will be returned,
or the bytes written if part of the write succeeded.

View file

@ -24,8 +24,13 @@ This performance does come at the cost of some functionality as
`mergerfs` no longer has control over reads and writes which means all
features related to read/write are affected.
* [moveonenospc](moveonenospc.md): Does not work because errors are
not reported back to `mergerfs`.
* [moveonenospc](moveonenospc.md): A file opened with a passthrough
backing ID is never relocated, as the kernel keeps reading and
writing the original file through that ID. A file gets the ID from
its first open, when no handle has it open yet, and keeps it until
all of its handles are closed. With `passthrough.io=rw` that is every
file; with `ro` or `wo`, a file first opened read-only or write-only.
A file first opened any other way is relocated as usual.
* nullrw: Since `mergerfs` no longer receives read/write requests
there is no read or write to ignore.
* [readahead](readahead.md): Still affects the `readahead` values of

View file

@ -45,7 +45,8 @@ Distributions that enforce RPM signature checking reject the unsigned
local build. When installing a locally built RPM that you built and
trust, pass `--nosignature` to `rpm`.
mergerfs requires a C++20 compiler and the system `g++` on RHEL /
mergerfs requires a C++20 compiler and standard library that support
atomic waiting, such as GCC 11 or newer. The system `g++` on RHEL /
Rocky 8 is too old. On RHEL / Rocky `install-build-pkgs` installs the
newest GCC Toolset when it is newer than the system compiler, and the
Rocky Linux container builds use it. When `CXX` is unset and the