mergerfs/mkdocs/docs/setup/build.md
Antonio SJ Musumeci c49989b75f 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-30 23:49:56 -05:00

136 lines
4.5 KiB
Markdown

# Build
**NOTE:** Prebuilt packages can be found at and recommended for most
users: https://github.com/trapexit/mergerfs/releases
**NOTE:** Only tagged releases are supported. `master` and other
branches should be considered works in progress.
First, get the code from [github](https://github.com/trapexit/mergerfs).
```
$ git clone https://github.com/trapexit/mergerfs.git
$ # or
$ wget https://github.com/trapexit/mergerfs/releases/download/<ver>/mergerfs-<ver>.tar.gz
```
## Debian / Ubuntu
```
$ cd mergerfs
$ sudo buildtools/install-build-pkgs
$ . /etc/mergerfs-build-env
$ make deb
$ sudo dpkg -i build/pkgs/mergerfs_<version>_<arch>.deb
```
Debian package compilation uses the available CPUs by default. To limit
parallelism, set `DEB_BUILD_OPTIONS`:
```
$ DEB_BUILD_OPTIONS=parallel=4 make deb
```
## RHEL / CentOS / Rocky / Fedora
```
$ su -
# cd mergerfs
# buildtools/install-build-pkgs
# make rpm
# rpm -i rpmbuild/RPMS/<arch>/mergerfs-<version>-1.<dist>.<arch>.rpm
```
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 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
default `g++` can't build C++20, `make rpm` activates the newest
installed GCC Toolset automatically. If the selected compiler lacks
the annobin plugin that the RPM build flags load, `make rpm` also
disables RPM's annobin annotations (`--undefine _annotated_build`).
Both the Rocky 10 `/usr/lib/gcc-toolset/<version>-env.source`
activation script and the older `/opt/rh/gcc-toolset-<version>/enable`
layout are supported. To build outside of `make rpm` on such systems
(for example `make` or `make install`) activate the toolset first:
```
# . /opt/rh/gcc-toolset-<version>/enable
# make
```
## Container packages
Package build targets are defined in `buildtools/containerfiles/`, with
the standard distro targets listed in `buildtools/target-containers.txt`.
Debian release builds target Debian 12 and 13. Other targets include
Fedora 45 on `amd64` and `arm64`, and Ubuntu 26.10 on `amd64`, `arm64`,
`armhf`, and `riscv64`. Foreign-architecture builds require QEMU/binfmt
support.
Standalone static archives (`mergerfs-<version>-static-linux_<arch>.tar.gz`)
are built with `make release-static`, which selects every `static.<arch>`
containerfile: `amd64`, `arm64`, `armhf`, `i386`, and `riscv64`.
`make release-all` builds every containerfile in the directory. A target
name that matches no containerfile (exact or substring) makes
`buildtools/build-release` fail with `no package targets match '<name>'`;
there is no installer target.
For a release, pass the intended tag to the Makefile (`GITREF=<tag>`)
or call `buildtools/build-release` directly with both `--git-repo` (the
repository for the container to clone) and `--branch` (the intended
tag); the script fails when either is missing. The named ref must be
available in that repository. A failed target gives the driver a
nonzero exit status; `build-report.txt` in the output directory lists
each target's result.
## Generic Linux
Have git, g++ or clang, make, python installed.
```
$ cd mergerfs
$ make
$ sudo make install
```
## FreeBSD
Have git, g++ or clang, gmake, python installed.
```
$ cd mergerfs
$ gmake
$ gmake install # as root
```
## Build options
```
$ make help
usage: make ARG
USE_XATTR=0 - build program without xattrs functionality
STATIC=1 - build static binary
LTO=1 - build with link time optimization
SANITIZE=1 - build with sanitizers (address,undefined,leak)
SANITIZE=list - build with custom sanitizers (comma separated)
CLEANUP=1 - cleanup images between release build process
PKGDIR=/dir/ - location for release build pkgs
GITREF=gitref - gitref to use for release builds
```
A plain `make` builds the debug profile (`-O0 -g3`, assertions
enabled). `make release` builds the binaries and `preload.so` with
`-O3` and assertions disabled. It rebuilds the objects, binaries and
vendored libfuse so debug objects cannot end up in the release build.
It leaves packages and reports in `build/pkgs/` (or your `PKGDIR`)
alone. `make clean` still removes the entire `build/` directory.
`make NDEBUG=1` selects the same release flags without cleaning; the
static package builds use it together with `LTO=1 STATIC=1`.