fix: make the rename-exdev fallback safe and transactional

rename-exdev (opt-in; the default is passthrough) moves a source it
cannot rename across branches into a staging tree and publishes a
symlink at the destination. This reworks that fallback so a rename
either completes or leaves both names as they were.

- Staging opens every component relative to a pinned parent with
  O_NOFOLLOW, requires a daemon-owned staging root, and refuses when
  the tree can't be trusted.
- Staged names are taken without replacement and must be free on
  every configured branch, not only those holding the source, so a
  later lookup cannot reach another rename's payload. Over-long names
  are shortened. Fallback renames are serialized.
- Destinations: a compatible existing destination is replaced, kept
  under a temporary name until every branch is published, and
  restored if a later branch fails. A destination on a read-only
  branch, or a no-create one where the link can't be created, is
  refused before anything moves. Temporary and restore names never
  replace an entry, and a restore that fails is reported.
- An ordinary rename (rename-exdev=passthrough) also refuses with
  EXDEV when the destination exists on a read-only branch that does
  not hold the source: that copy could neither be replaced nor
  removed and would shadow the renamed file there. It is never
  scheduled for removal either.
- The replacement symlink is prepared in a daemon-private directory,
  its UID and GID are verified through a pinned descriptor, and it is
  published without replacement, before any source moves. A sticky,
  world-writable destination directory under Linux protected symlinks
  is refused. Each prepared link is tracked by identity for cleanup.
- Symlink sources are recreated with their original text, owner and
  times instead of staged, since staging changes where a relative
  target resolves. Sources of different types on different branches
  are refused. A hidden source that cannot be removed fails the rename.
- libfuse re-parents the kernel's node onto the staged inode, using a
  name allocated before publication, so open handles and cached
  dentries keep working and nothing can fail after the rename commits.
- Without renameat2 (FreeBSD 14.4 and 15.0, or seccomp), regular
  files and directories use a portable layout: a fresh daemon-owned
  wrapper, access gates that mirror the source's parents with the
  source's GID and its read and execute bits only, and the payload
  directory inside the last gate. Foreign-owned parents, unsettable
  group ownership and non-trivial ACLs are refused before the source
  moves. A namespace the source never entered is removed. A portable
  directory that can't be restored stays staged with its path logged,
  and an existing directory destination is refused before a portable
  source moves: keeping it aside would need the no-replace rename the
  portable layout lacks.
- Document the fallback, its refusals and FreeBSD limits.
- TEST_rename_exdev_publication, TEST_rename_exdev_portable and
  tests.cpp cover publication rollback, shadows, collisions,
  ownership, sticky directories, mixed source types, a refused portable
  directory destination and the portable layout through a mounted
  pool. TEST_rename_exdev_publication keeps its fixture if its tmpfs
  can't be detached.

Performance: an ordinary rename only adds an lstat per read-only
branch, and only when one is configured. The fallback adds about two
lstats per branch plus the staging work, and fallback renames run one
at a time.
This commit is contained in:
Antonio SJ Musumeci 2026-11-25 13:45:42 -05:00
parent 036246140b
commit c9854f236f
16 changed files with 3962 additions and 202 deletions

View file

@ -13,16 +13,32 @@ the newpath. The target value is determined by the value of
mount point.
* Defaults to `passthrough`.
Replacement symlinks retain the source inode's UID and GID, read without
following a source symlink. Relative and pool targets use the first
`getattr`-policy source branch; absolute branch targets use the source
selected by the `open` policy. Creation uses daemon credentials, then
sets the source ownership, rather than using the link request's UID/GID,
which may be invalid when `allow-idmap` is enabled. A newly created
replacement is removed if its ownership cannot be set.
Replacement symlinks retain the source inode's UID and GID where
supported, read without following a source symlink. Relative and pool
targets use the first `getattr`-policy source branch; absolute branch
targets use the source selected by the `open` policy. The replacement
is prepared before publication in the daemon-private
`.mergerfs_symlink_exdev` directory at the branch root. An inherited
setgid bit is cleared on the pinned staging directory before creation so
source credentials produce the source UID and GID. Any remaining
ownership fixup is made through a pinned file descriptor. Linux
publishes with a no-replace rename or a hard link from the private
staging directory; neither replaces an existing link destination.
Source branch selection and path construction retain the same branch
snapshot, even when the branch list is updated at runtime.
FreeBSD corrects the pinned symlink's ownership and publishes it with
a hard link from the private staging directory.
If the source ownership cannot be represented or the verified symlink
cannot be published, the fallback fails rather than publishing a link
with the daemon's UID or a setgid parent's GID. The link request's
UID/GID is never used: it may be invalid when `allow-idmap` is enabled.
In a sticky, world-writable directory (such as a shared `/tmp`-like
directory) a symlink can only be followed by its owner unless the
directory's owner also owns it (Linux's `fs.protected_symlinks`, on by
default). A replacement symlink owned by the source's owner there would
be unusable by everyone else, root included, so in that case mergerfs
does not use the fallback and returns `EXDEV`, as if it were set to
`passthrough`.
**NOTE:** It is possible that some applications check the file they
link. In those cases, it is possible it will error or complain.