common/rc: factor out _scratch_xfs_[get|set]_sb_field
authorHou Tao <houtao1@huawei.com>
Thu, 11 Jan 2018 06:49:33 +0000 (14:49 +0800)
committerEryu Guan <eguan@redhat.com>
Fri, 12 Jan 2018 03:40:39 +0000 (11:40 +0800)
It's common to get and set the values of fields in XFS super block,
so factor them out as scratch_xfs_[get|set]_sb_field, reimplement
them based on _scratch_xfs_[get|set]_metadata_field, and update the
related test cases accordingly.

Also move _scratch_xfs_[get|set]_metadata_field from common/fuzzy to
common/xfs.

Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Eryu Guan <eguan@redhat.com>
common/fuzzy
common/xfs
tests/xfs/007
tests/xfs/098
tests/xfs/186
tests/xfs/199
tests/xfs/307
tests/xfs/308
tests/xfs/339
tests/xfs/340

index d70964f4766133649ddf13e9947f2eafd60a7b94..9a0456fafaf4560d780b4c62029b68d90b84f7da 100644 (file)
@@ -126,39 +126,6 @@ _scratch_xfs_list_metadata_fields() {
        _scratch_xfs_db "${cmds[@]}" -c print | __filter_xfs_db_print_fields "${filter}"
 }
 
-# Get a metadata field
-# The first arg is the field name
-# The rest of the arguments are xfs_db commands to find the metadata.
-_scratch_xfs_get_metadata_field() {
-       key="$1"
-       shift
-
-       grep_key="$(echo "${key}" | tr '[]()' '....')"
-       local cmds=()
-       for arg in "$@"; do
-               cmds+=("-c" "${arg}")
-       done
-       _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
-               sed -e 's/^.* = //g'
-}
-
-# Set a metadata field
-# The first arg is the field name
-# The second arg is the new value
-# The rest of the arguments are xfs_db commands to find the metadata.
-_scratch_xfs_set_metadata_field() {
-       key="$1"
-       value="$2"
-       shift; shift
-
-       local cmds=()
-       for arg in "$@"; do
-               cmds+=("-c" "${arg}")
-       done
-       _scratch_xfs_db -x "${cmds[@]}" -c "write -d ${key} ${value}"
-       echo
-}
-
 # Fuzz a metadata field
 # The first arg is the field name
 # The second arg is the xfs_db fuzz verb
index 5cfa761248833bc41feee2d1cee1a2e0a28a3905..45b84a04f46c1c2cf8222f4538441510fe6d8741 100644 (file)
@@ -624,3 +624,50 @@ _require_no_xfs_debug()
                _notrun "Require XFS built without CONFIG_XFS_DEBUG"
        fi
 }
+
+# Get a metadata field
+# The first arg is the field name
+# The rest of the arguments are xfs_db commands to find the metadata.
+_scratch_xfs_get_metadata_field()
+{
+       local key="$1"
+       shift
+
+       local grep_key="$(echo "${key}" | tr '[]()' '....')"
+       local cmds=()
+       local arg
+       for arg in "$@"; do
+               cmds+=("-c" "${arg}")
+       done
+       _scratch_xfs_db "${cmds[@]}" -c "print ${key}" | grep "^${grep_key}" | \
+               sed -e 's/^.* = //g'
+}
+
+# Set a metadata field
+# The first arg is the field name
+# The second arg is the new value
+# The rest of the arguments are xfs_db commands to find the metadata.
+_scratch_xfs_set_metadata_field()
+{
+       local key="$1"
+       local value="$2"
+       shift; shift
+
+       local cmds=()
+       local arg
+       for arg in "$@"; do
+               cmds+=("-c" "${arg}")
+       done
+       _scratch_xfs_db -x "${cmds[@]}" -c "write -d ${key} -- ${value}"
+       echo
+}
+
+_scratch_xfs_get_sb_field()
+{
+       _scratch_xfs_get_metadata_field "$1" "sb 0"
+}
+
+_scratch_xfs_set_sb_field()
+{
+       _scratch_xfs_set_metadata_field "$1" "$2" "sb 0"
+}
index d80d380f22e6f9755e789639726783b0c94d511e..215bd1d58be712f692b9988cd0b710f801bd5a9f 100755 (executable)
@@ -62,10 +62,8 @@ do_test()
        echo "*** umount"
        _scratch_unmount
 
