Debian Codegraph @

tests/blockwise-compat-test

raw symbols SCIP
1 #!/bin/bash
2
3 # set _FORCE_LOCAL environment variable to run blockwise unit tests even on local
4 # nfs. Some tests will fail because nfs is eager to write for example 4095 bytes
5 # in O_DIRECT mode.
6
7 BW_UNIT=./unit-utils-io
8 STRACE=strace
9 MNT_DIR=./mnt_bwunit
10 LOCAL_FILE=./blockwise_localfile
11
12 # $1 path to scsi debug bdev
13 scsi_debug_teardown() {
14 local _tries=15;
15
16 while [ -b "$1" -a $_tries -gt 0 ]; do
17 rmmod scsi_debug >/dev/null 2>&1
18 if [ -b "$1" ]; then
19 sleep .1
20 _tries=$((_tries-1))
21 fi
22 done
23
24 test ! -b "$1" || rmmod scsi_debug >/dev/null 2>&1
25 }
26
27 cleanup() {
28 if [ -d "$MNT_DIR" ] ; then
29 umount -f $MNT_DIR 2>/dev/null
30 rmdir $MNT_DIR 2>/dev/null
31 fi
32 rm -f $LOCAL_FILE 2> /dev/null
33 scsi_debug_teardown "$DEV" || exit 100
34 }
35
36 fail()
37 {
38 if [ -n "$1" ] ; then echo "FAIL $1" ; else echo "FAIL" ; fi
39 cleanup
40 exit 100
41 }
42
43 _sigchld() { local c=$?; [ $c -eq 139 ] && fail "Segfault"; [ $c -eq 134 ] && fail "Aborted"; }
44 trap _sigchld CHLD
45
46 fail_count()
47 {
48 echo "$MSG[FAIL]"
49 FAILS=$((FAILS+1))
50 }
51
52 warn_count()
53 {
54 echo "$MSG[WARNING]"
55 WARNS=$((WARNS+1))
56 }
57
58 skip()
59 {
60 echo "TEST SKIPPED: $1"
61 cleanup
62 exit 77
63 }
64
65 add_device() {
66 rmmod scsi_debug >/dev/null 2>&1
67 if [ -d /sys/module/scsi_debug ] ; then
68 skip "Cannot use scsi_debug module (in use or compiled-in)."
69 fi
70 modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
71 if [ $? -ne 0 ] ; then
72 skip "This kernel seems to not support proper scsi_debug module."
73 fi
74 sleep 1
75 DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
76 DEV="/dev/$DEV"
77 [ -b $DEV ] || fail "Cannot find $DEV."
78 }
79
80 falloc() {
81 dd if=/dev/zero of=$2 bs=1M count=$1 2> /dev/null
82 }
83
84 run_all_in_fs() {
85 for file in $(ls img_fs_*.img.xz) ; do
86 echo -n "Run tests in $file put on top block device. "
87 xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
88 [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
89 mount $DEV $MNT_DIR 2>/dev/null
90 if [ $? -ne 0 ]; then
91 echo "[N/A]"
92 continue;
93 fi
94 rm -rf $MNT_DIR/* 2>/dev/null
95 local tfile=$MNT_DIR/bwunit_tstfile
96 falloc $DEVSIZEMB $tfile || fail "enospc?"
97 local iobsize=$(stat -c "%o" $tfile)
98 test -n "$iobsize" -a $iobsize -gt 0 || fail
99 local oldbsize=$BSIZE
100 BSIZE=$iobsize
101 run_all $tfile
102 BSIZE=$oldbsize
103 umount $MNT_DIR || fail
104 echo "[OK]"
105 done
106 }
107
108 trunc_file() {
109 test $1 -eq 0 || truncate -c -s $1 $2 2>/dev/null || dd if=/dev/zero of=$2 bs=$1 count=1 2>/dev/null || fail "Failed to truncate test file $2."
110 }
111
112 RUN() {
113 local _res=$1
114 shift
115 local _dev=$1
116 shift
117 local _fn=$1
118 shift
119 local _type="bdev"
120 local _fsize=0
121
122 test -b $_dev || {
123 _type="file"
124 _fsize=$(stat -c "%s" $_dev)
125 }
126
127 case "$_res" in
128 P)
129 MSG="Testing $_fn on $_type with params $@ [expecting TRUE]..."
130 $BW_UNIT $_dev $_fn $@
131 if [ $? -ne 0 ]; then
132 if [ $_type = "file" ]; then
133 warn_count
134 else
135 fail_count
136 fi
137 trunc_file $_fsize $_dev
138 test -z "$STRACE" || $STRACE -o ./$BW_UNIT-fail-$FAILS-should-pass.log $BW_UNIT $_dev $_fn $@ 2> /dev/null
139 else
140 MSG="$MSG[OK]"
141 fi
142 ;;
143 F)
144 MSG="Testing $_fn on $_type with params $@ [expecting FALSE]..."
145 $BW_UNIT $_dev $_fn $@ 2> /dev/null
146 if [ $? -eq 0 ]; then
147 if [ $_type = "file" ]; then
148 warn_count
149 else
150 fail_count
151 fi
152 trunc_file $_fsize $_dev
153 test -z "$STRACE" || $STRACE -o ./$BW_UNIT-fail-$FAILS-should-fail.log $BW_UNIT $_dev $_fn $@ 2> /dev/null
154 else
155 MSG="$MSG[OK]"
156 fi
157 ;;
158 *)
159 fail "Internal test error"
160 ;;
161 esac
162
163 trunc_file $_fsize $_dev
164 }
165
166 run_all() {
167 if [ -b "$1" ]; then
168 BD_FAIL="F"
169 else
170 BD_FAIL="P"
171 fi
172
173 # buffer io support only blocksize aligned ios
174 # device/file fn_name length
175 RUN "P" $1 read_buffer $BSIZE
176 RUN "P" $1 read_buffer $((2*BSIZE))
177 RUN "F" $1 read_buffer $((BSIZE-1))
178 RUN "F" $1 read_buffer $((BSIZE+1))
179 RUN "P" $1 read_buffer 0
180
181 RUN "P" $1 write_buffer $BSIZE
182 RUN "P" $1 write_buffer $((2*BSIZE))
183
184 RUN "F" $1 write_buffer $((BSIZE-1))
185 RUN "F" $1 write_buffer $((BSIZE+1))
186 RUN "F" $1 write_buffer 0
187
188 # basic blockwise functions
189 # device/file fn_name length bsize
190 RUN "P" $1 read_blockwise 0 $BSIZE
191 RUN "P" $1 read_blockwise $((BSIZE)) $BSIZE
192 RUN "P" $1 read_blockwise $((BSIZE-1)) $BSIZE
193 RUN "P" $1 read_blockwise $((BSIZE+1)) $BSIZE
194 RUN "P" $1 read_blockwise $((DEVSIZE)) $BSIZE
195 RUN "P" $1 read_blockwise $((DEVSIZE-1)) $BSIZE
196 RUN "F" $1 read_blockwise $((DEVSIZE+1)) $BSIZE
197
198 RUN "P" $1 write_blockwise 0 $BSIZE
199 RUN "P" $1 write_blockwise $((BSIZE)) $BSIZE
200 RUN "P" $1 write_blockwise $((BSIZE-1)) $BSIZE
201 RUN "P" $1 write_blockwise $((BSIZE+1)) $BSIZE
202 RUN "P" $1 write_blockwise $((DEVSIZE)) $BSIZE
203 RUN "P" $1 write_blockwise $((DEVSIZE-1)) $BSIZE
204 RUN "$BD_FAIL" $1 write_blockwise $((DEVSIZE+1)) $BSIZE
205
206 # seek variant blockwise functions
207 # device/file fn_name length bsize offset
208 RUN "P" $1 read_lseek_blockwise 0 $BSIZE 0
209 RUN "P" $1 read_lseek_blockwise 0 $BSIZE 1
210 RUN "P" $1 read_lseek_blockwise 0 $BSIZE $((DEVSIZE))
211 # length = 0 is significant here
212 RUN "P" $1 read_lseek_blockwise 0 $BSIZE $((DEVSIZE+1))
213
214 # beginning of device
215 RUN "P" $1 read_lseek_blockwise 1 $BSIZE 0
216 RUN "P" $1 read_lseek_blockwise 1 $BSIZE 1
217 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE-1))
218 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE/2))
219
220 # somewhere in the 'middle'
221 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $BSIZE
222 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE+1))
223 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((2*BSIZE-1))
224 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE+BSIZE/2-1))
225
226 # cross-sector tests
227 RUN "P" $1 read_lseek_blockwise 2 $BSIZE $((BSIZE-1))
228 RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
229 RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
230 RUN "P" $1 read_lseek_blockwise 2 $BSIZE $((2*BSIZE-1))
231 RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
232 RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((2*BSIZE-1))
233
234 # including one whole sector
235 RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE))
236 RUN "P" $1 read_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
237 RUN "P" $1 read_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE-1))
238 RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
239 RUN "P" $1 read_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
240 RUN "P" $1 read_lseek_blockwise $((3*BSIZE-2)) $BSIZE $((BSIZE+1))
241
242 # hitting exactly the sector boundary
243 RUN "P" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE 1
244 RUN "P" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE $((BSIZE+1))
245 RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
246 RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
247
248 # device end
249 RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((DEVSIZE-1))
250 RUN "P" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+1))
251 RUN "P" $1 read_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE))
252 RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE-1))
253
254 # this must fail on both device and file
255 RUN "F" $1 read_lseek_blockwise 1 $BSIZE $((DEVSIZE))
256 RUN "F" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+2))
257 RUN "F" $1 read_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE+1))
258 RUN "F" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE))
259
260 RUN "P" $1 write_lseek_blockwise 0 $BSIZE 0
261 # TODO: this may pass but must not write a byte (write(0) is undefined).
262 # Test it with underlying dm-error or phony read/write syscalls.
263 # Skipping read is optimization.
264 # HINT: currently it performs useless write and read as well
265 RUN "P" $1 write_lseek_blockwise 0 $BSIZE 1
266 RUN "P" $1 write_lseek_blockwise 0 $BSIZE $BSIZE
267
268 # beginning of device
269 RUN "P" $1 write_lseek_blockwise 1 $BSIZE 0
270 RUN "P" $1 write_lseek_blockwise 1 $BSIZE 1
271 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE-1))
272 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE/2))
273
274 # somewhere in the 'middle'
275 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $BSIZE
276 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE+1))
277 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((2*BSIZE-1))
278 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE+BSIZE/2-1))
279
280 # cross-sector tests
281 RUN "P" $1 write_lseek_blockwise 2 $BSIZE $((BSIZE-1))
282 RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
283 RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
284 RUN "P" $1 write_lseek_blockwise 2 $BSIZE $((2*BSIZE-1))
285 RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
286 RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((2*BSIZE-1))
287
288 # including one whole sector
289 RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE))
290 RUN "P" $1 write_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
291 RUN "P" $1 write_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE-1))
292 RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
293 RUN "P" $1 write_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
294 RUN "P" $1 write_lseek_blockwise $((3*BSIZE-2)) $BSIZE $((BSIZE+1))
295
296 # hitting exactly the sector boundary
297 RUN "P" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE 1
298 RUN "P" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE $((BSIZE+1))
299 RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
300 RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
301
302 # device end
303 RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((DEVSIZE-1))
304 RUN "P" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+1))
305 RUN "P" $1 write_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE))
306 RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE-1))
307
308 # this must fail on device, but pass on file (which is unfortunate and maybe design mistake)
309 RUN "$BD_FAIL" $1 write_lseek_blockwise 1 $BSIZE $((DEVSIZE))
310 RUN "$BD_FAIL" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+2))
311 RUN "$BD_FAIL" $1 write_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE+1))
312 RUN "$BD_FAIL" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE))
313 }
314
315 command -v $STRACE >/dev/null || unset STRACE
316 test -x $BW_UNIT || skip "Run \"make `basename $BW_UNIT`\" first"
317
318 FAILS=0
319 WARNS=0
320 DEVSIZEMB=2
321 DEVSIZE=$((DEVSIZEMB*1024*1024))
322
323 PAGE_SIZE=$(getconf PAGE_SIZE)
324 echo "System PAGE_SIZE=$PAGE_SIZE"
325
326 echo "Run tests in local filesystem"
327 falloc $DEVSIZEMB $LOCAL_FILE || fail "Failed to create file in local filesystem."
328 BSIZE=$(stat -c "%o" $LOCAL_FILE)
329 if [ $BSIZE -gt $((512*1024)) ]; then
330 echo "Detected file block size: $BSIZE bytes"
331 echo "Tuning it down to system page size ($PAGE_SIZE bytes)"
332 BSIZE=$PAGE_SIZE
333 fi
334 run_all $LOCAL_FILE
335
336 [ $(id -u) -eq 0 ] || {
337 echo "WARNING: You must be root to run remaining tests."
338 test $FAILS -eq 0 || fail "($FAILS wrong result(s) in total)"
339 cleanup
340 exit 0
341 }
342
343 DEVBSIZE=512
344 BSIZE=$DEVBSIZE
345 EXP=0
346 DEVSIZEMBIMG=32
347
348 echo "# Create classic 512B drive"
349 echo "# (logical_block_size=$DEVBSIZE, physical_block_size=$((DEVBSIZE*(1<<EXP))))"
350 add_device dev_size_mb=$DEVSIZEMB sector_size=$DEVBSIZE physblk_exp=$EXP num_tgts=1
351 run_all $DEV
352 cleanup
353 add_device dev_size_mb=$DEVSIZEMBIMG sector_size=$DEVBSIZE physblk_exp=$EXP num_tgts=1
354 run_all_in_fs
355 cleanup
356
357 EXP=3
358 echo "# Create desktop-class 4K drive"
359 echo "# (logical_block_size=$DEVBSIZE, physical_block_size=$((DEVBSIZE*(1<<EXP))))"
360 add_device dev_size_mb=$DEVSIZEMB physblk_exp=$EXP sector_size=$DEVBSIZE num_tgts=1
361 run_all $DEV
362 BSIZE=$((DEVBSIZE*(1<<EXP)))
363 run_all $DEV
364 cleanup
365
366 add_device dev_size_mb=$DEVSIZEMBIMG physblk_exp=$EXP sector_size=$DEVBSIZE num_tgts=1
367 run_all_in_fs
368 cleanup
369
370 DEVBSIZE=4096
371 BSIZE=$DEVBSIZE
372 EXP=0
373 echo "# Create enterprise-class 4K drive"
374 echo "# (logical_block_size=$DEVBSIZE, physical_block_size=$((DEVBSIZE*(1<<EXP))))"
375 add_device dev_size_mb=$DEVSIZEMB physblk_exp=$EXP sector_size=$DEVBSIZE num_tgts=1
376 run_all $DEV
377 cleanup
378 add_device dev_size_mb=$DEVSIZEMBIMG sector_size=$DEVBSIZE physblk_exp=$EXP num_tgts=1
379 run_all_in_fs
380 cleanup
381
382 test $WARNS -eq 0 || echo "(WARNING: $WARNS suspicious result(s) in total)"
383 test $FAILS -eq 0 || fail "($FAILS wrong result(s) in total)"