Commit graph

29 commits

Author SHA1 Message Date
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
Antonio SJ Musumeci
b367b593ba fix: make the statfs cache correct and its timeout configurable
- A failed statvfs was cached as a success: for the next cache.statfs
  seconds every caller got the entry's old or zeroed stats and create
  policies chose branches from them. Keep a failure as an error for the
  timeout, like a success, so a slow failing branch isn't retried under
  the lock by every caller in turn. Document that the branch then stays
  excluded from space-based create policies until the timeout expires.
- A new entry was told apart from a fresh one only by a time of 0, so a
  timeout of at least the current time in seconds returned the zeroed
  struct as success. Give entries a valid flag set by the first
  statvfs, and measure age with a steady clock from when statvfs
  returned.
- cache.statfs given at mount was stored but never applied. Bind the
  option directly to the atomic the cache reads, so mount options,
  config files and control-file updates follow one path and concurrent
  readback no longer races with updates.
- tests.cpp checks that a cached failure is never served as success,
  that failures are kept until the timeout, huge timeouts, an invalid
  update keeping a cached ENOENT, and a timeout of 0 exposing a newly
  created directory immediately.
2026-11-29 23:11:06 -05:00
Antonio SJ Musumeci
8cc6be6ff2 fix: make enum, minfreespace and process-name options safe to change
- Options set through the .mergerfs xattr are written while request
  threads read them, with no synchronization. That is a data race; on
  32-bit targets a request could see half of a new 64-bit minfreespace.
- Store the default minfreespace as std::atomic<u64> with relaxed
  loads and stores. Per-branch values are unchanged.
- Store every enum option (rename-exdev, xattr, cache.files, statfs,
  nfsopenhack, follow-symlinks and the rest) atomically. Copies take a
  snapshot.
- getxattr, setxattr and removexattr read the xattr option once per
  request instead of twice.
- cache.files.process-names was cleared and refilled in place while
  open and create looked names up in it, which could walk freed tree
  nodes. Build the new set aside and swap it in under a lock that
  lookups share.
- Other options (booleans, numbers and strings) are still plain values;
  this covers only the ones above.
2026-11-29 23:11:03 -05:00
Antonio SJ Musumeci
d679fdd265 fix: validate and cap thread and queue counts
- std::stoi threw std::out_of_range for a func.readdir concurrency or
  queue depth above INT_MAX, e.g. cor:99999999999. Given at mount,
  mergerfs aborted. Set at runtime through .mergerfs, the daemon shut
  down and the mount went away, or with process-thread-count>0 the
  request never got a reply. Parse with str::from, which reports the
  overflow, and reject the value like any other malformed one.
- Reject a readdir queue depth whose product with the concurrency
  overflows an int, e.g. cor:2147483647:2147483647, instead of
  wrapping it.
- Limit read-thread-count, process-thread-count and the cor/cosr pool
  size to 64 threads per pool, after resolving automatic and
  CPU-divided counts, and log a warning when clamping. Resolve
  negative FUSE counts without overflowing on INT_MIN, and drop the
  unreachable negative readdir concurrency branch.
- process-thread-queue-depth is multiplied by the process thread count.
  Saturate the product at INT_MAX instead of overflowing.
- Correct the docs: cor/cosr counts accept digits only and 0 means the
  core count, at most 8; a queue depth of 0 means 2 per thread;
  read-thread-count=0 with process-thread-count=0 is 2 read threads
  and max(2, cores - 2) process threads, at most 8; the queue limit is
  depth times process threads, each read thread holding one in-flight
  buffer of its own. Point at threads.md for how the settings combine.
- Document the per-read-thread /dev/fuse clones and their fallback.
- tests.cpp covers the overflow cases.

Performance: default thread counts are unchanged (8 on hosts with 8 or
more cores). Only explicit or CPU-divided counts above 64, which
2.42.0 honored, are reduced.
2026-11-29 23:11:00 -05:00
Antonio SJ Musumeci
61597c8dfd fix: pick the moveonenospc source and destination correctly
- moveonenospc copied from the branch stored when the file was opened,
  which a move never updates, so a second ENOSPC on the same handle
  tried to copy a path that no longer existed. Find the source branch
  from the fd's device and inode at the start of each move, trying the
  stored branch first. No match leaves the write's ENOSPC in place.
- The source branch was dropped from the create policy's result, so a
  policy returning one branch (ff, mfs, lfs, lus, pfrd) left nothing
  when it picked the source, and a destination that was the source by
  another path (bind mount) was renamed over the file and unlinked.
  Remove every alias of the source before the policy runs, so it picks
  another branch; with no candidate left the move is refused.
- Refuse relocation if a duplicate on another branch could become
  visible once the source is removed, and publish without replacing a
  destination that appeared meanwhile. When the no-replace rename is
  unsupported, publish with link(); a staging name that cannot be
  removed afterwards is logged instead of silently left behind.
- Ask the create policy by the parent directory, as create does. The
  existing-path policies and newest only pick a branch that has the
  path they are given, and the destination has the directory but not
  the file yet.
- TEST_movefile_enospc moves a handle twice and checks duplicates
  through the pool. tests.cpp covers the source by its own path and an
  alias, and every existing-path policy for nested and root files.