-       QINO_1=`xfs_db -c "sb 0" -c "p" $SCRATCH_DEV | \
-                       grep $qino_1 | awk '{print $NF}'`
-       QINO_2=`xfs_db -c "sb 0" -c "p" $SCRATCH_DEV | \
-                       grep $qino_2 | awk '{print $NF}'`
+       QINO_1=`_scratch_xfs_get_sb_field $qino_1`
+       QINO_2=`_scratch_xfs_get_sb_field $qino_2`
 
        echo "*** Usage before quotarm ***"
        _scratch_xfs_db -c "inode $QINO_1" -c "p core.nblocks"
index 7873f3226658213d5171a99889d5a04a56b8ec0f..9bcd94b0c0c74d8768a972d98e67f02630c9f030 100755 (executable)
@@ -96,9 +96,9 @@ echo "+ check fs"
 _scratch_xfs_repair -n >> $seqres.full 2>&1 || _fail "xfs_repair should not fail"
 
 echo "+ corrupt image"
-logstart="$(_scratch_xfs_db -c 'sb 0' -c 'p' | grep '^logstart =' | cut -d ' ' -f 3)"
+logstart="$(_scratch_xfs_get_sb_field logstart)"
 logstart="$(_scratch_xfs_db -c "convert fsblock ${logstart} byte" | sed -e 's/^.*(\([0-9]*\).*$/\1/g')"
-logblocks="$(xfs_db -c 'sb 0' -c 'p' "${SCRATCH_DEV}" | grep '^logblocks =' | cut -d ' ' -f 3)"
+logblocks="$(_scratch_xfs_get_sb_field logblocks)"
 $XFS_IO_PROG -f -c "pwrite -S 0x62 ${logstart} $((logblocks * blksz))" "${SCRATCH_DEV}" >> $seqres.full
 
 echo "+ mount image"
index 4b36ae68348033bc62ff28b70088023d041b7123..a4527ed77d2e69a4198187d8118247abc3ec81a6 100755 (executable)
@@ -173,8 +173,7 @@ fi
 
 # set inum to root dir ino
 # we'll add in dirents and EAs into the root directory
-eval `_scratch_xfs_db -r -c 'sb 0' -c 'p rootino' | $SED_PROG 's/ //g'`
-inum=$rootino
+inum=`_scratch_xfs_get_sb_field rootino`
 fork_dir=$SCRATCH_MNT
 _print_inode
 
index ee26439591372d4124fded580e8d82f50e0f20f6..a0f48ad90b32ec3bc6f10ad02fd9f7ce46fa5964 100755 (executable)
@@ -49,11 +49,6 @@ _supported_os Linux
 
 _require_scratch
 
-get_features()
-{
-       _scratch_xfs_db -x  -c "sb" -c "print $1" | awk '// {print $3}'
-}
-
 # clear any mkfs options so that we can directly specify the options we need to
 # be able to test the features bitmask behaviour correctly.
 MKFS_OPTIONS=
@@ -63,8 +58,8 @@ _scratch_mkfs_xfs -m crc=0 -l lazy-count=1 >/dev/null 2>&1
 # gives us the values the resultant tests should be the same as for testing at
 # the end of the test. We don't hard code feature values, because that makes the
 # test break every time we change mkfs feature defaults.
-f2=`get_features features2`
-bf2=`get_features bad_features2`
+f2=`_scratch_xfs_get_sb_field features2`
+bf2=`_scratch_xfs_get_sb_field bad_features2`
 
 #
 # Now clear the normal flags
@@ -74,7 +69,7 @@ _scratch_xfs_db -x  -c 'sb' -c 'write features2 0'
 
 _scratch_mount
 _scratch_unmount
