mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 21:00:13 -05:00
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.
208 lines
12 KiB
Markdown
208 lines
12 KiB
Markdown
# rename-exdev
|
|
|
|
If using path preservation and a `rename` fails with `EXDEV`:
|
|
|
|
1. Move the source into `.mergerfs_rename_exdev` at the root of its
|
|
branch. The original `a/b/c` layout is used when atomic no-replace
|
|
rename is available. Otherwise a fresh portable namespace is used.
|
|
The replacement symlink names the file
|
|
`.mergerfs_rename_exdev/<random>/a/b/.payload/c`, where `a/b` are
|
|
real directories mirroring the source's parent directories and the
|
|
final `.payload` is a real directory containing the moved file; a
|
|
root source stages at
|
|
`.mergerfs_rename_exdev/<random>/.payload/c`.
|
|
2. symlink the rename's `newpath` to the moved file.
|
|
|
|
As with `rename(2)` an existing `newpath` is replaced: the symlink is
|
|
created under a temporary name next to `newpath` and renamed over it,
|
|
and copies of `newpath` on other branches are removed. A file can't
|
|
replace a directory (`EISDIR`), a directory can't replace a
|
|
non-directory (`ENOTDIR`) and a directory can only replace an empty
|
|
directory (`ENOTEMPTY`). Those cases are detected before the source is
|
|
moved. Before anything is moved, mergerfs also scans every configured
|
|
branch once and pins which of them hold the destination, together with
|
|
each copy's identity. A destination on a branch configured read-only
|
|
cannot be removed, and on a branch configured no-create the
|
|
replacement link cannot be created, so in both cases the old entry
|
|
would shadow the published link there and the fallback refuses, as it
|
|
does when two configured branches are the same underlying directory. Publication then touches a
|
|
destination copy only on a branch of that scan and only while the copy
|
|
still matches its pinned identity: a copy on a branch which will not
|
|
publish the link, one which appeared after the scan, and one which
|
|
cannot be verified all refuse the rename instead of being moved or
|
|
deleted. A destination the scan found absent is never replaced either:
|
|
if something is there by the time the link is published, the rename is
|
|
refused instead of clobbering it. When publishing on several branches,
|
|
mergerfs keeps each replaced destination under a temporary name until
|
|
the last publication. If publication fails, it attempts to restore the
|
|
destinations and source without overwriting any name created in the
|
|
meantime. A portable directory cannot be restored atomically and stays
|
|
at the recovery path reported in the log. A destination copy that
|
|
disappeared before publication is not recreated.
|
|
|
|
An ordinary rename, with `rename-exdev=passthrough`, refuses the same
|
|
way when the destination exists on a branch configured read-only which
|
|
does not hold the source, and a destination copy left on one is not
|
|
scheduled for removal either. Branches which may be modified, including
|
|
no-create ones, keep their usual behavior of removing destination copies
|
|
left on them.
|
|
|
|
A symlink being renamed is not moved into the staging directory, as
|
|
that would break a relative target (including those of replacement
|
|
symlinks made by `rel-symlink`). Since `rename(2)` does not change a
|
|
symlink's content, the symlink is recreated at `newpath` with the same
|
|
target, owner and timestamps and the original is removed. Recreation
|
|
collapses every branch's copy of the symlink into the one target of
|
|
the first source, so a symlink which exists on more than one branch is
|
|
refused before anything is moved; single-source renames keep working.
|
|
Sources of differing types across the selected branches are refused
|
|
the same way before anything is moved, whether the first source is the
|
|
symlink or a regular file or directory behind it: the staged copies
|
|
all sit at the one path the replacement link points at, and a symlink
|
|
among them, staged one directory deeper, would resolve its relative
|
|
target from inside the staging directory and dangle.
|
|
Every source link is moved aside before the new one is published, so a
|
|
source which can't be moved aside fails the rename and nothing
|
|
changes. If a moved source cannot then be unlinked, the rename fails
|
|
and that source remains under its temporary name beside the original
|
|
parent.
|
|
|
|
The `target` value is determined by the value of `rename-exdev`.
|
|
|
|
* `rename-exdev=passthrough`: Return `EXDEV` as normal.
|
|
* `rename-exdev=rel-symlink`: A relative path from the `newpath`.
|
|
* `rename-exdev=abs-symlink`: An absolute value using the mergerfs
|
|
mount point.
|
|
* Defaults to `passthrough`.
|
|
|
|
The staging directory is created on demand, including for files at the
|
|
pool root. Relative targets are calculated within the pool's relative
|
|
path namespace.
|
|
|
|
`.mergerfs_rename_exdev` itself must be a real directory owned by the
|
|
mergerfs process and is kept writable only by it (group and other
|
|
write permission is removed from directories made by older releases).
|
|
Components are opened without following symlinks. Existing targets in
|
|
the original `a/b/c` layout remain usable.
|
|
|
|
With the original layout, a staged file is never replaced. The staged
|
|
path is verified absent from every configured branch, including
|
|
read-only and no-create ones, before it is used: a missing name is
|
|
free, a name which is there is a collision and another suffixed name
|
|
is tried, and any other inspection error cannot be verified and fails
|
|
the rename rather than being mistaken for either. Fallback
|
|
transactions are serialized against each other so two of them cannot
|
|
take the same name, and a retry shortens the leaf as needed to fit the
|
|
staging directory's `NAME_MAX`; when even the shortest suffixed form
|
|
of the source's leaf cannot fit, the retry uses a bounded opaque
|
|
random leaf instead of failing. Staging and rollback use atomic
|
|
no-replace renames.
|
|
|
|
Without atomic no-replace rename, mergerfs creates a fresh `.p-`
|
|
prefixed random directory under the secured staging root. The source's
|
|
pool parent directories are reproduced inside it as access gates,
|
|
owned by the mergerfs process, with the source's GID and only the
|
|
source's read and execute permission bits: every write bit and the
|
|
setid and sticky bits are cleared, applied once after all gates exist
|
|
and before the source moves. An ancestor owned by another user cannot
|
|
be represented that way and the fallback refuses before the source
|
|
moves. The backing branch root is deliberately not copied: reaching
|
|
the staging tree already means traversing the branch root itself, so
|
|
its permissions apply to the staged payload exactly as they do to the
|
|
source's original path, whatever the pool presents at its own root.
|
|
UID, GID and mode are verified exactly before publication. A mergerfs
|
|
process which cannot give a gate the source's GID (for example an
|
|
unprivileged one, not a member of that group) refuses rather than leave
|
|
the group bits applying to a different group. The native layout above
|
|
copies parent metadata on a best-effort basis, as `clonepath` does, so
|
|
the two layouts can differ in this case. Portable staging refuses an
|
|
existing directory destination with `EXDEV`, before moving the source:
|
|
directories cannot be hard-linked aside for rollback. Native staging
|
|
can replace an empty directory when no-replace rename is supported.
|
|
Portable staging supports mode-representable access only: Linux
|
|
sources with POSIX access or default ACL xattrs are refused, and
|
|
FreeBSD accepts only native trivial ACLs (including UFS's empty
|
|
default ACL representation). Nontrivial or unverifiable ACLs fail
|
|
before the source moves rather than broadening access. These gates
|
|
prevent the additional pathname components from bypassing restrictive
|
|
source parents.
|
|
|
|
The real `.payload` directory is the last component of the route,
|
|
created inside the final gate (directly in the wrapper for a root
|
|
source) while the whole namespace is still private. There is no
|
|
intermediate link and no path to the payload which does not pass
|
|
through every gate. Gates belong to the mergerfs process and are not
|
|
writable by anyone, so no gate owner exists who could rename the
|
|
payload out of its mirror or replace anything inside a gate. A source
|
|
component named `.payload` is an ordinary mirrored directory like any
|
|
other; only the final `.payload` is created by mergerfs. The wrapper
|
|
and `.payload` are owned by the daemon, created exclusively 0700 and
|
|
verified as real directories through pinned descriptors. They become 0555
|
|
only after the move and access-gate setup completes; they are never
|
|
publicly writable. The source is staged under its original leaf name
|
|
in `.payload`. The extra components can make an otherwise
|
|
maximum-length source path too long to relocate.
|
|
|
|
Before any move, mergerfs prepares the actual temporary replacement
|
|
symlink, including its required ownership. Common creation and
|
|
permission failures therefore happen before the source is moved. A
|
|
portable namespace the source never entered is removed again. Native
|
|
rollback uses atomic no-replace rename. A portable non-directory
|
|
is rolled back by atomically hard-linking it at the old source name and
|
|
then unlinking the staged link, so a recreated source is never
|
|
replaced. No equivalent portable atomic operation exists for a
|
|
directory. If later publication or rollback fails, a portable
|
|
directory is left intact in its staged namespace and mergerfs logs its
|
|
recovery path rather than risking the source. Empty staging directories
|
|
may remain after a completed rollback.
|
|
|
|
**FreeBSD 14.4 and 15.0:** These releases' headers do not provide
|
|
`SYS_renameat2`, so regular-file and directory staging uses the portable
|
|
layout. Both files and directories can be staged, subject to the
|
|
directory-destination and rollback limits above. Ordinary renames and
|
|
the fallback for a source symlink do not use this layout. Because every
|
|
mirrored ancestor must be owned by the mergerfs process, the portable
|
|
layout refuses ordinary user-owned trees unless mergerfs runs as that
|
|
owner. On Linux the standalone portable fixture makes `renameat2`
|
|
return `ENOSYS` with a process-local seccomp filter and verifies the
|
|
portable layout. A host without filter support skips the forced
|
|
portable fixture explicitly.
|
|
|
|
Replacement symlinks retain the UID and GID of the first source inode
|
|
selected by the rename action policy where supported, read before moving
|
|
the source and without following source symlinks. The replacement is
|
|
prepared 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 the temporary link exclusively, using a no-replace rename or
|
|
a hard link from the private staging directory.
|
|
|
|
FreeBSD corrects the pinned symlink's ownership and publishes it with
|
|
a hard link from the private staging directory.
|
|
If source ownership cannot be represented or the verified replacement
|
|
cannot be published, the fallback fails rather than publishing a link
|
|
with different ownership. The request's UID/GID is never used: it may
|
|
be invalid with `allow-idmap`. If replacement creation fails, mergerfs
|
|
attempts to restore the original source paths.
|
|
|
|
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`. This is checked before the source is moved.
|
|
|
|
Until the kernel's cached entry for `newpath` expires (see
|
|
`cache.entry`) `newpath` keeps referring to the moved file itself, as
|
|
after a normal rename, and open handles keep working. After that,
|
|
`newpath` is seen as the symlink.
|
|
|
|
**NOTE:** It is possible that some applications check the file they
|
|
rename. In those cases it is possible it will error or complain.
|
|
|
|
**NOTE:** The reason `abs-symlink` is not split into two like
|
|
`link-exdev` is due to the complexities in managing absolute base
|
|
symlinks when multiple `oldpaths` exist.
|