Skip to contentSkip to conversation

fstests: btrfs: add test case to validate sysfs input arguments

PATCH Superseded Anand Jain 2 participants 9 messages Feb 5, 2025 Btrfs file system

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

v2 cover fstests: btrfs: add test case to validate sysfs input arguments

v2:
Mainly adds a generic function to check if a sysfs attribute reports invalid input.

v1:
https://lwn.net/ml/all/[email protected]/

Anand Jain (5):
fstests: common/rc: set_fs_sysfs_attr: redirect errors to stdout
fstests: filter: helper for sysfs error filtering
fstests: common/rc: add sysfs argument verification helpers
fstests: btrfs: testcase for sysfs policy syntax verification
fstests: btrfs: testcase for sysfs chunk_size attribute validation

 common/filter       |   9 +++
 common/rc           | 139 +++++++++++++++++++++++++++++++++++++++++++-
 tests/btrfs/329     |  18 ++++++
 tests/btrfs/329.out |  19 ++++++
 tests/btrfs/334     |  18 ++++++
 tests/btrfs/334.out |  14 +++++
 6 files changed, 216 insertions(+), 1 deletion(-)
 create mode 100755 tests/btrfs/329
 create mode 100644 tests/btrfs/329.out
 create mode 100755 tests/btrfs/334
 create mode 100644 tests/btrfs/334.out
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH v2 1/5 fstests: common/rc: set_fs_sysfs_attr: redirect errors to stdout

Redirect sysfs write errors to stdout as a preparatory patch to enable testing of expected sysfs write failures. Also, log the executed sysfs write command and its failure if any to seqres.full for better debugging and traceability.

---
 common/rc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
common/rc +2 -1
index c73007fef762..def55ac68771 100644
@@ -5081,7 +5081,8 @@ _set_fs_sysfs_attr()
 
 	local dname=$(_fs_sysfs_dname $dev)
 
-	echo "$content" > /sys/fs/${FSTYP}/${dname}/${attr}
+	echo "echo "$content" 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr}" >> $seqres.full
+	echo "$content" 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr} | tee -a $seqres.full
 }
 
 # Print the content of /sys/fs/$FSTYP/$DEV/$ATTR
Signature · 1 line
2.47.0
author
#2 lore raw

PATCH v2 2/5 fstests: filter: helper for sysfs error filtering

In reply to Anand Jain

Added filter helper to filter sysfs write errors, retain only the error part.

---
 common/filter | 9 +++++++++
 1 file changed, 9 insertions(+)
common/filter +9 -0
index 7e02ded377cc..44ba2b38c21d 100644
@@ -671,5 +671,14 @@ _filter_flakey_EIO()
 	sed -e "s#.*: Input\/output error#$message#"
 }
 
+# Filters
+#      +./common/rc: line 5085: echo: write error: Invalid argument
+# to
+# 	Invalid argument
+_filter_sysfs_error()
+{
+	sed 's/.*: \(.*\)$/\1/'
+}
+
 # make sure this script returns success
 /bin/true
Signature · 1 line
2.47.0
author
#3 lore raw

PATCH v2 3/5 fstests: common/rc: add sysfs argument verification helpers

In reply to Anand Jain

Introduce verify_sysfs_syntax() and _require_fs_sysfs_attr_policy() to verify whether a sysfs attribute rejects invalid input arguments during writes.

---
 common/rc | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
common/rc +136 -0
index def55ac68771..cc5680581608 100644
@@ -5147,6 +5147,142 @@ _require_fs_sysfs_attr()
 	_notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr}"
 }
 
