mergerfs/mkdocs/docs/config/rename_and_link.md
Antonio SJ Musumeci c9854f236f 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.
2026-11-29 23:11:10 -05:00

3.4 KiB

rename and link

rename and link are arguably the most complicated functions to create in a union filesystem. rename only works within a single filesystem or device. If a rename can not be done due to the source and destination paths existing on different mount points it will return an error (EXDEV, cross device / improper link). So if a rename's source and target are on different filesystems within the pool it creates an issue.

Originally mergerfs would return EXDEV whenever a rename was requested which was cross directory in any way. This made the code simple and was technically compliant with POSIX requirements. However, many applications fail to handle EXDEV at all and treat it as a normal error or otherwise handle it poorly. Such apps include: gvfsd-fuse v1.20.3 and prior, Finder / CIFS/SMB client in Apple OSX 10.9+, NZBGet, Samba's recycling bin feature.

As a result a compromise was made in order to get most software to work while still obeying mergerfs' policies. The behavior is explained below.

  • If using a create policy which tries to preserve directory paths (epff,eplfs,eplus,epmfs)
    • Using the rename policy get the list of files to rename
    • For each file attempt rename:
      • If failure with ENOENT (no such file or directory) run create policy
      • If create policy returns the same branch as currently evaluating then clone the path
      • Re-attempt rename
    • If any of the renames succeed the higher level rename is considered a success
    • If no renames succeed the first error encountered will be returned
    • On success:
      • Remove the target from all branches with no source file
      • Remove the source from all branches which failed to rename
  • If using a create policy which does not try to preserve directory paths
    • Using the rename policy get the list of files to rename
    • Using the getattr policy get the target path
    • For each file attempt rename:
      • If the target path does not exist on the source branch:
        • Clone target path from target branch to source branch
      • rename
    • If any of the renames succeed the higher level rename is considered a success
    • If no renames succeed the first error encountered will be returned
    • On success:
      • Remove the target from all branches with no source file
      • Remove the source from all branches which failed to rename

The removals are subject to normal entitlement checks. If the unlink fails it will fail silently.

A target on a branch configured read-only (RO) which holds no source file can be neither replaced nor removed, and would keep shadowing the renamed file there. The rename returns EXDEV before anything is changed, and such a target is never scheduled for removal. Branches which may be modified, including no-create (NC) ones, keep the removal behavior described above.

The above behavior will help minimize the likelihood of EXDEV being returned but it will still be possible.

link uses the same strategy but without the removals.

Additional Reading