On Fri, Feb 28, 2025 at 01:55:21PM +0800, Anand Jain wrote: > Introduce `verify_sysfs_syntax()` and `_require_fs_sysfs_attr_policy()` to verify > whether a sysfs attribute rejects invalid input arguments during writes. > > Signed-off-by: Anand Jain <anand.jain@xxxxxxxxxx> > --- The subject is "common/rc: ...", but this patch isn't about common/rc. So feel free to remove the "/rc", or use "/sysfs". > common/sysfs | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 142 insertions(+) > create mode 100644 common/sysfs > > diff --git a/common/sysfs b/common/sysfs > new file mode 100644 > index 000000000000..1362a1261dfc > --- /dev/null > +++ b/common/sysfs > @@ -0,0 +1,142 @@ > +##/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 > +} > + I got this warning: Applying: fstests: common/rc: add sysfs argument verification helpers .git/rebase-apply/patch:153: new blank line at EOF. + warning: 1 line adds whitespace errors. Other looks good to me, Reviewed-by: Zorro Lang <zlang@xxxxxxxxxx> Thanks, Zorro > -- > 2.47.0 > >