+# Test for the existence of a policy at /sys/fs/$FSTYP/$DEV/$ATTR
+#
+# All arguments are necessary, and in this order:
+#  - dev: device name, e.g. $SCRATCH_DEV
+#  - attr: path name under /sys/fs/$FSTYP/$dev
+#  - policy: policy within /sys/fs/$FSTYP/$dev
+#
+# Usage example:
+#   _has_fs_sysfs_attr_policy /dev/mapper/scratch-dev read_policy round-robin
+_has_fs_sysfs_attr_policy()
+{
+	local dev=$1
+	local attr=$2
+	local policy=$3
+
+	if [ ! -b "$dev" -o -z "$attr" -o -z "$policy" ]; then
+		_fail \
+	     "Usage: _has_fs_sysfs_attr_policy <mounted_device> <attr> <policy>"
+	fi
+
+	local dname=$(_fs_sysfs_dname $dev)
+	test -e /sys/fs/${FSTYP}/${dname}/${attr}
+
+	cat /sys/fs/${FSTYP}/${dname}/${attr} | grep -q ${policy}
+}
+
+# Require the existence of a sysfs entry at /sys/fs/$FSTYP/$DEV/$ATTR
+# and value in it contains $policy
+# All arguments are necessary, and in this order:
+#  - dev: device name, e.g. $SCRATCH_DEV
+#  - attr: path name under /sys/fs/$FSTYP/$dev
+#  - policy: mentioned in /sys/fs/$FSTYP/$dev/$attr
+#
+# Usage example:
+#   _require_fs_sysfs_attr_policy /dev/mapper/scratch-dev read_policy round-robin
+_require_fs_sysfs_attr_policy()
+{
+	_has_fs_sysfs_attr_policy "$@" && return
+
+	local dev=$1
+	local attr=$2
+	local policy=$3
+	local dname=$(_fs_sysfs_dname $dev)
+
+	_notrun "This test requires /sys/fs/${FSTYP}/${dname}/${attr} ${policy}"
+}
+
+set_sysfs_policy()
+{
+	local dev=$1
+	local attr=$2
+	shift
+	shift
+	local policy=$@
+
+	_set_fs_sysfs_attr $dev $attr ${policy}
+
+	case "$FSTYP" in
+	btrfs)
+		_get_fs_sysfs_attr $dev $attr | grep -q "[${policy}]"
+		if [[ $? != 0 ]]; then
+			echo "Setting sysfs $attr $policy failed"
+		fi
+		;;
+	*)
+		_fail \
+"sysfs syntax verification for '${attr}' '${policy}' for '${FSTYP}' is not implemented"
+		;;
+	esac
+}
+
+set_sysfs_policy_must_fail()
+{
+	local dev=$1
+	local attr=$2
+	shift
+	shift
+	local policy=$@
+
+	_set_fs_sysfs_attr $dev $attr ${policy} | _filter_sysfs_error \
+							   | tee -a $seqres.full
+}
+
+# Verify sysfs attribute rejects invalid input.
+# Usage syntax:
+#   verify_sysfs_syntax <$dev> <$attr> <$policy> [$value]
+# Examples:
+#   verify_sysfs_syntax $TEST_DEV read_policy pid
+#   verify_sysfs_syntax $TEST_DEV read_policy round-robin 4k
+# Note:
+#  Process must call . ./common/filter
+verify_sysfs_syntax()
+{
+	local dev=$1
+	local attr=$2
+	local policy=$3
+	local value=$4
+
+	# Do this in the test case so that we know its prerequisites.
+	# '_require_fs_sysfs_attr_policy $TEST_DEV $attr $policy'
+
+	# Test policy specified wrongly. Must fail.
+	set_sysfs_policy_must_fail $dev $attr "'$policy $policy'"
+	set_sysfs_policy_must_fail $dev $attr "'$policy t'"
+	set_sysfs_policy_must_fail $dev $attr "' '"
+	set_sysfs_policy_must_fail $dev $attr "'${policy} n'"
+	set_sysfs_policy_must_fail $dev $attr "'n ${policy}'"
+	set_sysfs_policy_must_fail $dev $attr "' ${policy}'"
+	set_sysfs_policy_must_fail $dev $attr "' ${policy} '"
+	set_sysfs_policy_must_fail $dev $attr "'${policy} '"
+	set_sysfs_policy_must_fail $dev $attr _${policy}
+	set_sysfs_policy_must_fail $dev $attr ${policy}_
+	set_sysfs_policy_must_fail $dev $attr _${policy}_
+	set_sysfs_policy_must_fail $dev $attr ${policy}:
+	# Test policy longer than 32 chars fails stable.
+	set_sysfs_policy_must_fail $dev $attr 'jfdkkkkjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjffjfjfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
+
+	# Test policy specified correctly. Must pass.
+	set_sysfs_policy $dev $attr $policy
+
+	# If the policy has no value return
+	if [[ -z $value ]]; then
+		return
+	fi
+
+	# Test value specified wrongly. Must fail.
+	set_sysfs_policy_must_fail $dev $attr "'$policy: $value'"
+	set_sysfs_policy_must_fail $dev $attr "'$policy:$value '"
+	set_sysfs_policy_must_fail $dev $attr "'$policy:$value typo'"
+	set_sysfs_policy_must_fail $dev $attr "'$policy:${value}typo'"
+	set_sysfs_policy_must_fail $dev $attr "'$policy :$value'"
+
+	# Test policy and value all specified correctly. Must pass.
+	set_sysfs_policy $dev $attr $policy:$value
+}
+
 # Test for the existence of a sysfs entry at /sys/fs/$FSTYP/DEV/$ATTR
 #
 # Only one argument is needed:
Signature · 1 line
2.47.0
#4 lore raw

Re: [PATCH v2 3/5] fstests: common/rc: add sysfs argument verification helpers

