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