Skip to contentSkip to conversation

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

PATCH Superseded Anand Jain 2 participants 13 messages Apr 7, 2025 – Apr 8, 2025 Btrfs file system

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

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

v5:
- Added rb.
- New patch (2/6) for unset seqres (can merge into 1/6, but skipped as 1/6 got rb).
- Fixed git am whitespace.
- Renamed patch (was: fstests: common/rc: add sysfs argument verification helpers).
- Prefixed helpers function in common/sysfs with _.
- Updated function names in last two patches.

v4:
https://lore.kernel.org/fstests/[email protected]/

v3:
https://lore.kernel.org/fstests/[email protected]/

v2:
https://lore.kernel.org/fstests/[email protected]/

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

Anand Jain (6):
fstests: common/rc: set_fs_sysfs_attr: redirect errors to stdout
fstests: common/rc: fix unset seqres in _set_fs_sysfs_attr
fstests: filter: helper for sysfs error filtering
fstests: common/sysfs: add new file sysfs and helpers
fstests: btrfs: testcase for sysfs policy syntax verification
fstests: btrfs: testcase for sysfs chunk_size attribute validation

 common/filter       |   9 +++
 common/rc           |  10 +++-
 common/sysfs        | 141 ++++++++++++++++++++++++++++++++++++++++++++
 tests/btrfs/329     |  19 ++++++
 tests/btrfs/329.out |  19 ++++++
 tests/btrfs/334     |  19 ++++++
 tests/btrfs/334.out |  14 +++++
 7 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 common/sysfs
 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 v5 1/6 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 16d627e1bdd4..e89eee5de840 100644
@@ -5208,7 +5208,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 v5 2/6 fstests: common/rc: fix unset seqres in _set_fs_sysfs_attr

In reply to Anand Jain

Ensure logs don’t write to a .full file when _set_fs_sysfs_attr() is called during setup (before a testcase) in XFS due to unset seqres.

---
 common/rc | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
common/rc +9 -2
index e89eee5de840..ca1d13ca1f0b 100644
@@ -5201,6 +5201,13 @@ _set_fs_sysfs_attr()
 	local attr=$1
 	shift
 	local content="$*"
+	local logfile="/dev/null"
+
+	# This function may be called outside a testcase during setup,
+	# so seqres might not be set.
+	if [[ -v seqres ]]; then
+		logfile="$seqres.full"
+	fi
 
 	if [ ! -b "$dev" -o -z "$attr" -o -z "$content" ];then
 		_fail "Usage: _set_fs_sysfs_attr <mounted_device> <attr> <content>"
@@ -5208,8 +5215,8 @@ _set_fs_sysfs_attr()
 
 	local dname=$(_fs_sysfs_dname $dev)
 
-	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
+	echo "echo '$content' 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr}" >> $logfile
+	echo "$content" 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr} | tee -a $logfile
 }
 
 # Print the content of /sys/fs/$FSTYP/$DEV/$ATTR
Signature · 1 line
2.47.0
author
#6 lore raw

PATCH v5 3/6 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 1ebfd27e898e..bbe13f4c8a8d 100644
@@ -674,5 +674,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
#7 lore raw

PATCH v5 4/6 fstests: common/sysfs: add new file sysfs and 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/sysfs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 common/sysfs
common/sysfs new file +141 -0
new file mode 100644
index 000000000000..9f2d77c6b1f5
@@ -0,0 +1,141 @@
+##/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (c) 2025 Oracle.  All Rights Reserved.
+#
+# Common/sysfs file for the sysfs related helper functions.
+
+# 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
+}
Signature · 1 line
2.47.0
author
#11 lore raw

PATCH v5 5/6 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     | 19 +++++++++++++++++++
 tests/btrfs/329.out | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100755 tests/btrfs/329
 create mode 100644 tests/btrfs/329.out
tests/btrfs/329 new file +19 -0
new file mode 100755
index 000000000000..c87e96a4f38a
@@ -0,0 +1,19 @@
+#! /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/sysfs
+. ./common/filter
+
+_require_fs_sysfs_attr_policy $TEST_DEV read_policy round-robin
+_verify_sysfs_syntax $TEST_DEV read_policy round-robin 4096
+
+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
#12 lore raw

