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