2026-11-29 23:10:59 -05:00
Antonio SJ Musumeci
7e29c3f72a test: run test_clonepath_modes where gid 1 is unmapped, and clean up
Root in a user namespace may not have gid 1 mapped, so the chown to it
failed and the test aborted. Use a distinct group only when gid 1 is
available and differs from the caller's, and only check setgid group
inheritance then; permissions, ownership and existing destinations
are still checked on single-group hosts. TEST_ASSERT aborts without
unwinding and left the fixture in /tmp, so the body checks and
returns and the caller always removes it.
2026-11-29 23:10:56 -05:00
Antonio SJ Musumeci
6d138906ec Fix ARMv5 atomic loads from Boost dummy storage 2026-11-14 18:27:14 -06:00
Antonio SJ Musumeci
74eade91f5 Preserve directory modes when cloning paths across branches
- Restore source permissions after ownership to preserve setgid and clear unwanted inherited mode bits.
- Add regression coverage for directory modes, group inheritance, and existing destination directories.
- Update the branch configuration test to use the current API.
- Document metadata preservation when cloning directory paths.
2026-11-04 19:25:10 -06:00
Antonio SJ Musumeci
b10fb10590 fix: preserve source ownership in EXDEV symlink fallbacks 2026-11-01 21:11:04 -06:00
Antonio SJ Musumeci
7b7e96bbc5 fix: initialize EXDEV rename staging for root sources 2026-11-01 21:11:04 -06:00
Antonio SJ Musumeci
3d81e24f32 fix: keep EXDEV rename symlink targets relative 2026-11-01 21:11:04 -06:00
Antonio SJ Musumeci
28d809f2f8 Fix MSP create policy hangs when no branches qualify
- Stop parent traversal after evaluating the empty relative root.
- Preserve filtering errors, parent fallback, and closest-path selection.
- Cover all four MSP policies with rejection and fallback regressions.
- Document errors when traversal exhausts the branch root.
2026-11-01 21:11:04 -06:00
Antonio SJ Musumeci
f1034a3ad6 Remove timed node removal 2026-05-06 20:49:46 -04:00
Antonio SJ Musumeci
943f91ff98 Strengthen mergerfs integration tests
- Make the test runner fail on failed tests and report explicit skips
- Strengthen assertions for O_DIRECT, LUP policy, EXDEV fallback, and xattrs
- Fix cleanup and file descriptor handling across test scripts
2026-04-29 20:25:20 -04:00
Antonio SJ Musumeci
ef1e01b5a5 Add unit and integration tests for recently changed code
Unit tests (29 new in tests/tests.cpp):
- str::eq() and str::startswith() char* nullptr safety
- str::from() u64/s64/int suffix parsing (K/M/G/T, overflow, invalid)
- num::humanize() byte formatting (0, 512, 1K, 1M, 1G, 1T, non-exact)
- RmdirErr priority error accumulation (ENOTEMPTY/EEXIST override rules)
- HashSet inline-to-heap growth, empty string, and large insertion
- fs::inode algorithm switching (valid/invalid names, passthrough raw ino,
  distinct inodes across algorithms)
- RND::rand64() bounds (range, max, shrink_to_rand_elem)
- Branches::Impl copy and move assignment re-links default_minfreespace

Integration tests (5 new in tests/):
- TEST_policy_lup: LUP policy create and search
- TEST_rmdir_enotempty_priority: rmdir returns ENOTEMPTY for non-empty dirs
- TEST_open_after_unlink: read/write via fd after unlink
- TEST_api_xattr: mergerfs xattr API (fullpath, relpath, basepath, allpaths)
- TEST_movefile_enospc: moveonenospc configuration round-trip

Also: remove xattr nosys checks from TEST_cfg_xattr_modes, reformat
integration test scripts to use temp_dir/cleanup_dir helpers, and
minor parameter alignment changes in fuse_getxattr, fuse_init,
fuse_rename.
2026-04-29 17:41:00 -04:00
Antonio S. Musumeci
351adc2b20 Add tests for HashSet, rapidhash withSeed consistency, and ReaddirCalc parity with calc
Some checks failed
mkdocs / deploy (push) Has been cancelled
2026-04-12 23:44:23 -05:00
Antonio SJ Musumeci
0f1a8e9b69 Fix error condition in fs_copyfile 2026-03-24 21:13:34 -04:00
Antonio SJ Musumeci
7f52623536 Cleanup branches and add more unit tests 2026-03-24 21:13:34 -04:00
Antonio SJ Musumeci
a30a4df9c0 Cleanup config and add tests 2026-03-24 21:13:34 -04:00
Antonio SJ Musumeci
076e359891 Improve thread pool 2026-03-24 21:13:34 -04:00
Antonio SJ Musumeci
164b7f73c9 Misc fixes and improvements to thread pool 2026-03-24 21:13:34 -04:00
Antonio SJ Musumeci
3a45be4cd8 Rework string utils, more test cases 2026-03-24 21:13:34 -04:00
Antonio SJ Musumeci
8f1ef59770 Move 3rd party libraries to "vendored" directory 2026-03-02 01:09:16 -05:00
Antonio SJ Musumeci
160d3c832d Fix branches unit test 2026-03-02 01:09:16 -05:00
trapexit
cfaad60275
Misc updates to arg parsing (#1554)
Some checks failed
CodeQL / Analyze (push) Has been cancelled
mkdocs / deploy (push) Has been cancelled
Ignore "defaults" and add a number of more modern VFS mount switches
2025-10-26 21:05:05 -05:00
trapexit
d7549f8d53
Fix string manipulation functions after refactor (#1514) 2025-08-22 19:42:31 -05:00
Ilya Taranov
d04e04e89a Fix config file parsing 2023-03-10 20:55:20 -08:00
Antonio SJ Musumeci
d337574665 fix parsing of relative branch paths 2021-02-11 19:22:17 -05:00
Antonio SJ Musumeci
538467b86d config: rework global config, remove rwlock, make branches RCU like
Also added unit tests. Should have done separately but found a number of bugs.
2021-01-14 21:02:10 -05:00