PATCH v5 6/6 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     | 19 +++++++++++++++++++
 tests/btrfs/334.out | 14 ++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100755 tests/btrfs/334
 create mode 100644 tests/btrfs/334.out
tests/btrfs/334 new file +19 -0
new file mode 100755
index 000000000000..fd1ac67f9000
@@ -0,0 +1,19 @@
+#! /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/sysfs
+. ./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
#3 lore raw

Re: [PATCH v5 2/6] fstests: common/rc: fix unset seqres in _set_fs_sysfs_attr

In reply to Anand Jain
Quoted 13 lines · Anand Jain

Ensure logs don’t write to a .full file when _set_fs_sysfs_attr() is called during setup (before a testcase) in XFS due to unset seqres.

---
 common/rc | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
common/rc +0 -0
index e89eee5de840..ca1d13ca1f0b 100644

common/rc _set_fs_sysfs_attr() · line 5204

Quoted 4 lines
common/rc +1 -0
@@ -5201,6 +5201,13 @@ _set_fs_sysfs_attr()
 	local attr=$1
 	shift
 	local content="$*"
+	local logfile="/dev/null"

If we're not actually running a test, should the error output go to $check.full instead?

--D

Quoted 24 lines
common/rc +8 -2
+
+	# This function may be called outside a testcase during setup,
+	# so seqres might not be set.
+	if [[ -v seqres ]]; then
+		logfile="$seqres.full"
+	fi
 
 	if [ ! -b "$dev" -o -z "$attr" -o -z "$content" ];then
 		_fail "Usage: _set_fs_sysfs_attr <mounted_device> <attr> <content>"
@@ -5208,8 +5215,8 @@ _set_fs_sysfs_attr()
 
 	local dname=$(_fs_sysfs_dname $dev)
 
-	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
+	echo "echo '$content' 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr}" >> $logfile
+	echo "$content" 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr} | tee -a $logfile
 }
 
 # Print the content of /sys/fs/$FSTYP/$DEV/$ATTR
Signature · 1 line
2.47.0
#8 lore raw

Re: [PATCH v5 4/6] fstests: common/sysfs: add new file sysfs and helpers

In reply to Anand Jain
Quoted 31 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/sysfs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 common/sysfs
common/sysfs new file +15 -0
new file mode 100644
index 000000000000..9f2d77c6b1f5
@@ -0,0 +1,141 @@
+##/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (c) 2025 Oracle.  All Rights Reserved.
+#
+# Common/sysfs file for the sysfs related helper functions.
+
+# 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

common/sysfs line 25

Quoted 10 lines
common/sysfs new file +10 -0
+_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

Shouldn't this return 1 if the parameters are not fully specified?

common/sysfs line 28

Quoted 3 lines
common/sysfs new file +3 -0
+
+	local dname=$(_fs_sysfs_dname $dev)
+	test -e /sys/fs/${FSTYP}/${dname}/${attr}

What is the point of testing path existence here if the function doesn't actually change its behavior?

Quoted 68 lines
common/sysfs new file +68 -0
+
+	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:

common/sysfs line 106

Quoted 10 lines
common/sysfs new file +10 -0
+#  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'

But it's commented out ... ?

--D

Quoted 39 lines
common/sysfs new file +35 -0
+
+	# 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
+}
Signature · 1 line
2.47.0
author
#4 lore raw

Re: [PATCH v5 2/6] fstests: common/rc: fix unset seqres in _set_fs_sysfs_attr

In reply to Darrick J. Wong
Quoted 9 lines · Darrick J. Wong
Quoted 8 lines · Anand Jain

Ensure logs don’t write to a .full file when _set_fs_sysfs_attr() is called during setup (before a testcase) in XFS due to unset seqres.

---
  common/rc | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
Quoted 13 lines

common/rc _set_fs_sysfs_attr() · line 5204

Quoted 9 lines
common/rc +1 -0
index e89eee5de840..ca1d13ca1f0b 100644
@@ -5201,6 +5201,13 @@ _set_fs_sysfs_attr()
  	local attr=$1
  	shift
  	local content="$*"