Quoted 7 lines · Anand Jain

Introduce verify_sysfs_syntax() and _require_fs_sysfs_attr_policy() to verify whether a sysfs attribute rejects invalid input arguments during writes.

---
 common/rc | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)

Can we move all this sysfs specific stuff to a new common/sysfs file? common/rc is already too large and there's more than enough sysfs specific code here to justify it's own file...

-Dave.

Signature · 2 lines
Dave Chinner
[email protected]
author
#5 lore raw

PATCH v2 4/5 fstests: btrfs: testcase for sysfs policy syntax verification

In reply to Anand Jain

Checks if the sysfs attribute sanitizes arguments and verifies input syntax.

---
 tests/btrfs/329     | 18 ++++++++++++++++++
 tests/btrfs/329.out | 19 +++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100755 tests/btrfs/329
 create mode 100644 tests/btrfs/329.out
tests/btrfs/329 new file +18 -0
new file mode 100755
index 000000000000..3b52d51bb477
@@ -0,0 +1,18 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 Oracle.  All Rights Reserved.
+#
+# FS QA Test 329
+#
+# Verify sysfs knob input syntax for read_policy round-robin
+#
+. ./common/preamble
+_begin_fstest auto quick
+
+. ./common/filter
+
+_require_fs_sysfs_attr_policy $TEST_DEV read_policy round-robin
+verify_sysfs_syntax $TEST_DEV read_policy round-robin 4k
+
+status=0
+exit
tests/btrfs/329.out new file +19 -0
new file mode 100644
index 000000000000..eff7573adb6a
@@ -0,0 +1,19 @@
+QA output created by 329
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
Signature · 1 line
2.47.0
author
#6 lore raw

PATCH v2 5/5 fstests: btrfs: testcase for sysfs chunk_size attribute validation

In reply to Anand Jain

Checks if the sysfs attribute sanitizes arguments and verifies input syntax allocation/data/chunk_size.

---
 tests/btrfs/334     | 18 ++++++++++++++++++
 tests/btrfs/334.out | 14 ++++++++++++++
 2 files changed, 32 insertions(+)
 create mode 100755 tests/btrfs/334
 create mode 100644 tests/btrfs/334.out
tests/btrfs/334 new file +18 -0
new file mode 100755
index 000000000000..b662fe2aca63
@@ -0,0 +1,18 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 Oracle.  All Rights Reserved.
+#
+# FS QA Test 334
+#
+# Verify sysfs knob input syntax for allocation/data/chunk_size
+#
+. ./common/preamble
+_begin_fstest auto quick
+
+. ./common/filter
+
+_require_fs_sysfs_attr $TEST_DEV allocation/data/chunk_size
+verify_sysfs_syntax $TEST_DEV allocation/data/chunk_size 256m
+
+status=0
+exit
tests/btrfs/334.out new file +14 -0
new file mode 100644
index 000000000000..f64f9ac09499
@@ -0,0 +1,14 @@
+QA output created by 334
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
+Invalid argument
Signature · 1 line
2.47.0
#7 lore raw
In reply to Anand Jain
Quoted 7 lines · Anand Jain

v2:
Mainly adds a generic function to check if a sysfs attribute reports invalid input.

v1:
https://lwn.net/ml/all/[email protected]/

Quoted 6 lines

Anand Jain (5):
fstests: common/rc: set_fs_sysfs_attr: redirect errors to stdout
fstests: filter: helper for sysfs error filtering
fstests: common/rc: add sysfs argument verification helpers
fstests: btrfs: testcase for sysfs policy syntax verification
fstests: btrfs: testcase for sysfs chunk_size attribute validation

All looks ok, except for my comment about creating common/sysfs for all the sysfs stuff. The individual tests look much simpler and nicer, too.

-Dave.

Signature · 2 lines
Dave Chinner
[email protected]
author
#8 lore raw
Quoted 15 lines · Dave Chinner
Quoted 7 lines · Anand Jain

v2:
Mainly adds a generic function to check if a sysfs attribute reports invalid input.

v1:
https://lwn.net/ml/all/[email protected]/

Quoted 6 lines

Anand Jain (5):
fstests: common/rc: set_fs_sysfs_attr: redirect errors to stdout
fstests: filter: helper for sysfs error filtering
fstests: common/rc: add sysfs argument verification helpers
fstests: btrfs: testcase for sysfs policy syntax verification
fstests: btrfs: testcase for sysfs chunk_size attribute validation

Quoted 4 lines

All looks ok, except for my comment about creating common/sysfs for all the sysfs stuff. The individual tests look much simpler and nicer, too.

Yeah, the sysfs functions need a new file. I'll send the reorg cleanups.

Thx, Anand

Quoted 1 line

-Dave.