-rwf2=`get_features features2`
+rwf2=`_scratch_xfs_get_sb_field features2`
 
 #
 # Clear the normal flags again for the second rount.
@@ -88,7 +83,7 @@ _scratch_xfs_db -x  -c 'sb' -c 'write features2 0'
 _scratch_mount -o ro
 _scratch_mount -o remount,rw
 _scratch_unmount
-rof2=`get_features features2`
+rof2=`_scratch_xfs_get_sb_field features2`
 
 [ "$f2" != "$bf2" ] && echo "mkfs: features2 $f2 != bad_features2 $bf2"
 [ "$f2" != "$rwf2" ] && echo "mount rw: old features2 $f2 != new features2 $rwf2"
index 4ce3e66342b5816f253015ccfa936a9149f41cde..d8295245232c8b04ef85c2503ea1668688c983f1 100755 (executable)
@@ -71,18 +71,11 @@ _set_agf_data() {
 }
 
 _get_sb_data() {
-       field="$1"
-       shift
-
-       _scratch_xfs_db -c 'sb 0' "$@" -c "p $field"  | awk '{print $3}'
+       _scratch_xfs_get_sb_field "$@"
 }
 
 _set_sb_data() {
-       field="$1"
-       value="$2"
-       shift; shift
-
-       _scratch_xfs_db -x -c 'sb 0' "$@" -c "write $field -- $value"  >> $seqres.full
+       _scratch_xfs_set_sb_field "$@" >> $seqres.full
 }
 
 _filter_leftover() {
index e9d7f768d0d166ec01f8f24c912d266799c47736..7e7adac36d3fd38d9b59d448b70507e451f93997 100755 (executable)
@@ -71,18 +71,11 @@ _set_agf_data() {
 }
 
 _get_sb_data() {
-       field="$1"
-       shift
-
-       _scratch_xfs_db -c 'sb 0' "$@" -c "p $field"  | awk '{print $3}'
+       _scratch_xfs_get_sb_field "$@"
 }
 
 _set_sb_data() {
-       field="$1"
-       value="$2"
-       shift; shift
-
-       _scratch_xfs_db -x -c 'sb 0' "$@" -c "write $field -- $value"  >> $seqres.full
+       _scratch_xfs_set_sb_field "$@" >> $seqres.full
 }
 
 _filter_leftover() {
index 734d47bb3b8b469c6aaadf9bf4d5661fd99cf701..78b714f9651ade226cc73f5723ac3361a399e939 100755 (executable)
@@ -59,9 +59,9 @@ ln $SCRATCH_MNT/f3 $SCRATCH_MNT/f4
 _scratch_unmount
 
 echo "Corrupt fs"
-rrmapino=$(_scratch_xfs_db -c 'sb 0' -c 'p rrmapino' | awk '{print $3}')
-_scratch_xfs_db -x -c 'sb 0' -c 'addr rootino' \
-       -c "write u3.sfdir3.list[3].inumber.i4 $rrmapino" >> $seqres.full
+rrmapino=`_scratch_xfs_get_sb_field rrmapino`
+_scratch_xfs_set_metadata_field "u3.sfdir3.list[3].inumber.i4" $rrmapino \
+       'sb 0' 'addr rootino' >> $seqres.full
 _scratch_mount
 
 echo "Check files"
index 8cc50a29d91d1db3e218b1827aae751076dfc143..7b1b7d13b1a2518b944eb417e51a38173b5d26bd 100755 (executable)
@@ -59,7 +59,7 @@ ino=$(stat -c '%i' $SCRATCH_MNT/f3)
 _scratch_unmount
 
 echo "Corrupt fs"
-rrmapino=$(_scratch_xfs_db -c 'sb 0' -c 'p rrmapino' | awk '{print $3}')
+rrmapino=$(_scratch_xfs_get_sb_field rrmapino)
 _scratch_xfs_db -x -c "inode $rrmapino" \
        -c 'write core.format 2' -c 'write core.size 0' \
        -c 'write core.nblocks 0' -c 'sb 0' -c 'addr rootino' \