+	local logfile="/dev/null"

If we're not actually running a test, should the error output go to $check.full instead?

Ah. I didn't know. It seems like no one is using check.full.

-----

$ find . -type f -exec grep check\.full {} \; -print
	rm -f $check.full
./check
-----

Thanks, Anand

Quoted 26 lines

--D

Quoted 24 lines
common/rc +8 -2
+
+	# This function may be called outside a testcase during setup,
+	# so seqres might not be set.
+	if [[ -v seqres ]]; then
+		logfile="$seqres.full"
+	fi
 
  	if [ ! -b "$dev" -o -z "$attr" -o -z "$content" ];then
  		_fail "Usage: _set_fs_sysfs_attr <mounted_device> <attr> <content>"
@@ -5208,8 +5215,8 @@ _set_fs_sysfs_attr()
 
  	local dname=$(_fs_sysfs_dname $dev)
 
-	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
+	echo "echo '$content' 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr}" >> $logfile
+	echo "$content" 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr} | tee -a $logfile
  }
 
  # Print the content of /sys/fs/$FSTYP/$DEV/$ATTR
Signature · 1 line
2.47.0
author
#9 lore raw

Re: [PATCH v5 4/6] fstests: common/sysfs: add new file sysfs and helpers

In reply to Darrick J. Wong
Quoted 36 lines · Darrick J. Wong
Quoted 35 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/sysfs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 141 insertions(+)

create mode 100644 common/sysfs

common/sysfs new file +19 -0
new file mode 100644
index 000000000000..9f2d77c6b1f5
@@ -0,0 +1,141 @@
+##/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (c) 2025 Oracle.  All Rights Reserved.
+#
+# Common/sysfs file for the sysfs related helper functions.
+
+# 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
Quoted 9 lines

common/sysfs line 25

Quoted 6 lines
common/sysfs new file +6 -0
+	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

Shouldn't this return 1 if the parameters are not fully specified?

Do you mean, instead of using _fail, just echo "Usage" and return 1?

Quoted 7 lines

common/sysfs line 28

Quoted 3 lines
common/sysfs new file +3 -0
+
+	local dname=$(_fs_sysfs_dname $dev)
+	test -e /sys/fs/${FSTYP}/${dname}/${attr}

What is the point of testing path existence here if the function doesn't actually change its behavior?

Ah, I missed the if—updated it locally now.

@@ -25,7 +25,9 @@ _has_fs_sysfs_attr_policy()

        fi

        local dname=$(_fs_sysfs_dname $dev)

- test -e /sys/fs/${FSTYP}/${dname}/${attr}
+ if test -e /sys/fs/${FSTYP}/${dname}/${attr}; then
+ return 1
+ fi

        cat /sys/fs/${FSTYP}/${dname}/${attr} | grep -q ${policy}

}

Quoted 72 lines
Quoted 72 lines
common/sysfs new file +72 -0
+
+	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
Quoted 8 lines

common/sysfs line 106

Quoted 6 lines
common/sysfs new file +6 -0
+	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'

But it's commented out ... ?

Yeah, I commented it out since it's just an example. I think I need to rephrase the comment.

Thanks, Anand

Quoted 42 lines

--D

Quoted 39 lines
common/sysfs new file +35 -0
+
+	# 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
+}
Signature · 1 line
2.47.0
#5 lore raw

Re: [PATCH v5 2/6] fstests: common/rc: fix unset seqres in _set_fs_sysfs_attr

In reply to Anand Jain
Quoted 26 lines · Anand Jain
Quoted 9 lines · Darrick J. Wong
Quoted 8 lines · Anand Jain

Ensure logs don’t write to a .full file when _set_fs_sysfs_attr() is called during setup (before a testcase) in XFS due to unset seqres.

---
  common/rc | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
Quoted 13 lines

common/rc _set_fs_sysfs_attr() · line 5204

Quoted 9 lines
common/rc +1 -0
index e89eee5de840..ca1d13ca1f0b 100644
@@ -5201,6 +5201,13 @@ _set_fs_sysfs_attr()
  	local attr=$1
  	shift
  	local content="$*"
