Skip to contentSkip to conversation

btrfs: test graceful ENOMEM handling in synchronous dirops

PATCH New Jeff Layton 1 participant 1 message Sep 22 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 btrfs: test graceful ENOMEM handling in synchronous dirops

Recently, we added some patches to allow btrfs to handle -ENOMEM errors in dir-morphing codepaths [1]. As part of that, we added fault injection knobs to a few functions so we could test that error handling.

Add a test case that uses the fail_function facility to inject -ENOMEM into btrfs_prealloc_delayed_dir_index() and confirms that a dir-modifying operation returns ENOMEM to userspace without aborting the filesystem:

- mkdir fails with ENOMEM (not EROFS),
- a subsequent create succeeds (fs is not read-only/aborted),
- a mount cycle runs orphan cleanup and the automatic fsck confirms consistency.

The injection is global, so any other btrfs dir-entry creation can consume it first -- including the test's own $tmp redirection when /tmp is itself on btrfs. Pre-create that file and retry until the mkdir takes the error.

Add generic _require_function_error_injection / _inject_function_error / _uninject_function_error helpers to common/inject for driving the kernel fail_function interface.

[1]: https://lore.kernel.org/linux-btrfs/[email protected]/

---
Changes in v3:
- Call _require_debugfs in _require_function_error_injection
- Add the test to the "dir" group
- Uninject the error in _cleanup, so an interrupted test can't leak it
- Use _exit 0 instead of setting status directly
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add _fixed_by_kernel_commit line
- Clean up changelog
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 common/inject       | 51 +++++++++++++++++++++++++++++
 tests/btrfs/354     | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/btrfs/354.out |  2 ++
 3 files changed, 145 insertions(+)
common/inject +51 -0
index 6b590804d1ea..fc63d3a32c8a 100644
@@ -111,3 +111,54 @@ _scratch_inject_error()
 		_fail "Cannot inject error ${type} value ${value}."
 	fi
 }
+
+# Requires the kernel fail_function facility (CONFIG_FUNCTION_ERROR_INJECTION
+# and CONFIG_FAIL_FUNCTION) and, if a function name is given, that the function
+# is annotated with ALLOW_ERROR_INJECTION().
+_require_function_error_injection()
+{
+	local func="$1"
+
+	_require_debugfs
+	test -d "$DEBUGFS_MNT/fail_function" || \
+		_notrun "$DEBUGFS_MNT/fail_function not found; CONFIG_FAIL_FUNCTION not enabled"
+
+	if [ -n "$func" ]; then
+		grep -qw "$func" "$DEBUGFS_MNT/error_injection/list" 2>/dev/null || \
+			_notrun "$func is not registered for error injection"
+	fi
+}
+
+# Inject a fixed return value into a kernel function via fail_function.
+#   $1 - function name (must be ALLOW_ERROR_INJECTION annotated)
+#   $2 - return value to inject (e.g. -12 for -ENOMEM)
+#   $3 - number of times to fail (default 1)
+_inject_function_error()
+{
+	local func="$1"
+	local retval="$2"
+	local times="${3:-1}"
+
+	# Writing the function name to "inject" creates its per-function dir.
+	echo "$func" > "$DEBUGFS_MNT/fail_function/inject"
+	# The retval file is an unsigned hex attribute (DEFINE_DEBUGFS_ATTRIBUTE
+	# "%llx"), so a negative errno like -12 must be written as its unsigned
+	# 64-bit (two's-complement) hex form; a bare "-12" is rejected with EINVAL.
+	printf '%#x\n' "$retval" > "$DEBUGFS_MNT/fail_function/$func/retval"
+	echo 100 > "$DEBUGFS_MNT/fail_function/probability"
+	echo 0 > "$DEBUGFS_MNT/fail_function/interval"
+	echo 0 > "$DEBUGFS_MNT/fail_function/space"
+	echo 0 > "$DEBUGFS_MNT/fail_function/verbose"
+	echo "$times" > "$DEBUGFS_MNT/fail_function/times"
+}
+
+# Stop injecting errors into a kernel function set up by
+# _inject_function_error().
+_uninject_function_error()
+{
+	local func="$1"
+
+	echo 0 > "$DEBUGFS_MNT/fail_function/times" 2>/dev/null
+	echo 0 > "$DEBUGFS_MNT/fail_function/probability" 2>/dev/null
+	echo "!$func" > "$DEBUGFS_MNT/fail_function/inject" 2>/dev/null
+}
tests/btrfs/354 new file +92 -0

92 lines not on this page · raw message

tests/btrfs/354.out new file +2 -0
new file mode 100644
index 000000000000..afe30ed36fea
@@ -0,0 +1,2 @@
+QA output created by 354
+silence is golden

---
base-commit: a370dcbed43563f0462801e889e0eceb93c7cfad
change-id: 20260915-btrfs-enomem-e70077862117

Best regards,

Signature · 1 line
Jeff Layton <[email protected]>