fa6a279ba1fd9307e683ddd2c1d4c438eee03909
[fio.git] / t / zbd / test-zbd-support
1 #!/bin/bash
2 #
3 # Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 #
5 # This file is released under the GPL.
6
7 usage() {
8         echo "Usage: $(basename "$0") [OPTIONS] <test target device file>"
9         echo "Options:"
10         echo -e "\t-d Run fio with valgrind using DRD tool"
11         echo -e "\t-e Run fio with valgrind using helgrind tool"
12         echo -e "\t-v Run fio with valgrind --read-var-info option"
13         echo -e "\t-l Test with libzbc ioengine"
14         echo -e "\t-r Reset all zones before test start"
15         echo -e "\t-o <max_open_zones> Run fio with max_open_zones limit"
16         echo -e "\t-t <test #> Run only a single test case with specified number"
17         echo -e "\t-z Run fio with debug=zbd option"
18 }
19
20 max() {
21     if [ "$1" -gt "$2" ]; then
22         echo "$1"
23     else
24         echo "$2"
25     fi
26 }
27
28 min() {
29     if [ "$1" -lt "$2" ]; then
30         echo "$1"
31     else
32         echo "$2"
33     fi
34 }
35
36 ioengine() {
37         if [ -n "$use_libzbc" ]; then
38                 echo -n "--ioengine=libzbc"
39         else
40                 echo -n "--ioengine=$1"
41         fi
42 }
43
44 set_io_scheduler() {
45     local dev=$1 sched=$2
46
47     [ -e "/sys/block/$dev" ] || return $?
48     if [ -e "/sys/block/$dev/mq" ]; then
49         case "$sched" in
50             noop)        sched=none;;
51             deadline)    sched=mq-deadline;;
52         esac
53     else
54         case "$sched" in
55             none)        sched=noop;;
56             mq-deadline) sched=deadline;;
57         esac
58     fi
59
60     echo "$sched" >"/sys/block/$dev/queue/scheduler"
61 }
62
63 check_read() {
64     local read
65
66     read=$(fio_read <"${logfile}.${test_number}")
67     echo "read: $read <> $1" >> "${logfile}.${test_number}"
68     [ "$read" = "$1" ]
69 }
70
71 check_written() {
72     local written
73
74     written=$(fio_written <"${logfile}.${test_number}")
75     echo "written: $written <> $1" >> "${logfile}.${test_number}"
76     [ "$written" = "$1" ]
77 }
78
79 # Compare the reset count from the log file with reset count $2 using operator
80 # $1 (=, -ge, -gt, -le, -lt).
81 check_reset_count() {
82     local reset_count
83
84     reset_count=$(fio_reset_count <"${logfile}.${test_number}")
85     echo "reset_count: test $reset_count $1 $2" >> "${logfile}.${test_number}"
86     eval "[ '$reset_count' '$1' '$2' ]"
87 }
88
89 # Check log for failed assertions and crashes. Without these checks,
90 # a test can succeed even when these events happen, but it must fail.
91 check_log() {
92      [ ! -f "${logfile}.${1}" ] && return 0
93      ! grep -q -e "Assertion " -e "Aborted " "${logfile}.${1}"
94 }
95
96 # Whether or not $1 (/dev/...) is a SCSI device.
97 is_scsi_device() {
98     local d f
99
100     d=$(basename "$dev")
101     for f in /sys/class/scsi_device/*/device/block/"$d"; do
102         [ -e "$f" ] && return 0
103     done
104     return 1
105 }
106
107 job_var_opts_exclude() {
108         local o
109         local ex_key="${1}"
110
111         for o in "${job_var_opts[@]}"; do
112                 if [[ ${o} =~ "${ex_key}" ]]; then
113                         continue
114                 fi
115                 echo -n "${o}"
116         done
117 }
118
119 has_max_open_zones() {
120         while (($# > 1)); do
121                 if [[ ${1} =~ "--max_open_zones" ]]; then
122                         return 0
123                 fi
124                 shift
125         done
126         return 1
127 }
128
129 run_fio() {
130     local fio opts
131
132     fio=$(dirname "$0")/../../fio
133
134     opts=(${global_var_opts[@]})
135     opts+=("--max-jobs=16" "--aux-path=/tmp" "--allow_file_create=0" \
136                            "--significant_figures=10" "$@")
137     # When max_open_zones option is specified to this test script, add
138     # max_open_zones option to fio command unless the test case already add it.
139     if [[ -n ${max_open_zones_opt} ]] && ! has_max_open_zones "${opts[@]}"; then
140             opts+=("--max_open_zones=${max_open_zones_opt}")
141     fi
142     { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"
143
144     "${dynamic_analyzer[@]}" "$fio" "${opts[@]}"
145 }
146
147 run_one_fio_job() {
148     local r
149
150     r=$(((RANDOM << 16) | RANDOM))
151     run_fio --name="$dev" --filename="$dev" "$@" --randseed="$r"        \
152             --thread=1 --direct=1
153 }
154
155 write_and_run_one_fio_job() {
156     local r
157     local write_offset="${1}"
158     local write_size="${2}"
159     local -a write_opts
160
161     shift 2
162     r=$(((RANDOM << 16) | RANDOM))
163     write_opts=(--name="write_job" --rw=write "$(ioengine "psync")" \
164                       --bs="${logical_block_size}" --zonemode=zbd \
165                       --zonesize="${zone_size}" --thread=1 --direct=1 \
166                       --offset="${write_offset}" --size="${write_size}")
167     write_opts+=("${job_var_opts[@]}")
168     run_fio --filename="$dev" --randseed="$r" "${write_opts[@]}" \
169             --name="$dev" --wait_for="write_job" "$@" --thread=1 --direct=1
170 }
171
172 # Run fio on the first four sequential zones of the disk.
173 run_fio_on_seq() {
174     local opts=()
175
176     opts+=("--offset=$((first_sequential_zone_sector * 512))")
177     opts+=("--size=$((4 * zone_size))" "--zonemode=zbd")
178     if [ -z "$is_zbd" ]; then
179         opts+=("--zonesize=${zone_size}")
180     fi
181     run_one_fio_job "${opts[@]}" "$@"
182 }
183
184 # Prepare for write test by resetting zones. When max_open_zones option is
185 # specified, reset all zones of the test target to ensure that zones out of the
186 # test target range do not have open zones. This allows the write test to the
187 # target range to be able to open zones up to max_open_zones.
188 prep_write() {
189         [[ -n "${max_open_zones_opt}" && -n "${is_zbd}" ]] &&
190                 reset_zone "${dev}" -1
191 }
192
193 SKIP_TESTCASE=255
194
195 require_scsi_dev() {
196         if ! is_scsi_device "$dev"; then
197                 SKIP_REASON="$dev is not a SCSI device"
198                 return 1
199         fi
200         return 0
201 }
202
203 require_conv_zone_bytes() {
204         local req_bytes=${1}
205
206         if ((req_bytes > first_sequential_zone_sector * 512)); then
207                 SKIP_REASON="$dev does not have enough conventional zones"
208                 return 1
209         fi
210         return 0
211 }
212
213 require_zbd() {
214         if [[ -z ${is_zbd} ]]; then
215                 SKIP_REASON="$dev is not a zoned block device"
216                 return 1
217         fi
218         return 0
219 }
220
221 require_regular_block_dev() {
222         if [[ -n ${is_zbd} ]]; then
223                 SKIP_REASON="$dev is not a regular block device"
224                 return 1
225         fi
226         return 0
227 }
228
229 require_seq_zones() {
230         local req_seq_zones=${1}
231         local seq_bytes=$((disk_size - first_sequential_zone_sector * 512))
232
233         if ((req_seq_zones > seq_bytes / zone_size)); then
234                 SKIP_REASON="$dev does not have $req_seq_zones sequential zones"
235                 return 1
236         fi
237         return 0
238 }
239
240 # Check whether buffered writes are refused.
241 test1() {
242     run_fio --name=job1 --filename="$dev" --rw=write --direct=0 --bs=4K \
243             "$(ioengine "psync")" --size="${zone_size}" --thread=1      \
244             --zonemode=zbd --zonesize="${zone_size}" 2>&1 |
245         tee -a "${logfile}.${test_number}" |
246         grep -q 'Using direct I/O is mandatory for writing to ZBD drives'
247     local fio_rc=${PIPESTATUS[0]} grep_rc=${PIPESTATUS[2]}
248     case "$fio_rc" in
249         0|1) ;;
250         *)   return "$fio_rc"
251     esac
252     if [ -n "$is_zbd" ]; then
253         [ "$grep_rc" = 0 ]
254     else
255         [ "$grep_rc" != 0 ]
256     fi
257 }
258
259 # Block size exceeds zone size.
260 test2() {
261     local bs off opts=() rc
262
263     off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
264     bs=$((2 * zone_size))
265     opts+=("$(ioengine "psync")")
266     opts+=("--name=job1" "--filename=$dev" "--rw=write" "--direct=1")
267     opts+=("--zonemode=zbd" "--offset=$off" "--bs=$bs" "--size=$bs")
268     if [ -z "$is_zbd" ]; then
269         opts+=("--zonesize=${zone_size}")
270     fi
271     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 && return 1
272     grep -q 'buflen exceeds zone size' "${logfile}.${test_number}"
273 }
274
275 # Run fio against an empty zone. This causes fio to report "No I/O performed".
276 test3() {
277     local off opts=() rc
278
279     require_seq_zones 129 || return $SKIP_TESTCASE
280     off=$((first_sequential_zone_sector * 512 + 128 * zone_size))
281     size=$((zone_size))
282     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
283     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--bs=4K")
284     opts+=("--size=$size" "--zonemode=zbd")
285     opts+=("$(ioengine "psync")" "--rw=read" "--direct=1" "--thread=1")
286     if [ -z "$is_zbd" ]; then
287         opts+=("--zonesize=${zone_size}")
288     fi
289     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
290     ! grep -q 'READ:' "${logfile}.${test_number}"
291 }
292
293 # Run fio with --read_beyond_wp=1 against an empty zone.
294 test4() {
295     local off opts=()
296
297     require_seq_zones 130 || return $SKIP_TESTCASE
298     off=$((first_sequential_zone_sector * 512 + 129 * zone_size))
299     size=$((zone_size))
300     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
301     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--bs=$size")
302     opts+=("--size=$size" "--thread=1" "--read_beyond_wp=1")
303     opts+=("$(ioengine "psync")" "--rw=read" "--direct=1" "--disable_lat=1")
304     opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
305     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
306     check_read $size || return $?
307 }
308
309 # Sequential write to sequential zones.
310 test5() {
311     local size off capacity
312
313     prep_write
314     off=$((first_sequential_zone_sector * 512))
315     capacity=$(total_zone_capacity 4 $off $dev)
316     size=$((4 * zone_size))
317     run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write \
318                    --bs="$(max $((zone_size / 64)) "$logical_block_size")"\
319                    --do_verify=1 --verify=md5                           \
320                    >>"${logfile}.${test_number}" 2>&1 || return $?
321     check_written $capacity || return $?
322     check_read $capacity || return $?
323 }
324
325 # Sequential read from sequential zones.
326 test6() {
327     local size off capacity
328
329     prep_write
330     off=$((first_sequential_zone_sector * 512))
331     capacity=$(total_zone_capacity 4 $off $dev)
332     size=$((4 * zone_size))
333     write_and_run_one_fio_job \
334             $((first_sequential_zone_sector * 512)) "${size}" \
335             --offset="${off}" \
336             --size="${size}" --zonemode=zbd --zonesize="${zone_size}" \
337             "$(ioengine "psync")" --iodepth=1 --rw=read \
338             --bs="$(max $((zone_size / 64)) "$logical_block_size")" \
339             >>"${logfile}.${test_number}" 2>&1 || return $?
340     check_read $capacity || return $?
341 }
342
343 # Random write to sequential zones, libaio, queue depth 1.
344 test7() {
345     local size=$((zone_size))
346     local off capacity
347
348     prep_write
349     off=$((first_sequential_zone_sector * 512))
350     capacity=$(total_zone_capacity 1 $off $dev)
351     run_fio_on_seq "$(ioengine "libaio")" --iodepth=1 --rw=randwrite    \
352                    --bs="$(min 16384 "${zone_size}")"                   \
353                    --do_verify=1 --verify=md5 --size="$size"            \
354                    >>"${logfile}.${test_number}" 2>&1 || return $?
355     check_written $capacity || return $?
356     check_read $capacity || return $?
357 }
358
359 # Random write to sequential zones, libaio, queue depth 64.
360 test8() {
361     local size off capacity
362
363     prep_write
364     size=$((4 * zone_size))
365     off=$((first_sequential_zone_sector * 512))
366     capacity=$(total_zone_capacity 4 $off $dev)
367     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite   \
368                    --bs="$(min 16384 "${zone_size}")"                   \
369                    --do_verify=1 --verify=md5                           \
370                    >>"${logfile}.${test_number}" 2>&1 || return $?
371     check_written $capacity || return $?
372     check_read $capacity || return $?
373 }
374
375 # Random write to sequential zones, sg, queue depth 1.
376 test9() {
377     local size
378
379     require_scsi_dev || return $SKIP_TESTCASE
380
381     prep_write
382     size=$((4 * zone_size))
383     run_fio_on_seq --ioengine=sg                                        \
384                    --iodepth=1 --rw=randwrite --bs=16K                  \
385                    --do_verify=1 --verify=md5                           \
386                    >>"${logfile}.${test_number}" 2>&1 || return $?
387     check_written $size || return $?
388     check_read $size || return $?
389 }
390
391 # Random write to sequential zones, sg, queue depth 64.
392 test10() {
393     local size
394
395     require_scsi_dev || return $SKIP_TESTCASE
396
397     prep_write
398     size=$((4 * zone_size))
399     run_fio_on_seq --ioengine=sg                                        \
400                    --iodepth=64 --rw=randwrite --bs=16K                 \
401                    --do_verify=1 --verify=md5                           \
402                    >>"${logfile}.${test_number}" 2>&1 || return $?
403     check_written $size || return $?
404     check_read $size || return $?
405 }
406
407 # Random write to sequential zones, libaio, queue depth 64, random block size.
408 test11() {
409     local size off capacity
410
411     prep_write
412     size=$((4 * zone_size))
413     off=$((first_sequential_zone_sector * 512))
414     capacity=$(total_zone_capacity 4 $off $dev)
415     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite   \
416                    --bsrange=4K-64K --do_verify=1 --verify=md5          \
417                    --debug=zbd >>"${logfile}.${test_number}" 2>&1 || return $?
418     check_written $capacity || return $?
419     check_read $capacity || return $?
420 }
421
422 # Random write to sequential zones, libaio, queue depth 64, max 1 open zone.
423 test12() {
424     local size off capacity
425
426     prep_write
427     size=$((8 * zone_size))
428     off=$((first_sequential_zone_sector * 512))
429     capacity=$(total_zone_capacity 8 $off $dev)
430     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
431                    --max_open_zones=1 --size=$size --do_verify=1 --verify=md5 \
432                    --debug=zbd >>"${logfile}.${test_number}" 2>&1 || return $?
433     check_written $capacity || return $?
434     check_read $capacity || return $?
435 }
436
437 # Random write to sequential zones, libaio, queue depth 64, max 4 open zones.
438 test13() {
439     local size off capacity
440
441     prep_write
442     size=$((8 * zone_size))
443     off=$((first_sequential_zone_sector * 512))
444     capacity=$(total_zone_capacity 8 $off $dev)
445     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
446                    --max_open_zones=4 --size=$size --do_verify=1 --verify=md5 \
447                    --debug=zbd                                                \
448                    >>"${logfile}.${test_number}" 2>&1 || return $?
449     check_written $capacity || return $?
450     check_read $capacity || return $?
451 }
452
453 # Random write to conventional zones.
454 test14() {
455     local size
456
457     prep_write
458     size=$((16 * 2**20)) # 20 MB
459     require_conv_zone_bytes "${size}" || return $SKIP_TESTCASE
460
461     run_one_fio_job "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
462                     --zonemode=zbd --zonesize="${zone_size}" --do_verify=1 \
463                     --verify=md5 --size=$size                              \
464                     >>"${logfile}.${test_number}" 2>&1 || return $?
465     check_written $((size)) || return $?
466     check_read $((size)) || return $?
467 }
468
469 # Sequential read on a mix of empty and full zones.
470 test15() {
471     local i off size
472     local w_off w_size w_capacity
473
474     for ((i=0;i<4;i++)); do
475         [ -n "$is_zbd" ] &&
476             reset_zone "$dev" $((first_sequential_zone_sector +
477                                  i*sectors_per_zone))
478     done
479     prep_write
480     w_off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
481     w_size=$((2 * zone_size))
482     w_capacity=$(total_zone_capacity 2 $w_off $dev)
483     off=$((first_sequential_zone_sector * 512))
484     size=$((4 * zone_size))
485     write_and_run_one_fio_job "${w_off}" "${w_size}" \
486                     "$(ioengine "psync")" --rw=read --bs=$((zone_size / 16)) \
487                     --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
488                     --size=$((size)) >>"${logfile}.${test_number}" 2>&1 ||
489         return $?
490     check_written $((w_capacity)) || return $?
491     check_read $((w_capacity))
492 }
493
494 # Random read on a mix of empty and full zones.
495 test16() {
496     local off size
497     local i w_off w_size w_capacity
498
499     for ((i=0;i<4;i++)); do
500         [ -n "$is_zbd" ] &&
501             reset_zone "$dev" $((first_sequential_zone_sector +
502                                  i*sectors_per_zone))
503     done
504     prep_write
505     w_off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
506     w_size=$((2 * zone_size))
507     w_capacity=$(total_zone_capacity 2 $w_off $dev)
508     off=$((first_sequential_zone_sector * 512))
509     size=$((4 * zone_size))
510     write_and_run_one_fio_job "${w_off}" "${w_size}" \
511                     "$(ioengine "libaio")" --iodepth=64 --rw=randread --bs=16K \
512                     --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
513                     --size=$size >>"${logfile}.${test_number}" 2>&1 || return $?
514     check_written $w_capacity || return $?
515     check_read $size || return $?
516 }
517
518 # Random reads and writes in the last zone.
519 test17() {
520     local io off read size written
521
522     off=$(((disk_size / zone_size - 1) * zone_size))
523     size=$((disk_size - off))
524     if [ -n "$is_zbd" ]; then
525         reset_zone "$dev" $((off / 512)) || return $?
526     fi
527     prep_write
528     run_one_fio_job "$(ioengine "libaio")" --iodepth=8 --rw=randrw --bs=4K \
529                     --zonemode=zbd --zonesize="${zone_size}"            \
530                     --offset=$off --loops=2 --norandommap=1\
531                     >>"${logfile}.${test_number}" 2>&1 || return $?
532     written=$(fio_written <"${logfile}.${test_number}")
533     read=$(fio_read <"${logfile}.${test_number}")
534     io=$((written + read))
535     echo "Total number of bytes read and written: $io <> $size" \
536          >>"${logfile}.${test_number}"
537     [ $io = $((size * 2)) ];
538 }
539
540 # Out-of-range zone reset threshold and frequency parameters.
541 test18() {
542     run_fio_on_seq --zone_reset_threshold=-1 |&
543         tee -a "${logfile}.${test_number}"   |
544             grep -q 'value out of range' || return $?
545 }
546
547 test19() {
548     run_fio_on_seq --zone_reset_threshold=2  |&
549         tee -a "${logfile}.${test_number}"   |
550         grep -q 'value out of range' || return $?
551 }
552
553 test20() {
554     run_fio_on_seq --zone_reset_threshold=.4:.6 |&
555         tee -a "${logfile}.${test_number}"   |
556         grep -q 'the list exceeding max length' || return $?
557 }
558
559 test21() {
560     run_fio_on_seq --zone_reset_frequency=-1 |&
561         tee -a "${logfile}.${test_number}"   |
562         grep -q 'value out of range' || return $?
563 }
564
565 test22() {
566     run_fio_on_seq --zone_reset_frequency=2  |&
567         tee -a "${logfile}.${test_number}"   |
568         grep -q 'value out of range' || return $?
569 }
570
571 test23() {
572     run_fio_on_seq --zone_reset_frequency=.4:.6  |&
573         tee -a "${logfile}.${test_number}"   |
574         grep -q 'the list exceeding max length' || return $?
575 }
576
577 test24() {
578     local bs loops=9 size=$((zone_size))
579     local off capacity
580
581     prep_write
582     off=$((first_sequential_zone_sector * 512))
583     capacity=$(total_zone_capacity 1 $off $dev)
584
585     bs=$(min $((256*1024)) "$zone_size")
586     run_fio_on_seq "$(ioengine "psync")" --rw=write --bs="$bs"          \
587                    --size=$size --loops=$loops                          \
588                    --zone_reset_frequency=.01 --zone_reset_threshold=.90 \
589                    >> "${logfile}.${test_number}" 2>&1 || return $?
590     check_written $((capacity * loops)) || return $?
591     check_reset_count -eq 8 ||
592         check_reset_count -eq 9 ||
593         check_reset_count -eq 10 || return $?
594 }
595
596 # Multiple non-overlapping sequential write jobs for the same drive.
597 test25() {
598     local i opts=()
599
600     for ((i=0;i<16;i++)); do
601         [ -n "$is_zbd" ] &&
602             reset_zone "$dev" $((first_sequential_zone_sector + i*sectors_per_zone))
603     done
604     prep_write
605     for ((i=0;i<16;i++)); do
606         opts+=("--name=job$i" "--filename=$dev" "--thread=1" "--direct=1")
607         opts+=("--offset=$((first_sequential_zone_sector*512 + zone_size*i))")
608         opts+=("--size=$zone_size" "$(ioengine "psync")" "--rw=write" "--bs=16K")
609         opts+=("--zonemode=zbd" "--zonesize=${zone_size}" "--group_reporting=1")
610         opts+=(${job_var_opts[@]})
611     done
612     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
613 }
614
615 write_to_first_seq_zone() {
616     local loops=4 r
617     local off capacity
618
619     prep_write
620     off=$((first_sequential_zone_sector * 512))
621     capacity=$(total_zone_capacity 1 $off $dev)
622
623     r=$(((RANDOM << 16) | RANDOM))
624     run_fio --name="$dev" --filename="$dev" "$(ioengine "psync")" --rw="$1" \
625             --thread=1 --do_verify=1 --verify=md5 --direct=1 --bs=4K    \
626             --offset=$off                                               \
627             --size=$zone_size --loops=$loops --randseed="$r"            \
628             --zonemode=zbd --zonesize="${zone_size}" --group_reporting=1        \
629             --gtod_reduce=1 >> "${logfile}.${test_number}" 2>&1 || return $?
630     check_written $((loops * capacity)) || return $?
631 }
632
633 # Overwrite the first sequential zone four times sequentially.
634 test26() {
635     write_to_first_seq_zone write
636 }
637
638 # Overwrite the first sequential zone four times using random writes.
639 test27() {
640     write_to_first_seq_zone randwrite
641 }
642
643 # Multiple overlapping random write jobs for the same drive.
644 test28() {
645     local i jobs=16 off opts
646
647     require_seq_zones 65 || return $SKIP_TESTCASE
648     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
649     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
650     prep_write
651     opts=("--debug=zbd")
652     capacity=$(total_zone_capacity 1 $off $dev)
653     for ((i=0;i<jobs;i++)); do
654         opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
655         opts+=("--size=$zone_size" "--io_size=$capacity" "$(ioengine "psync")" "--rw=randwrite")
656         opts+=("--thread=1" "--direct=1" "--zonemode=zbd")
657         opts+=("--zonesize=${zone_size}" "--group_reporting=1")
658         opts+=(${job_var_opts[@]})
659     done
660     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
661     check_written $((jobs * $capacity)) || return $?
662     check_reset_count -eq $jobs ||
663         check_reset_count -eq $((jobs - 1)) ||
664         return $?
665 }
666
667 # Multiple overlapping random write jobs for the same drive and with a limited
668 # number of open zones.
669 test29() {
670     local i jobs=16 off opts=()
671
672     require_seq_zones 80 || return $SKIP_TESTCASE
673     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
674     size=$((16*zone_size))
675     prep_write
676     opts=("--debug=zbd")
677     for ((i=0;i<jobs;i++)); do
678         opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
679         opts+=("--size=$size" "--io_size=$zone_size" "--thread=1")
680         opts+=("$(ioengine "psync")" "--rw=randwrite" "--direct=1")
681         opts+=("--max_open_zones=4" "--group_reporting=1")
682         opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
683         # max_open_zones is already specified
684         opts+=($(job_var_opts_exclude "--max_open_zones"))
685     done
686     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
687     check_written $((jobs * zone_size)) || return $?
688 }
689
690 # Random reads and writes across the entire disk for 30s.
691 test30() {
692     local off
693
694     prep_write
695     off=$((first_sequential_zone_sector * 512))
696     run_one_fio_job "$(ioengine "libaio")" --iodepth=8 --rw=randrw      \
697                     --bs="$(max $((zone_size / 128)) "$logical_block_size")"\
698                     --zonemode=zbd --zonesize="${zone_size}" --offset=$off\
699                     --loops=2 --time_based --runtime=30s --norandommap=1\
700                     >>"${logfile}.${test_number}" 2>&1
701 }
702
703 # Random reads across all sequential zones for 30s. This is not only a fio
704 # test but also allows to verify the performance of a drive.
705 test31() {
706     local bs inc nz off opts size
707
708     prep_write
709     # Start with writing 128 KB to 128 sequential zones.
710     bs=128K
711     nz=128
712     # shellcheck disable=SC2017
713     inc=$(((disk_size - (first_sequential_zone_sector * 512)) / (nz * zone_size)
714            * zone_size))
715     opts=()
716     for ((off = first_sequential_zone_sector * 512; off < disk_size;
717           off += inc)); do
718         opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--io_size=$bs")
719         opts+=("--bs=$bs" "--size=$zone_size" "$(ioengine "libaio")")
720         opts+=("--rw=write" "--direct=1" "--thread=1" "--stats=0")
721         opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
722         opts+=(${job_var_opts[@]})
723     done
724     "$(dirname "$0")/../../fio" "${opts[@]}" >> "${logfile}.${test_number}" 2>&1
725     # Next, run the test.
726     off=$((first_sequential_zone_sector * 512))
727     size=$((disk_size - off))
728     opts=("--name=$dev" "--filename=$dev" "--offset=$off" "--size=$size")
729     opts+=("--bs=$bs" "$(ioengine "psync")" "--rw=randread" "--direct=1")
730     opts+=("--thread=1" "--time_based" "--runtime=30" "--zonemode=zbd")
731     opts+=("--zonesize=${zone_size}")
732     opts+=(${job_var_opts[@]})
733     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
734 }
735
736 # Random writes across all sequential zones. This is not only a fio test but
737 # also allows to verify the performance of a drive.
738 test32() {
739     local off opts=() size
740
741     require_zbd || return $SKIP_TESTCASE
742
743     prep_write
744     off=$((first_sequential_zone_sector * 512))
745     size=$((disk_size - off))
746     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--size=$size")
747     opts+=("--bs=128K" "$(ioengine "psync")" "--rw=randwrite" "--direct=1")
748     opts+=("--thread=1" "--time_based" "--runtime=30")
749     opts+=("--max_open_zones=$max_open_zones" "--zonemode=zbd")
750     opts+=("--zonesize=${zone_size}")
751     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
752 }
753
754 # Write to sequential zones with a block size that is not a divisor of the
755 # zone size.
756 test33() {
757     local bs io_size size
758     local off capacity=0;
759
760     prep_write
761     off=$((first_sequential_zone_sector * 512))
762     capacity=$(total_zone_capacity 1 $off $dev)
763     size=$((2 * zone_size))
764     io_size=$((5 * capacity))
765     bs=$((3 * capacity / 4))
766     run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write \
767                    --size=$size --io_size=$io_size --bs=$bs     \
768                    >> "${logfile}.${test_number}" 2>&1 || return $?
769     check_written $(((io_size + bs - 1) / bs * bs)) || return $?
770 }
771
772 # Write to sequential zones with a block size that is not a divisor of the
773 # zone size and with data verification enabled.
774 test34() {
775     local size
776
777     prep_write
778     size=$((2 * zone_size))
779     run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write --size=$size \
780                    --do_verify=1 --verify=md5 --bs=$((3 * zone_size / 4)) \
781                    >> "${logfile}.${test_number}" 2>&1 && return 1
782     grep -q 'not a divisor of' "${logfile}.${test_number}"
783 }
784
785 # Test 1/4 for the I/O boundary rounding code: $size < $zone_size.
786 test35() {
787     local bs off io_size size
788
789     prep_write
790     off=$(((first_sequential_zone_sector + 1) * 512))
791     size=$((zone_size - 2 * 512))
792     bs=$((zone_size / 4))
793     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
794                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
795                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
796                     >> "${logfile}.${test_number}" 2>&1 && return 1
797     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
798 }
799
800 # Test 2/4 for the I/O boundary rounding code: $size < $zone_size.
801 test36() {
802     local bs off io_size size
803
804     prep_write
805     off=$(((first_sequential_zone_sector) * 512))
806     size=$((zone_size - 512))
807     bs=$((zone_size / 4))
808     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
809                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
810                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
811                     >> "${logfile}.${test_number}" 2>&1 && return 1
812     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
813 }
814
815 # Test 3/4 for the I/O boundary rounding code: $size > $zone_size.
816 test37() {
817     local bs off size capacity
818
819     prep_write
820     capacity=$(total_zone_capacity 1 $first_sequential_zone_sector $dev)
821     if [ "$first_sequential_zone_sector" = 0 ]; then
822         off=0
823     else
824         off=$(((first_sequential_zone_sector - 1) * 512))
825     fi
826     size=$((zone_size + 2 * 512))
827     bs=$((zone_size / 4))
828     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
829                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
830                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
831                     >> "${logfile}.${test_number}" 2>&1
832     check_written $capacity || return $?
833 }
834
835 # Test 4/4 for the I/O boundary rounding code: $offset > $disk_size - $zone_size
836 test38() {
837     local bs off size
838
839     prep_write
840     size=$((logical_block_size))
841     off=$((disk_size - logical_block_size))
842     bs=$((logical_block_size))
843     run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"    \
844                     --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
845                     --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
846                     >> "${logfile}.${test_number}" 2>&1 && return 1
847     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
848 }
849
850 # Read one block from a block device.
851 read_one_block() {
852     local bs
853
854     bs=$((logical_block_size))
855     run_one_fio_job --rw=read "$(ioengine "psync")" --bs=$bs --size=$bs "$@" 2>&1 |
856         tee -a "${logfile}.${test_number}"
857 }
858
859 # Check whether fio accepts --zonemode=none for zoned block devices.
860 test39() {
861     require_zbd || return $SKIP_TESTCASE
862     read_one_block --zonemode=none >/dev/null || return $?
863     check_read $((logical_block_size)) || return $?
864 }
865
866 # Check whether fio accepts --zonemode=strided for zoned block devices.
867 test40() {
868     local bs
869
870     bs=$((logical_block_size))
871     require_zbd || return $SKIP_TESTCASE
872     read_one_block --zonemode=strided |
873         grep -q 'fio: --zonesize must be specified when using --zonemode=strided' ||
874         return $?
875     read_one_block --zonemode=strided --zonesize=$bs >/dev/null || return $?
876     check_read $bs || return $?
877 }
878
879 # Check whether fio checks the zone size for zoned block devices.
880 test41() {
881     require_zbd || return $SKIP_TESTCASE
882     read_one_block --zonemode=zbd --zonesize=$((2 * zone_size)) |
883         grep -q 'job parameter zonesize.*does not match disk zone size'
884 }
885
886 # Check whether fio handles --zonesize=0 correctly for regular block devices.
887 test42() {
888     require_regular_block_dev || return $SKIP_TESTCASE
889     read_one_block --zonemode=zbd --zonesize=0 |
890         grep -q 'Specifying the zone size is mandatory for regular block devices with --zonemode=zbd'
891 }
892
893 # Check whether fio handles --zonesize=1 correctly for regular block devices.
894 test43() {
895     require_regular_block_dev || return $SKIP_TESTCASE
896     read_one_block --zonemode=zbd --zonesize=1 |
897         grep -q 'zone size must be at least 512 bytes for --zonemode=zbd'
898 }
899
900 # Check whether fio handles --zonemode=none --zonesize=1 correctly.
901 test44() {
902     read_one_block --zonemode=none --zonesize=1 |
903         grep -q 'fio: --zonemode=none and --zonesize are not compatible'
904 }
905
906 test45() {
907     local bs i
908
909     require_zbd || return $SKIP_TESTCASE
910     prep_write
911     bs=$((logical_block_size))
912     run_one_fio_job "$(ioengine "psync")" --iodepth=1 --rw=randwrite --bs=$bs\
913                     --offset=$((first_sequential_zone_sector * 512)) \
914                     --size="$zone_size" --do_verify=1 --verify=md5 2>&1 |
915         tee -a "${logfile}.${test_number}" |
916         grep -q "fio: first I/O failed. If .* is a zoned block device, consider --zonemode=zbd"
917 }
918
919 # Random write to sequential zones, libaio, 8 jobs, queue depth 64 per job
920 test46() {
921     local size
922
923     prep_write
924     size=$((4 * zone_size))
925     run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=4K \
926                    --group_reporting=1 --numjobs=8 \
927                    >> "${logfile}.${test_number}" 2>&1 || return $?
928     check_written $((size * 8)) || return $?
929 }
930
931 # Check whether fio handles --zonemode=zbd --zoneskip=1 correctly.
932 test47() {
933     local bs
934
935     prep_write
936     bs=$((logical_block_size))
937     run_fio_on_seq "$(ioengine "psync")" --rw=write --bs=$bs --zoneskip=1 \
938                     >> "${logfile}.${test_number}" 2>&1 && return 1
939     grep -q 'zoneskip 1 is not a multiple of the device zone size' "${logfile}.${test_number}"
940 }
941
942 # Multiple overlapping random write jobs for the same drive and with a
943 # limited number of open zones. This is similar to test29, but uses libaio
944 # to stress test zone locking.
945 test48() {
946     local i jobs=16 off opts=()
947
948     require_zbd || return $SKIP_TESTCASE
949     require_seq_zones 80 || return $SKIP_TESTCASE
950
951     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
952     size=$((16*zone_size))
953     prep_write
954     opts=("--aux-path=/tmp" "--allow_file_create=0" "--significant_figures=10")
955     opts+=("--debug=zbd")
956     opts+=("$(ioengine "libaio")" "--rw=randwrite" "--direct=1")
957     opts+=("--time_based" "--runtime=30")
958     opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
959     opts+=("--max_open_zones=4")
960     for ((i=0;i<jobs;i++)); do
961         opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
962         opts+=("--io_size=$zone_size" "--iodepth=256" "--thread=1")
963         opts+=("--size=$size" "--group_reporting=1")
964         # max_open_zones is already specified
965         opts+=($(job_var_opts_exclude "--max_open_zones"))
966     done
967
968     fio=$(dirname "$0")/../../fio
969
970     { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"
971
972     timeout -v -s KILL 45s \
973             "${dynamic_analyzer[@]}" "$fio" "${opts[@]}" \
974             >> "${logfile}.${test_number}" 2>&1 || return $?
975 }
976
977 # Check if fio handles --zonecapacity on a normal block device correctly
978 test49() {
979
980     require_regular_block_dev || return $SKIP_TESTCASE
981
982     size=$((2 * zone_size))
983     capacity=$((zone_size * 3 / 4))
984
985     run_one_fio_job "$(ioengine "psync")" --rw=write \
986                     --zonemode=zbd --zonesize="${zone_size}" \
987                     --zonecapacity=${capacity} \
988                     --verify=md5  --size=${size} >>"${logfile}.${test_number}" 2>&1 ||
989         return $?
990     check_written $((capacity * 2)) || return $?
991     check_read $((capacity * 2)) || return $?
992 }
993
994 tests=()
995 dynamic_analyzer=()
996 reset_all_zones=
997 use_libzbc=
998 zbd_debug=
999 max_open_zones_opt=
1000
1001 while [ "${1#-}" != "$1" ]; do
1002   case "$1" in
1003     -d) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=drd"
1004                           "--show-confl-seg=no");
1005         shift;;
1006     -e) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=helgrind");
1007         shift;;
1008     -l) use_libzbc=1; shift;;
1009     -r) reset_all_zones=1; shift;;
1010     -t) tests+=("$2"); shift; shift;;
1011     -o) max_open_zones_opt="${2}"; shift; shift;;
1012     -v) dynamic_analyzer=(valgrind "--read-var-info=yes");
1013         shift;;
1014     -z) zbd_debug=1; shift;;
1015     --) shift; break;;
1016   esac
1017 done
1018
1019 if [ $# != 1 ]; then
1020     usage
1021     exit 1
1022 fi
1023
1024 # shellcheck source=functions
1025 source "$(dirname "$0")/functions" || exit $?
1026
1027 global_var_opts=()
1028 job_var_opts=()
1029 if [ -n "$zbd_debug" ]; then
1030     global_var_opts+=("--debug=zbd")
1031 fi
1032 dev=$1
1033 realdev=$(readlink -f "$dev")
1034 basename=$(basename "$realdev")
1035
1036 if [[ -b "$realdev" ]]; then
1037         major=$((0x$(stat -L -c '%t' "$realdev"))) || exit $?
1038         minor=$((0x$(stat -L -c '%T' "$realdev"))) || exit $?
1039         disk_size=$(($(<"/sys/dev/block/$major:$minor/size")*512))
1040
1041         # When the target is a partition device, get basename of its
1042         # holder device to access sysfs path of the holder device
1043         if [[ -r "/sys/dev/block/$major:$minor/partition" ]]; then
1044                 realsysfs=$(readlink "/sys/dev/block/$major:$minor")
1045                 basename=$(basename "${realsysfs%/*}")
1046         fi
1047         logical_block_size=$(<"/sys/block/$basename/queue/logical_block_size")
1048         case "$(<"/sys/class/block/$basename/queue/zoned")" in
1049         host-managed|host-aware)
1050                 is_zbd=true
1051                 if ! check_blkzone "${dev}"; then
1052                         exit 1
1053                 fi
1054                 if ! result=($(first_sequential_zone "$dev")); then
1055                         echo "Failed to determine first sequential zone"
1056                         exit 1
1057                 fi
1058                 first_sequential_zone_sector=${result[0]}
1059                 sectors_per_zone=${result[1]}
1060                 zone_size=$((sectors_per_zone * 512))
1061                 if ! max_open_zones=$(max_open_zones "$dev"); then
1062                         echo "Failed to determine maximum number of open zones"
1063                         exit 1
1064                 fi
1065                 set_io_scheduler "$basename" deadline || exit $?
1066                 if [ -n "$reset_all_zones" ]; then
1067                         reset_zone "$dev" -1
1068                 fi
1069                 ;;
1070         *)
1071                 first_sequential_zone_sector=$(((disk_size / 2) &
1072                                                 (logical_block_size - 1)))
1073                 zone_size=$(max 65536 "$logical_block_size")
1074                 sectors_per_zone=$((zone_size / 512))
1075                 max_open_zones=128
1076                 set_io_scheduler "$basename" none || exit $?
1077                 ;;
1078         esac
1079 elif [[ -c "$realdev" ]]; then
1080         # For an SG node, we must have libzbc option specified
1081         if [[ ! -n "$use_libzbc" ]]; then
1082                 echo "Character device files can only be used with -l (libzbc) option"
1083                 exit 1
1084         fi
1085
1086         if ! $(is_zbc "$dev"); then
1087                 echo "Device is not a ZBC disk"
1088                 exit 1
1089         fi
1090         is_zbd=true
1091
1092         if ! disk_size=($(( $(zbc_disk_sectors "$dev") * 512))); then
1093                 echo "Failed to determine disk size"
1094                 exit 1
1095         fi
1096         if ! logical_block_size=($(zbc_logical_block_size "$dev")); then
1097                 echo "Failed to determine logical block size"
1098                 exit 1
1099         fi
1100         if ! result=($(first_sequential_zone "$dev")); then
1101                 echo "Failed to determine first sequential zone"
1102                 exit 1
1103         fi
1104         first_sequential_zone_sector=${result[0]}
1105         sectors_per_zone=${result[1]}
1106         zone_size=$((sectors_per_zone * 512))
1107         if ! max_open_zones=$(max_open_zones "$dev"); then
1108                 echo "Failed to determine maximum number of open zones"
1109                 exit 1
1110         fi
1111         if [ -n "$reset_all_zones" ]; then
1112                 reset_zone "$dev" -1
1113         fi
1114 fi
1115
1116 if [[ -n ${max_open_zones_opt} ]]; then
1117         # Override max_open_zones with the script option value
1118         max_open_zones="${max_open_zones_opt}"
1119         job_var_opts+=("--max_open_zones=${max_open_zones_opt}")
1120 fi
1121
1122 echo -n "First sequential zone starts at sector $first_sequential_zone_sector;"
1123 echo " zone size: $((zone_size >> 20)) MB"
1124
1125 if [ "${#tests[@]}" = 0 ]; then
1126     readarray -t tests < <(declare -F | grep "test[0-9]*" | \
1127                                    tr -c -d "[:digit:]\n" | sort -n)
1128 fi
1129
1130 logfile=$0.log
1131
1132 passed=0
1133 skipped=0
1134 failed=0
1135 if [ -t 1 ]; then
1136     red="\e[1;31m"
1137     green="\e[1;32m"
1138     cyan="\e[1;36m"
1139     end="\e[m"
1140 else
1141     red=""
1142     green=""
1143     end=""
1144 fi
1145 rc=0
1146
1147 intr=0
1148 trap 'intr=1' SIGINT
1149 ret=0
1150
1151 for test_number in "${tests[@]}"; do
1152     rm -f "${logfile}.${test_number}"
1153     unset SKIP_REASON
1154     echo -n "Running test $(printf "%02d" $test_number) ... "
1155     eval "test$test_number"
1156     ret=$?
1157     if ((!ret)) && check_log $test_number; then
1158         status="PASS"
1159         cc_status="${green}${status}${end}"
1160         ((passed++))
1161     elif ((ret==SKIP_TESTCASE)); then
1162         status="SKIP"
1163         echo "${SKIP_REASON}" >> "${logfile}.${test_number}"
1164         cc_status="${cyan}${status}${end}    ${SKIP_REASON}"
1165         ((skipped++))
1166     else
1167         status="FAIL"
1168         cc_status="${red}${status}${end}"
1169         ((failed++))
1170         rc=1
1171     fi
1172     echo -e "$cc_status"
1173     echo "$status" >> "${logfile}.${test_number}"
1174     [ $intr -ne 0 ] && exit 1
1175 done
1176
1177 echo "$passed tests passed"
1178 if [ $skipped -gt 0 ]; then
1179     echo " $skipped tests skipped"
1180 fi
1181 if [ $failed -gt 0 ]; then
1182     echo " $failed tests failed"
1183 fi
1184 exit $rc