mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-12-02 21:00:13 -05:00
- func.readdir=cor appended entries in whichever order branch workers finished. Directory offsets are positions in that list, so a reader reopening an unchanged directory and seeking to a previously issued cookie (NFS does this) could see a duplicate and miss a later entry. Read branches concurrently into their own buffers, then merge and deduplicate them in configured branch order. The merge mutex is gone. - Growing a dirents buffer overwrote the only pointer with realloc's result while recording the new capacity, so an allocation failure leaked the buffer and the next fill wrote through NULL. Grow both vectors without changing pointer, length or capacity until the allocation succeeds, and return ENOMEM. - seq and cosr ignored that failure and served a listing with entries missing. Every policy now returns the error, and libfuse resets the handle's buffer whenever a fill fails, so a later request at a nonzero offset re-enumerates instead of serving a partial or stale listing as success. cosr closes the directories its workers opened when it gives up early. - Document the fill-error contract, which doesn't promise that every branch was read: seq and cosr can treat a branch read error as the end of the directory. Document cor's memory use, and refer to func.getattr, not fuse.getattr. - TEST_readdir_cor_cookie_replay compares cor listings, offsets and inodes with seq and replays cookies on fresh handles. TEST_dirents_allocation injects allocation failures into the dirents code. TEST_readdir_cor_enomem fails one allocation in a mounted daemon for cor and seq and checks the same handle then serves the exact suffix. Performance: cor copies each entry twice and merges serially after the parallel reads. Listing 100,000 entries over 4 tmpfs branches took the same time before and after (81-83 ms per listing for cor:4 and seq). Peak daemon memory for cor:4 dropped from 27.4 MB to 18.7 MB.
230 lines
7.3 KiB
Python
Executable file
230 lines
7.3 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Deterministic allocation-failure regression for the fuse_dirents layer.
|
|
|
|
Compiles the real vendored/libfuse/lib/fuse_dirents.cpp with a small C++
|
|
harness and -Wl,--wrap=realloc/--wrap=free, failing one exact call: the
|
|
initial reservation (NULL old pointer), the next data-buffer growth
|
|
(>= 32KiB quantum), or the next offset-table growth (< 32KiB). No mount,
|
|
no branches; disposable work dir under tests/.test_tmp. A failed call
|
|
must return -ENOMEM once and leave the added entries intact, verified by
|
|
name, inode and cookie (d->off == index + 1), never by pointers,
|
|
capacities or padding bytes; a disarmed retry must complete to exactly
|
|
the never-failed sequence, and a failed init must leave nothing behind.
|
|
Exit codes: 0 pass, 1 fail, 77 skip (non-Linux or no C++ compiler).
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
SKIP = 77
|
|
|
|
HARNESS = r"""
|
|
#include "fuse_dirents.hpp"
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
/* Data grows in 32KiB quanta; offset-table reallocs stay far below it,
|
|
so the allocation size discriminates the two vectors. Mode: 1 fails
|
|
a NULL old pointer (init), 2 the next data growth, 3 the next table
|
|
growth; each fires once at the Nth matching call. */
|
|
#define DATA_QUANTUM (1024 * 32)
|
|
#define NENTRIES 1500
|
|
|
|
extern "C" {
|
|
void *__real_realloc(void *, size_t);
|
|
void __real_free(void *);
|
|
}
|
|
|
|
static int fail_mode, fail_nth, fail_count;
|
|
static long live_blocks;
|
|
static char msg[256];
|
|
|
|
static void arm(int mode, int nth) {
|
|
fail_mode = mode;
|
|
fail_nth = nth;
|
|
fail_count = 0;
|
|
}
|
|
|
|
static bool ok(bool cond, const char *what) {
|
|
if(!cond && !msg[0])
|
|
snprintf(msg, sizeof(msg), "%s", what);
|
|
return cond;
|
|
}
|
|
|
|
static int should_fail(void *p, size_t n) {
|
|
int match;
|
|
if(fail_mode == 1)
|
|
match = (p == NULL);
|
|
else if(fail_mode == 2)
|
|
match = (p && n >= DATA_QUANTUM);
|
|
else if(fail_mode == 3)
|
|
match = (p && n < DATA_QUANTUM);
|
|
else
|
|
return 0;
|
|
if(!match || --fail_nth)
|
|
return 0;
|
|
fail_count++;
|
|
return 1;
|
|
}
|
|
|
|
/* A failed realloc leaves the old block alive (no count change); a
|
|
successful NULL-pointer realloc births one; nonnull moves are net
|
|
zero. libc internals do not route through these wrappers. */
|
|
extern "C" void *__wrap_realloc(void *p, size_t n) {
|
|
void *q;
|
|
if(should_fail(p, n))
|
|
return NULL;
|
|
q = __real_realloc(p, n);
|
|
if(q && !p)
|
|
live_blocks++;
|
|
return q;
|
|
}
|
|
|
|
extern "C" void __wrap_free(void *p) {
|
|
if(p)
|
|
live_blocks--;
|
|
__real_free(p);
|
|
}
|
|
|
|
static bool check_entries(fuse_dirents_t *d, int count, const char *what) {
|
|
int i;
|
|
char want[16];
|
|
if(!ok((int)kv_size(d->offs) == count + 1 && kv_A(d->offs, 0) == 0, what))
|
|
return false;
|
|
for(i = 0; i < count; i++) {
|
|
const fuse_dirent_t *de =
|
|
(const fuse_dirent_t *)&kv_A(d->data, kv_A(d->offs, i));
|
|
snprintf(want, sizeof(want), "e%07d", i);
|
|
if(!ok(de->ino == (uint64_t)i + 1 && de->off == (uint64_t)i + 1 &&
|
|
de->namelen == 8 && !memcmp(de->name, want, 8), what))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static int add_entry(fuse_dirents_t *d, int idx) {
|
|
char name[16];
|
|
alignas(fs::dirent64) char storage[64];
|
|
fs::dirent64 *de = (fs::dirent64 *)storage;
|
|
snprintf(name, sizeof(name), "e%07d", idx);
|
|
memset(storage, 0, sizeof(storage));
|
|
de->ino = (uint64_t)idx + 1;
|
|
de->reclen = (uint16_t)(offsetof(fs::dirent64, name) + 9);
|
|
de->type = DT_REG;
|
|
memcpy(de->name, name, 9);
|
|
return fuse_dirents_add(d, de, 8);
|
|
}
|
|
|
|
/* Arm the injector, add until one add fails, verify the survived prefix
|
|
and that a disarmed retry reaches exactly the never-failed sequence. */
|
|
static bool growth_scenario(int mode, const char *what) {
|
|
fuse_dirents_t d;
|
|
int i, failed_at = -1;
|
|
if(fuse_dirents_init(&d))
|
|
return ok(false, "clean init failed");
|
|
arm(mode, 1);
|
|
for(i = 0; i < NENTRIES; i++)
|
|
if(add_entry(&d, i)) { failed_at = i; break; }
|
|
fail_mode = 0;
|
|
if(!ok(fail_count == 1, "injected growth failure did not fire exactly once") ||
|
|
!ok(failed_at > 0, what) ||
|
|
!check_entries(&d, failed_at, what))
|
|
return false;
|
|
for(; i < NENTRIES; i++)
|
|
if(add_entry(&d, i))
|
|
return ok(false, "retry add after the failed growth failed");
|
|
if(!check_entries(&d, NENTRIES, what))
|
|
return false;
|
|
fuse_dirents_free(&d);
|
|
return ok(live_blocks == 0, "dirents allocation leaked");
|
|
}
|
|
|
|
static bool init_failure(void) {
|
|
fuse_dirents_t d;
|
|
arm(1, 1);
|
|
if(!ok(fuse_dirents_init(&d) == -ENOMEM,
|
|
"init did not return -ENOMEM on its failed initial allocation") ||
|
|
!ok(fail_count == 1, "init injector did not fire exactly once"))
|
|
return false;
|
|
fail_mode = 0;
|
|
fuse_dirents_free(&d);
|
|
if(!ok(live_blocks == 0, "failed init left an allocation behind"))
|
|
return false;
|
|
if(fuse_dirents_init(&d))
|
|
return ok(false, "init after a failed init failed");
|
|
fuse_dirents_free(&d);
|
|
return ok(live_blocks == 0, "dirents allocation leaked");
|
|
}
|
|
|
|
int main(void) {
|
|
static char stdout_buf[4096];
|
|
setvbuf(stdout, stdout_buf, _IOFBF, sizeof(stdout_buf));
|
|
if(!growth_scenario(2, "no add failed under the armed data-growth injector") ||
|
|
!growth_scenario(3, "no add failed under the armed offset-growth injector") ||
|
|
!init_failure()) {
|
|
printf("FAIL: %s\n", msg);
|
|
return 1;
|
|
}
|
|
printf("PASS\n");
|
|
return 0;
|
|
}
|
|
"""
|
|
|
|
|
|
def main():
|
|
if not sys.platform.startswith("linux"):
|
|
print("Linux-only allocation regression; skipping on " + sys.platform)
|
|
return SKIP
|
|
compiler = next((shutil.which(c) for c in ("g++", "c++", "clang++")
|
|
if shutil.which(c)), None)
|
|
if compiler is None:
|
|
print("no C++ compiler found; skipping")
|
|
return SKIP
|
|
|
|
tests_dir = os.path.dirname(os.path.realpath(__file__))
|
|
repo = os.path.dirname(tests_dir)
|
|
fuse_dirents_cpp = os.path.join(repo, "vendored", "libfuse", "lib",
|
|
"fuse_dirents.cpp")
|
|
if not os.path.isfile(fuse_dirents_cpp):
|
|
print("missing " + fuse_dirents_cpp)
|
|
return 1
|
|
|
|
tmp_parent = os.path.join(tests_dir, ".test_tmp")
|
|
os.makedirs(tmp_parent, exist_ok=True)
|
|
workdir = tempfile.mkdtemp(prefix="dirents_allocation_",
|
|
dir=tmp_parent)
|
|
try:
|
|
harness_cpp = os.path.join(workdir, "harness.cpp")
|
|
binary = os.path.join(workdir, "test_dirents_allocation")
|
|
with open(harness_cpp, "w", encoding="ascii") as stream:
|
|
stream.write(HARNESS)
|
|
compiled = subprocess.run(
|
|
[compiler, "-std=c++20", "-O2", "-fno-builtin",
|
|
"-D_FILE_OFFSET_BITS=64",
|
|
"-I" + os.path.join(repo, "vendored", "libfuse", "include"),
|
|
"-I" + os.path.join(repo, "src"),
|
|
"-I" + os.path.join(repo, "vendored"),
|
|
"-Wl,--wrap=realloc", "-Wl,--wrap=free",
|
|
harness_cpp, fuse_dirents_cpp, "-o", binary],
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
|
if compiled.returncode != 0:
|
|
print("compilation failed:\n" + compiled.stdout)
|
|
return 1
|
|
run = subprocess.run(
|
|
[binary], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
|
|
text=True)
|
|
sys.stdout.write(run.stdout)
|
|
return run.returncode
|
|
finally:
|
|
shutil.rmtree(workdir, ignore_errors=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|