fstests: move test group info to test files
[xfstests-dev.git] / tests / generic / 619
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2020 IBM Corporation. All Rights Reserved.
4 #
5 # FS QA Test 619
6 #
7 # ENOSPC regression test in a multi-threaded scenario. Test allocation
8 # strategies of the file system and validate space anomalies as reported by
9 # the system versus the allocated by the program.
10 #
11 # The test is motivated by a bug in ext4 systems where-in ENOSPC is
12 # reported by the file system even though enough space for allocations is
13 # available[1].
14 # [1]: https://patchwork.ozlabs.org/patch/1294003
15 #
16 # Linux kernel patch series that fixes the above regression:
17 # 53f86b170dfa ("ext4: mballoc: add blocks to PA list under same spinlock
18 #               after allocating blocks")
19 # cf5e2ca6c990 ("ext4: mballoc: refactor ext4_mb_discard_preallocations()")
20 # 07b5b8e1ac40 ("ext4: mballoc: introduce pcpu seqcnt for freeing PA to
21 #               improve ENOSPC handling")
22 # 8ef123fe02ca ("ext4: mballoc: refactor ext4_mb_good_group()")
23 # 993778306e79 ("ext4: mballoc: use lock for checking free blocks while
24 #               retrying")
25 #
26 . ./common/preamble
27 _begin_fstest auto rw enospc
28
29 FS_SIZE=$((240*1024*1024)) # 240MB
30 DEBUG=1 # set to 0 to disable debug statements in shell and c-prog
31 FACT=0.7
32
33 # Disk allocation methods
34 FALLOCATE=1
35 FTRUNCATE=2
36
37 # Helps to build TEST_VECTORS
38 SMALL_FILE_SIZE=$((512 * 1024)) # in Bytes
39 BIG_FILE_SIZE=$((1536 * 1024))  # in Bytes
40 MIX_FILE_SIZE=$((2048 * 1024))  # (BIG + SMALL small file size)
41
42 # Import common functions.
43 . ./common/filter
44
45 # Modify as appropriate.
46 _supported_fs generic
47 _require_scratch
48 _require_test_program "t_enospc"
49 _require_xfs_io_command "falloc"
50
51 debug()
52 {
53         if [ $DEBUG -eq 1 ]; then
54                 echo "$1" >> $seqres.full
55         fi
56 }
57
58 # Calculate the number of threads needed to fill the disk space
59 # Arguments
60 # $1: the size of a file
61 # $2: ratio in which $1 file should be split into multiple files.
62 # $3: percentage of the disk space should be used during the test
63 # Calculate the number of threads needed to fill the disk space
64 calc_thread_cnt()
65 {
66         local file_ratio_unit=$1
67         local file_ratio=$2
68         local disk_saturation=$3
69         local tot_avail_size
70         local avail_size
71         local thread_cnt
72
73         IFS=',' read -ra fratio <<< $file_ratio
74         file_ratio_cnt=${#fratio[@]}
75
76         tot_avail_size=$($DF_PROG --block-size=1 $SCRATCH_MNT | $AWK_PROG 'FNR == 2 { print $5 }')
77         avail_size=$(echo $tot_avail_size*$disk_saturation | $BC_PROG)
78         thread_cnt=$(echo "$file_ratio_cnt*($avail_size/$file_ratio_unit)" | $BC_PROG)
79
80         debug "Total available size: $tot_avail_size"
81         debug "Available size: $avail_size"
82         debug "Thread count: $thread_cnt"
83
84         echo ${thread_cnt}
85 }
86
87 # Arguments
88 # $1: a string containing test configuration separated by a colon.
89 #     $1 is treated as an array of arguments to the function.
90 #     Description of each array element is given below.
91 #
92 # @1: name of the test
93 # @2: thread in t_enospec exerciser will allocate file of @2 size
94 # @3: defines the proportion in which the file size defined in @2
95 #     should be divided into two files.
96 #     (valid @3: more than two values are not allowed)
97 #                values should be comma separated)
98 #                sum of all values must be 1)
99 # @4: define the percentage of available memory should be used to
100 #     during the test.
101 # @5: defines the disk allocation method (fallocate/ftruncate)
102 # @6: number of the test should run
103 run_testcase()
104 {
105         IFS=':' read -ra args <<< $1
106         local test_name=${args[0]}
107         local file_ratio_unit=${args[1]}
108         local file_ratio=${args[2]}
109         local disk_saturation=${args[3]}
110         local disk_alloc_method=${args[4]}
111         local test_iteration_cnt=${args[5]}
112         local extra_args=""
113         local thread_cnt
114
115         if [ "$disk_alloc_method" == "$FALLOCATE" ]; then
116                 extra_args="$extra_args -f"
117         fi
118
119         # enable the debug statements in c program
120         if [ "$DEBUG" -eq 1 ]; then
121                 extra_args="$extra_args -v"
122         fi
123
124         debug "============ Test details start ============"
125         debug "Test name: $test_name"
126         debug "File ratio unit: $file_ratio_unit"
127         debug "File ratio: $file_ratio"
128         debug "Disk saturation $disk_saturation"
129         debug "Disk alloc method $disk_alloc_method"
130         debug "Test iteration count: $test_iteration_cnt"
131         debug "Extra arg: $extra_args"
132
133         for i in $(eval echo "{1..$test_iteration_cnt}"); do
134                 # Setup the device
135                 _scratch_mkfs_sized $FS_SIZE >> $seqres.full 2>&1
136                 _scratch_mount
137
138                 debug "===== Test: $test_name iteration: $i starts ====="
139                 thread_cnt=$(calc_thread_cnt $file_ratio_unit $file_ratio $disk_saturation)
140
141                 # Start the test
142                 $here/src/t_enospc -t $thread_cnt -s $file_ratio_unit -r $file_ratio -p $SCRATCH_MNT $extra_args >> $seqres.full
143
144                 status=$(echo $?)
145                 if [ $status -ne 0 ]; then
146                         use_per=$($DF_PROG -h | grep $SCRATCH_MNT | awk '{print substr($6, 1, length($6)-1)}' | $BC_PROG)
147                         alloc_per=$(echo "$FACT * 100" | $BC_PROG)
148                         # We are here since t_enospc failed with an error code.
149                         # If the used filesystem space is still < available space - that means
150                         # the test failed due to FS wrongly reported ENOSPC.
151                         if [ $(echo "$use_per < $alloc_per" | $BC_PROG) -ne 0 ]; then
152                                 if [ $status -eq 134 ]; then
153                                         # SIGABRT asserted exit code = 134
154                                         echo "FAIL: Aborted assertion faliure"
155                                 elif [ $status -eq 7 ]; then
156                                         # SIGBUS asserted exit code = 7
157                                         echo "FAIL: ENOSPC BUS faliure"
158                                 fi
159                                 echo "$test_name failed at iteration count: $i"
160                                 echo "$($DF_PROG -h $SCRATCH_MNT)"
161                                 echo "Allocated: $alloc_per% Used: $use_per%"
162                                 exit
163                         fi
164                 fi
165
166                 # Make space for other tests
167                 _scratch_unmount
168
169                 debug "===== Test: $test_name iteration: $i ends ====="
170         done
171         debug "============ Test details end ============="
172 }
173
174 declare -a TEST_VECTORS=(
175 # test-name:file-ratio-unit:file-ratio:disk-saturation:disk-alloc-method:test-iteration-cnt
176 "Small-file-fallocate-test:$SMALL_FILE_SIZE:1:$FACT:$FALLOCATE:3"
177 "Big-file-fallocate-test:$BIG_FILE_SIZE:1:$FACT:$FALLOCATE:3"
178 "Mix-file-fallocate-test:$MIX_FILE_SIZE:0.75,0.25:$FACT:$FALLOCATE:3"
179 "Small-file-ftruncate-test:$SMALL_FILE_SIZE:1:$FACT:$FTRUNCATE:3"
180 "Big-file-ftruncate-test:$BIG_FILE_SIZE:1:$FACT:$FTRUNCATE:3"
181 "Mix-file-ftruncate-test:$MIX_FILE_SIZE:0.75,0.25:$FACT:$FTRUNCATE:3"
182 )
183
184 # real QA test starts here
185 for i in "${TEST_VECTORS[@]}"; do
186         run_testcase $i
187 done
188
189 echo "Silence is golden"
190 status=0
191 exit