mergerfs/tests/TEST_rm_rf_concurrent
Antonio S. Musumeci 9d12bcf235 Return ENOENT, not EBADF, for removed nodes with no open file
Once a file or directory is removed (unlink, rmdir, rename over it)
libfuse has no path for its node and falls back to the fh based
getattr/setattr/statx with fh=0. If mergerfs had no open file for the
node (a directory, an O_PATH fd, a cwd, or a dentry resolved just
before the removal) it returned EBADF. The kernel sends GETATTR for
such nodes in the permission checks of a path walk, so lstat() of a
directory, or of a path below it, could fail with EBADF while rm -rf
removed it. Two rm -rf at once, one on a directory and one on the
directory above it, met it when one removed a directory the other
still had open: the other's unlinkat() through its dirfd failed, rm
reported "Bad file descriptor" and left entries behind.

mergerfs now returns ENOENT. That is what a local filesystem gives
*at() calls in a removed directory, and what rm -f ignores. ESTALE
would make the kernel redo a path based lookup, which helps a stat
racing a rename over the path. But a call through a dirfd or cwd of a
removed directory meets the same node again and fails anyway, and the
concurrent rm -rf still did. Calls on the removed object itself, which
succeed on a local filesystem, get ENOENT instead of EBADF.

TEST_rm_rf_concurrent runs the two cases reported. The rm -rf one is
a narrow race; TEST_use_dirfd_after_rmdir covers its cause without
one, with cache.attr=0 so the kernel sends GETATTR every time.

See #1693.
2026-11-17 12:25:50 -06:00

95 lines
3.3 KiB
Python
Executable file

#!/usr/bin/env python3
# The two cases reported in #1693. lstat() of a directory, or of a path
# below it, while rm -rf removes it must succeed or return ENOENT. And
# rm -rf of a directory together with rm -rf of the directory above it
# must both succeed and leave nothing behind. When one rm removes a
# directory the other still has open, the kernel sends GETATTR for it
# in the other's permission checks, which mergerfs answered with EBADF,
# so lstat() returned EBADF and rm failed with "Bad file descriptor".
#
# cache.attr=0 makes the kernel send GETATTR every time. The lstat()
# case then fails in every run without the fix. The rm -rf case is a
# narrower race met in only a few runs; TEST_use_dirfd_after_rmdir
# covers its cause, a call through the dirfd of a removed directory,
# without a race.
import collections
import os
import subprocess
import time
from posix_parity import errno_name
from posix_parity import fail
from posix_parity import mergerfs_mount
OPTIONS = "defaults,cache.attr=0"
SECONDS = 2.0
def make_tree(path, width=4, depth=3):
os.makedirs(path, exist_ok=True)
for k in range(width):
open(os.path.join(path, f"f{k}"), "w").close()
if depth > 0:
for k in range(width):
make_tree(os.path.join(path, f"d{k}"), width, depth - 1)
def lstat_during_rm(mount):
counts = collections.Counter()
deadline = time.monotonic() + SECONDS
while time.monotonic() < deadline:
top = os.path.join(mount, "l")
make_tree(os.path.join(top, "inner", "sub"), width=3, depth=0)
rm = subprocess.Popen(["rm", "-rf", os.path.join(top, "inner")])
while rm.poll() is None:
for rel in ("inner", "inner/sub"):
try:
os.lstat(os.path.join(top, rel))
counts["ok"] += 1
except OSError as exc:
counts[errno_name(exc.errno)] += 1
del counts["ok"]
counts.pop("ENOENT", None)
return counts
def concurrent_rm(mount):
failures = collections.Counter()
deadline = time.monotonic() + SECONDS
while time.monotonic() < deadline:
top = os.path.join(mount, "c")
make_tree(os.path.join(top, "inner"))
procs = [subprocess.Popen(["rm", "-rf", path], stderr=subprocess.PIPE)
for path in (os.path.join(top, "inner"), top)]
for proc in procs:
_, err = proc.communicate()
if proc.returncode != 0:
failures[err.decode().strip().splitlines()[-1].split(": ")[-1]] += 1
if os.path.lexists(top):
failures["left behind"] += 1
subprocess.run(["rm", "-rf", top])
return failures
def main():
try:
with mergerfs_mount(num_branches=1, options=OPTIONS) as (mount, _):
errors = lstat_during_rm(mount)
if errors:
summary = " ".join(f"{k}={v}" for k, v in sorted(errors.items()))
return fail(f"lstat during rm -rf: {summary}")
failures = concurrent_rm(mount)
if failures:
summary = ", ".join(f"{k}: {v}" for k, v in sorted(failures.items()))
return fail(f"concurrent rm -rf: {summary}")
return 0
except RuntimeError as exc:
print(str(exc), end="")
return 77
if __name__ == "__main__":
raise SystemExit(main())