+	local logfile="/dev/null"

If we're not actually running a test, should the error output go to $check.full instead?

Quoted 1 line

Ah. I didn't know. It seems like no one is using check.full.

Oh, they do, but it's subtle:

$ git grep seqres=
check:852:      seqres="$check"
check:1120:     seqres="$check"

--D

Quoted 33 lines

-----

$ find . -type f -exec grep check\.full {} \; -print
	rm -f $check.full
./check
-----

Thanks, Anand

Quoted 22 lines

--D

Quoted 20 lines
common/rc +8 -2
+
+	# This function may be called outside a testcase during setup,
+	# so seqres might not be set.
+	if [[ -v seqres ]]; then
+		logfile="$seqres.full"
+	fi
  	if [ ! -b "$dev" -o -z "$attr" -o -z "$content" ];then
  		_fail "Usage: _set_fs_sysfs_attr <mounted_device> <attr> <content>"
@@ -5208,8 +5215,8 @@ _set_fs_sysfs_attr()
  	local dname=$(_fs_sysfs_dname $dev)
-	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
+	echo "echo '$content' 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr}" >> $logfile
+	echo "$content" 2>&1 > /sys/fs/${FSTYP}/${dname}/${attr} | tee -a $logfile
  }
  # Print the content of /sys/fs/$FSTYP/$DEV/$ATTR
Signature · 1 line
2.47.0
#10 lore raw

Re: [PATCH v5 4/6] fstests: common/sysfs: add new file sysfs and helpers

In reply to Anand Jain
Quoted 49 lines · Anand Jain
Quoted 36 lines · Darrick J. Wong
Quoted 35 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/sysfs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 141 insertions(+)

create mode 100644 common/sysfs

common/sysfs new file +19 -0
new file mode 100644
index 000000000000..9f2d77c6b1f5
@@ -0,0 +1,141 @@
+##/bin/bash
+# SPDX-License-Identifier: GPL-2.0+
+# Copyright (c) 2025 Oracle.  All Rights Reserved.
+#
+# Common/sysfs file for the sysfs related helper functions.
+
+# 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
Quoted 9 lines

common/sysfs line 25

Quoted 6 lines
common/sysfs new file +6 -0
+	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

Shouldn't this return 1 if the parameters are not fully specified?

Quoted 1 line

Do you mean, instead of using _fail, just echo "Usage" and return 1?

Doh, I missed that it was _fail and not echo. Please ignore my comment.

Quoted 12 lines
Quoted 7 lines

common/sysfs line 28

Quoted 3 lines
common/sysfs new file +3 -0
+
+	local dname=$(_fs_sysfs_dname $dev)
+	test -e /sys/fs/${FSTYP}/${dname}/${attr}

What is the point of testing path existence here if the function doesn't actually change its behavior?

Ah, I missed the if—updated it locally now.

Review _has_fs_sysfs_attr_policy() · line 30

Quoted 7 lines
(quoted diff) +3 -1
@@ -25,7 +25,9 @@ _has_fs_sysfs_attr_policy()
        fi
 
        local dname=$(_fs_sysfs_dname $dev)
-       test -e /sys/fs/${FSTYP}/${dname}/${attr}
+       if test -e /sys/fs/${FSTYP}/${dname}/${attr}; then
+               return 1
+       fi

<nod>

Quoted 85 lines
(quoted diff) +0 -0
        cat /sys/fs/${FSTYP}/${dname}/${attr} | grep -q ${policy}
 }
 
Quoted 73 lines
Quoted 73 lines
common/sysfs new file +72 -0
+
+	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
Quoted 8 lines

common/sysfs line 106

Quoted 6 lines
common/sysfs new file +6 -0
+	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'

But it's commented out ... ?

Quoted 2 lines

Yeah, I commented it out since it's just an example. I think I need to rephrase the comment.

Ok.

--D

Quoted 46 lines

Thanks, Anand

Quoted 42 lines

--D

Quoted 39 lines
common/sysfs new file +35 -0
+
+	# 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
+}
Signature · 1 line
2.47.0