Merge branch 'master' of https://github.com/albert-chang0/fio
[fio.git] / t / zbd / test-zbd-support
index 10c78e9a571b6c1e9552179eb2f38a5b34971564..acde3b3a3e5e08372a9da3432e7d6e0818650940 100755 (executable)
@@ -5,7 +5,16 @@
 # This file is released under the GPL.
 
 usage() {
-    echo "Usage: $(basename "$0") [-d] [-e] [-r] [-v] [-t <test>] <SMR drive device node>"
+       echo "Usage: $(basename "$0") [OPTIONS] <test target device file>"
+       echo "Options:"
+       echo -e "\t-d Run fio with valgrind using DRD tool"
+       echo -e "\t-e Run fio with valgrind using helgrind tool"
+       echo -e "\t-v Run fio with valgrind --read-var-info option"
+       echo -e "\t-l Test with libzbc ioengine"
+       echo -e "\t-r Reset all zones before test start"
+       echo -e "\t-o <max_open_zones> Run fio with max_open_zones limit"
+       echo -e "\t-t <test #> Run only a single test case with specified number"
+       echo -e "\t-z Run fio with debug=zbd option"
 }
 
 max() {
@@ -24,6 +33,14 @@ min() {
     fi
 }
 
+ioengine() {
+       if [ -n "$use_libzbc" ]; then
+               echo -n "--ioengine=libzbc"
+       else
+               echo -n "--ioengine=$1"
+       fi
+}
+
 set_io_scheduler() {
     local dev=$1 sched=$2
 
@@ -69,6 +86,13 @@ check_reset_count() {
     eval "[ '$reset_count' '$1' '$2' ]"
 }
 
+# Check log for failed assertions and crashes. Without these checks,
+# a test can succeed even when these events happen, but it must fail.
+check_log() {
+     [ ! -f "${logfile}.${1}" ] && return 0
+     ! grep -q -e "Assertion " -e "Aborted " "${logfile}.${1}"
+}
+
 # Whether or not $1 (/dev/...) is a SCSI device.
 is_scsi_device() {
     local d f
@@ -80,12 +104,41 @@ is_scsi_device() {
     return 1
 }
 
+job_var_opts_exclude() {
+       local o
+       local ex_key="${1}"
+
+       for o in "${job_var_opts[@]}"; do
+               if [[ ${o} =~ "${ex_key}" ]]; then
+                       continue
+               fi
+               echo -n "${o}"
+       done
+}
+
+has_max_open_zones() {
+       while (($# > 1)); do
+               if [[ ${1} =~ "--max_open_zones" ]]; then
+                       return 0
+               fi
+               shift
+       done
+       return 1
+}
+
 run_fio() {
     local fio opts
 
     fio=$(dirname "$0")/../../fio
 
-    opts=("--aux-path=/tmp" "--allow_file_create=0" "$@")
+    opts=(${global_var_opts[@]})
+    opts+=("--max-jobs=16" "--aux-path=/tmp" "--allow_file_create=0" \
+                          "--significant_figures=10" "$@")
+    # When max_open_zones option is specified to this test script, add
+    # max_open_zones option to fio command unless the test case already add it.
+    if [[ -n ${max_open_zones_opt} ]] && ! has_max_open_zones "${opts[@]}"; then
+           opts+=("--max_open_zones=${max_open_zones_opt}")
+    fi
     { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"
 
     "${dynamic_analyzer[@]}" "$fio" "${opts[@]}"
@@ -99,6 +152,23 @@ run_one_fio_job() {
            --thread=1 --direct=1
 }
 
+write_and_run_one_fio_job() {
+    local r
+    local write_offset="${1}"
+    local write_size="${2}"
+    local -a write_opts
+
+    shift 2
+    r=$(((RANDOM << 16) | RANDOM))
+    write_opts=(--name="write_job" --rw=write "$(ioengine "psync")" \
+                     --bs="${logical_block_size}" --zonemode=zbd \
+                     --zonesize="${zone_size}" --thread=1 --direct=1 \
+                     --offset="${write_offset}" --size="${write_size}")
+    write_opts+=("${job_var_opts[@]}")
+    run_fio --filename="$dev" --randseed="$r" "${write_opts[@]}" \
+           --name="$dev" --wait_for="write_job" "$@" --thread=1 --direct=1
+}
+
 # Run fio on the first four sequential zones of the disk.
 run_fio_on_seq() {
     local opts=()
@@ -111,10 +181,19 @@ run_fio_on_seq() {
     run_one_fio_job "${opts[@]}" "$@"
 }
 
+# Prepare for write test by resetting zones. When max_open_zones option is
+# specified, reset all zones of the test target to ensure that zones out of the
+# test target range do not have open zones. This allows the write test to the
+# target range to be able to open zones up to max_open_zones.
+prep_write() {
+       [[ -n "${max_open_zones_opt}" && -n "${is_zbd}" ]] &&
+               reset_zone "${dev}" -1
+}
+
 # Check whether buffered writes are refused.
 test1() {
     run_fio --name=job1 --filename="$dev" --rw=write --direct=0 --bs=4K        \
-           --size="${zone_size}" --thread=1                            \
+           "$(ioengine "psync")" --size="${zone_size}" --thread=1      \
            --zonemode=zbd --zonesize="${zone_size}" 2>&1 |
        tee -a "${logfile}.${test_number}" |
        grep -q 'Using direct I/O is mandatory for writing to ZBD drives'
@@ -136,6 +215,7 @@ test2() {
 
     off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
     bs=$((2 * zone_size))
+    opts+=("$(ioengine "psync")")
     opts+=("--name=job1" "--filename=$dev" "--rw=write" "--direct=1")
     opts+=("--zonemode=zbd" "--offset=$off" "--bs=$bs" "--size=$bs")
     if [ -z "$is_zbd" ]; then
@@ -154,18 +234,12 @@ test3() {
     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--bs=4K")
     opts+=("--size=$size" "--zonemode=zbd")
-    opts+=("--ioengine=psync" "--rw=read" "--direct=1" "--thread=1")
+    opts+=("$(ioengine "psync")" "--rw=read" "--direct=1" "--thread=1")
     if [ -z "$is_zbd" ]; then
        opts+=("--zonesize=${zone_size}")
     fi
     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
-    grep -q 'READ:' "${logfile}.${test_number}"
-    rc=$?
-    if [ -n "$is_zbd" ]; then
-       [ $rc != 0 ]
-    else
-       [ $rc = 0 ]
-    fi
+    ! grep -q 'READ:' "${logfile}.${test_number}"
 }
 
 # Run fio with --read_beyond_wp=1 against an empty zone.
@@ -177,7 +251,7 @@ test4() {
     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--bs=$size")
     opts+=("--size=$size" "--thread=1" "--read_beyond_wp=1")
-    opts+=("--ioengine=psync" "--rw=read" "--direct=1" "--disable_lat=1")
+    opts+=("$(ioengine "psync")" "--rw=read" "--direct=1" "--disable_lat=1")
     opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
     check_read $size || return $?
@@ -185,51 +259,68 @@ test4() {
 
 # Sequential write to sequential zones.
 test5() {
-    local size
+    local size off capacity
 
+    prep_write
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 4 $off $dev)
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=psync --iodepth=1 --rw=write             \
+    run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write        \
                   --bs="$(max $((zone_size / 64)) "$logical_block_size")"\
                   --do_verify=1 --verify=md5                           \
                   >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written $size || return $?
-    check_read $size || return $?
+    check_written $capacity || return $?
+    check_read $capacity || return $?
 }
 
-# Sequential read from sequential zones. Must be run after test5.
+# Sequential read from sequential zones.
 test6() {
-    local size
+    local size off capacity
 
+    prep_write
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 4 $off $dev)
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=psync --iodepth=1 --rw=read              \
-                  --bs="$(max $((zone_size / 64)) "$logical_block_size")"\
-                  >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_read $size || return $?
+    write_and_run_one_fio_job \
+           $((first_sequential_zone_sector * 512)) "${size}" \
+           --offset="${off}" \
+           --size="${size}" --zonemode=zbd --zonesize="${zone_size}" \
+           "$(ioengine "psync")" --iodepth=1 --rw=read \
+           --bs="$(max $((zone_size / 64)) "$logical_block_size")" \
+           >>"${logfile}.${test_number}" 2>&1 || return $?
+    check_read $capacity || return $?
 }
 
 # Random write to sequential zones, libaio, queue depth 1.
 test7() {
     local size=$((zone_size))
+    local off capacity
 
-    run_fio_on_seq --ioengine=libaio --iodepth=1 --rw=randwrite                \
+    prep_write
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 1 $off $dev)
+    run_fio_on_seq "$(ioengine "libaio")" --iodepth=1 --rw=randwrite   \
                   --bs="$(min 16384 "${zone_size}")"                   \
                   --do_verify=1 --verify=md5 --size="$size"            \
                   >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written $size || return $?
-    check_read $size || return $?
+    check_written $capacity || return $?
+    check_read $capacity || return $?
 }
 
 # Random write to sequential zones, libaio, queue depth 64.
 test8() {
-    local size
+    local size off capacity
 
+    prep_write
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=libaio --iodepth=64 --rw=randwrite       \
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 4 $off $dev)
+    run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite  \
                   --bs="$(min 16384 "${zone_size}")"                   \
                   --do_verify=1 --verify=md5                           \
                   >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written $size || return $?
-    check_read $size || return $?
+    check_written $capacity || return $?
+    check_read $capacity || return $?
 }
 
 # Random write to sequential zones, sg, queue depth 1.
@@ -241,8 +332,10 @@ test9() {
        return 0
     fi
 
+    prep_write
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=sg --iodepth=1 --rw=randwrite --bs=16K   \
+    run_fio_on_seq --ioengine=sg                                       \
+                  --iodepth=1 --rw=randwrite --bs=16K                  \
                   --do_verify=1 --verify=md5                           \
                   >>"${logfile}.${test_number}" 2>&1 || return $?
     check_written $size || return $?
@@ -258,8 +351,10 @@ test10() {
        return 0
     fi
 
+    prep_write
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=sg --iodepth=64 --rw=randwrite --bs=16K  \
+    run_fio_on_seq --ioengine=sg                                       \
+                  --iodepth=64 --rw=randwrite --bs=16K                 \
                   --do_verify=1 --verify=md5                           \
                   >>"${logfile}.${test_number}" 2>&1 || return $?
     check_written $size || return $?
@@ -268,52 +363,62 @@ test10() {
 
 # Random write to sequential zones, libaio, queue depth 64, random block size.
 test11() {
-    local size
+    local size off capacity
 
+    prep_write
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=libaio --iodepth=64 --rw=randwrite       \
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 4 $off $dev)
+    run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite  \
                   --bsrange=4K-64K --do_verify=1 --verify=md5          \
                   --debug=zbd >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written $size || return $?
-    check_read $size || return $?
+    check_written $capacity || return $?
+    check_read $capacity || return $?
 }
 
 # Random write to sequential zones, libaio, queue depth 64, max 1 open zone.
 test12() {
-    local size
+    local size off capacity
 
+    prep_write
     size=$((8 * zone_size))
-    run_fio_on_seq --ioengine=libaio --iodepth=64 --rw=randwrite --bs=16K     \
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 8 $off $dev)
+    run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
                   --max_open_zones=1 --size=$size --do_verify=1 --verify=md5 \
                   --debug=zbd >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written $size || return $?
-    check_read $size || return $?
+    check_written $capacity || return $?
+    check_read $capacity || return $?
 }
 
 # Random write to sequential zones, libaio, queue depth 64, max 4 open zones.
 test13() {
-    local size
+    local size off capacity
 
+    prep_write
     size=$((8 * zone_size))
-    run_fio_on_seq --ioengine=libaio --iodepth=64 --rw=randwrite --bs=16K     \
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 8 $off $dev)
+    run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
                   --max_open_zones=4 --size=$size --do_verify=1 --verify=md5 \
                   --debug=zbd                                                \
                   >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written $size || return $?
-    check_read $size || return $?
+    check_written $capacity || return $?
+    check_read $capacity || return $?
 }
 
 # Random write to conventional zones.
 test14() {
     local size
 
+    prep_write
     size=$((16 * 2**20)) # 20 MB
     if [ $size -gt $((first_sequential_zone_sector * 512)) ]; then
        echo "$dev does not have enough sequential zones" \
             >>"${logfile}.${test_number}"
        return 0
     fi
-    run_one_fio_job --ioengine=libaio --iodepth=64 --rw=randwrite --bs=16K \
+    run_one_fio_job "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=16K \
                    --zonemode=zbd --zonesize="${zone_size}" --do_verify=1 \
                    --verify=md5 --size=$size                              \
                    >>"${logfile}.${test_number}" 2>&1 || return $?
@@ -324,41 +429,49 @@ test14() {
 # Sequential read on a mix of empty and full zones.
 test15() {
     local i off size
+    local w_off w_size w_capacity
 
     for ((i=0;i<4;i++)); do
        [ -n "$is_zbd" ] &&
            reset_zone "$dev" $((first_sequential_zone_sector +
                                 i*sectors_per_zone))
     done
-    off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
-    size=$((2 * zone_size))
-    run_one_fio_job --ioengine=psync --rw=write --bs=$((zone_size / 16))\
-                   --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
-                   --size=$size >>"${logfile}.${test_number}" 2>&1 ||
-       return $?
-    check_written $size || return $?
+    prep_write
+    w_off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
+    w_size=$((2 * zone_size))
+    w_capacity=$(total_zone_capacity 2 $w_off $dev)
     off=$((first_sequential_zone_sector * 512))
     size=$((4 * zone_size))
-    run_one_fio_job --ioengine=psync --rw=read --bs=$((zone_size / 16))        \
+    write_and_run_one_fio_job "${w_off}" "${w_size}" \
+                   "$(ioengine "psync")" --rw=read --bs=$((zone_size / 16)) \
                    --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
                    --size=$((size)) >>"${logfile}.${test_number}" 2>&1 ||
        return $?
-    if [ -n "$is_zbd" ]; then
-       check_read $((size / 2))
-    else
-       check_read $size
-    fi
+    check_written $((w_capacity)) || return $?
+    check_read $((w_capacity))
 }
 
-# Random read on a mix of empty and full zones. Must be run after test15.
+# Random read on a mix of empty and full zones.
 test16() {
     local off size
+    local i w_off w_size w_capacity
 
+    for ((i=0;i<4;i++)); do
+       [ -n "$is_zbd" ] &&
+           reset_zone "$dev" $((first_sequential_zone_sector +
+                                i*sectors_per_zone))
+    done
+    prep_write
+    w_off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
+    w_size=$((2 * zone_size))
+    w_capacity=$(total_zone_capacity 2 $w_off $dev)
     off=$((first_sequential_zone_sector * 512))
     size=$((4 * zone_size))
-    run_one_fio_job --ioengine=libaio --iodepth=64 --rw=randread --bs=16K \
+    write_and_run_one_fio_job "${w_off}" "${w_size}" \
+                   "$(ioengine "libaio")" --iodepth=64 --rw=randread --bs=16K \
                    --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
                    --size=$size >>"${logfile}.${test_number}" 2>&1 || return $?
+    check_written $w_capacity || return $?
     check_read $size || return $?
 }
 
@@ -368,16 +481,11 @@ test17() {
 
     off=$(((disk_size / zone_size - 1) * zone_size))
     size=$((disk_size - off))
-    # Overwrite the last zone to avoid that reading from that zone fails.
     if [ -n "$is_zbd" ]; then
        reset_zone "$dev" $((off / 512)) || return $?
     fi
-    run_one_fio_job --ioengine=psync --rw=write --offset="$off"                \
-                   --zonemode=zbd --zonesize="${zone_size}"            \
-                   --bs="$zone_size" --size="$zone_size"               \
-                   >>"${logfile}.${test_number}" 2>&1 || return $?
-    check_written "$zone_size" || return $?
-    run_one_fio_job --ioengine=libaio --iodepth=8 --rw=randrw --bs=4K  \
+    prep_write
+    run_one_fio_job "$(ioengine "libaio")" --iodepth=8 --rw=randrw --bs=4K \
                    --zonemode=zbd --zonesize="${zone_size}"            \
                    --offset=$off --loops=2 --norandommap=1\
                    >>"${logfile}.${test_number}" 2>&1 || return $?
@@ -428,13 +536,18 @@ test23() {
 
 test24() {
     local bs loops=9 size=$((zone_size))
+    local off capacity
+
+    prep_write
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 1 $off $dev)
 
     bs=$(min $((256*1024)) "$zone_size")
-    run_fio_on_seq --ioengine=psync --rw=write --bs="$bs" --size=$size  \
-                  --loops=$loops                                        \
+    run_fio_on_seq "$(ioengine "psync")" --rw=write --bs="$bs"         \
+                  --size=$size --loops=$loops                          \
                   --zone_reset_frequency=.01 --zone_reset_threshold=.90 \
                   >> "${logfile}.${test_number}" 2>&1 || return $?
-    check_written $((size * loops)) || return $?
+    check_written $((capacity * loops)) || return $?
     check_reset_count -eq 8 ||
        check_reset_count -eq 9 ||
        check_reset_count -eq 10 || return $?
@@ -448,26 +561,33 @@ test25() {
         [ -n "$is_zbd" ] &&
            reset_zone "$dev" $((first_sequential_zone_sector + i*sectors_per_zone))
     done
+    prep_write
     for ((i=0;i<16;i++)); do
        opts+=("--name=job$i" "--filename=$dev" "--thread=1" "--direct=1")
        opts+=("--offset=$((first_sequential_zone_sector*512 + zone_size*i))")
-       opts+=("--size=$zone_size" "--ioengine=psync" "--rw=write" "--bs=16K")
+       opts+=("--size=$zone_size" "$(ioengine "psync")" "--rw=write" "--bs=16K")
        opts+=("--zonemode=zbd" "--zonesize=${zone_size}" "--group_reporting=1")
+       opts+=(${job_var_opts[@]})
     done
     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
 }
 
 write_to_first_seq_zone() {
     local loops=4 r
+    local off capacity
+
+    prep_write
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 1 $off $dev)
 
     r=$(((RANDOM << 16) | RANDOM))
-    run_fio --name="$dev" --filename="$dev" --ioengine=psync --rw="$1" \
+    run_fio --name="$dev" --filename="$dev" "$(ioengine "psync")" --rw="$1" \
            --thread=1 --do_verify=1 --verify=md5 --direct=1 --bs=4K    \
-           --offset=$((first_sequential_zone_sector * 512))            \
-           "--size=$zone_size" --loops=$loops --randseed="$r"          \
+           --offset=$off                                               \
+           --size=$zone_size --loops=$loops --randseed="$r"            \
            --zonemode=zbd --zonesize="${zone_size}" --group_reporting=1        \
            --gtod_reduce=1 >> "${logfile}.${test_number}" 2>&1 || return $?
-    check_written $((loops * zone_size)) || return $?
+    check_written $((loops * capacity)) || return $?
 }
 
 # Overwrite the first sequential zone four times sequentially.
@@ -486,15 +606,18 @@ test28() {
 
     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
     [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
+    prep_write
     opts=("--debug=zbd")
+    capacity=$(total_zone_capacity 1 $off $dev)
     for ((i=0;i<jobs;i++)); do
        opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
-       opts+=("--size=$zone_size" "--ioengine=psync" "--rw=randwrite")
+       opts+=("--size=$zone_size" "--io_size=$capacity" "$(ioengine "psync")" "--rw=randwrite")
        opts+=("--thread=1" "--direct=1" "--zonemode=zbd")
        opts+=("--zonesize=${zone_size}" "--group_reporting=1")
+       opts+=(${job_var_opts[@]})
     done
     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
-    check_written $((jobs * zone_size)) || return $?
+    check_written $((jobs * $capacity)) || return $?
     check_reset_count -eq $jobs ||
        check_reset_count -eq $((jobs - 1)) ||
        return $?
@@ -507,14 +630,16 @@ test29() {
 
     off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
     size=$((16*zone_size))
-    [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
+    prep_write
     opts=("--debug=zbd")
     for ((i=0;i<jobs;i++)); do
        opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
        opts+=("--size=$size" "--io_size=$zone_size" "--thread=1")
-       opts+=("--ioengine=psync" "--rw=randwrite" "--direct=1")
+       opts+=("$(ioengine "psync")" "--rw=randwrite" "--direct=1")
        opts+=("--max_open_zones=4" "--group_reporting=1")
        opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
+       # max_open_zones is already specified
+       opts+=($(job_var_opts_exclude "--max_open_zones"))
     done
     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
     check_written $((jobs * zone_size)) || return $?
@@ -524,8 +649,9 @@ test29() {
 test30() {
     local off
 
+    prep_write
     off=$((first_sequential_zone_sector * 512))
-    run_one_fio_job --ioengine=libaio --iodepth=8 --rw=randrw          \
+    run_one_fio_job "$(ioengine "libaio")" --iodepth=8 --rw=randrw     \
                    --bs="$(max $((zone_size / 128)) "$logical_block_size")"\
                    --zonemode=zbd --zonesize="${zone_size}" --offset=$off\
                    --loops=2 --time_based --runtime=30s --norandommap=1\
@@ -537,6 +663,7 @@ test30() {
 test31() {
     local bs inc nz off opts size
 
+    prep_write
     # Start with writing 128 KB to 128 sequential zones.
     bs=128K
     nz=128
@@ -547,18 +674,20 @@ test31() {
     for ((off = first_sequential_zone_sector * 512; off < disk_size;
          off += inc)); do
        opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--io_size=$bs")
-       opts+=("--bs=$bs" "--size=$zone_size" "--ioengine=libaio")
+       opts+=("--bs=$bs" "--size=$zone_size" "$(ioengine "libaio")")
        opts+=("--rw=write" "--direct=1" "--thread=1" "--stats=0")
        opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
+       opts+=(${job_var_opts[@]})
     done
     "$(dirname "$0")/../../fio" "${opts[@]}" >> "${logfile}.${test_number}" 2>&1
     # Next, run the test.
     off=$((first_sequential_zone_sector * 512))
     size=$((disk_size - off))
     opts=("--name=$dev" "--filename=$dev" "--offset=$off" "--size=$size")
-    opts+=("--bs=$bs" "--ioengine=psync" "--rw=randread" "--direct=1")
+    opts+=("--bs=$bs" "$(ioengine "psync")" "--rw=randread" "--direct=1")
     opts+=("--thread=1" "--time_based" "--runtime=30" "--zonemode=zbd")
     opts+=("--zonesize=${zone_size}")
+    opts+=(${job_var_opts[@]})
     run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
 }
 
@@ -567,10 +696,11 @@ test31() {
 test32() {
     local off opts=() size
 
+    prep_write
     off=$((first_sequential_zone_sector * 512))
     size=$((disk_size - off))
     opts+=("--name=$dev" "--filename=$dev" "--offset=$off" "--size=$size")
-    opts+=("--bs=128K" "--ioengine=psync" "--rw=randwrite" "--direct=1")
+    opts+=("--bs=128K" "$(ioengine "psync")" "--rw=randwrite" "--direct=1")
     opts+=("--thread=1" "--time_based" "--runtime=30")
     opts+=("--max_open_zones=$max_open_zones" "--zonemode=zbd")
     opts+=("--zonesize=${zone_size}")
@@ -581,12 +711,16 @@ test32() {
 # zone size.
 test33() {
     local bs io_size size
+    local off capacity=0;
 
+    prep_write
+    off=$((first_sequential_zone_sector * 512))
+    capacity=$(total_zone_capacity 1 $off $dev)
     size=$((2 * zone_size))
-    io_size=$((5 * zone_size))
-    bs=$((3 * zone_size / 4))
-    run_fio_on_seq --ioengine=psync --iodepth=1 --rw=write --size=$size        \
-                  --io_size=$io_size --bs=$bs                          \
+    io_size=$((5 * capacity))
+    bs=$((3 * capacity / 4))
+    run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write        \
+                  --size=$size --io_size=$io_size --bs=$bs     \
                   >> "${logfile}.${test_number}" 2>&1 || return $?
     check_written $(((io_size + bs - 1) / bs * bs)) || return $?
 }
@@ -596,8 +730,9 @@ test33() {
 test34() {
     local size
 
+    prep_write
     size=$((2 * zone_size))
-    run_fio_on_seq --ioengine=psync --iodepth=1 --rw=write --size=$size          \
+    run_fio_on_seq "$(ioengine "psync")" --iodepth=1 --rw=write --size=$size \
                   --do_verify=1 --verify=md5 --bs=$((3 * zone_size / 4)) \
                   >> "${logfile}.${test_number}" 2>&1 && return 1
     grep -q 'not a divisor of' "${logfile}.${test_number}"
@@ -607,12 +742,13 @@ test34() {
 test35() {
     local bs off io_size size
 
+    prep_write
     off=$(((first_sequential_zone_sector + 1) * 512))
     size=$((zone_size - 2 * 512))
     bs=$((zone_size / 4))
-    run_one_fio_job --offset=$off --size=$size --ioengine=psync        --iodepth=1 \
-                   --rw=write --do_verify=1 --verify=md5 --bs=$bs          \
-                   --zonemode=zbd --zonesize="${zone_size}"                \
+    run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"   \
+                   --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
+                   --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
                    >> "${logfile}.${test_number}" 2>&1 && return 1
     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
 }
@@ -621,20 +757,23 @@ test35() {
 test36() {
     local bs off io_size size
 
+    prep_write
     off=$(((first_sequential_zone_sector) * 512))
     size=$((zone_size - 512))
     bs=$((zone_size / 4))
-    run_one_fio_job --offset=$off --size=$size --ioengine=psync        --iodepth=1 \
-                   --rw=write --do_verify=1 --verify=md5 --bs=$bs          \
-                   --zonemode=zbd --zonesize="${zone_size}"                \
+    run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"   \
+                   --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
+                   --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
                    >> "${logfile}.${test_number}" 2>&1 && return 1
     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
 }
 
 # Test 3/4 for the I/O boundary rounding code: $size > $zone_size.
 test37() {
-    local bs off size
+    local bs off size capacity
 
+    prep_write
+    capacity=$(total_zone_capacity 1 $first_sequential_zone_sector $dev)
     if [ "$first_sequential_zone_sector" = 0 ]; then
        off=0
     else
@@ -642,23 +781,24 @@ test37() {
     fi
     size=$((zone_size + 2 * 512))
     bs=$((zone_size / 4))
-    run_one_fio_job --offset=$off --size=$size --ioengine=psync        --iodepth=1 \
-                   --rw=write --do_verify=1 --verify=md5 --bs=$bs          \
-                   --zonemode=zbd --zonesize="${zone_size}"                \
+    run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"   \
+                   --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
+                   --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
                    >> "${logfile}.${test_number}" 2>&1
-    check_written $((zone_size)) || return $?
+    check_written $capacity || return $?
 }
 
 # Test 4/4 for the I/O boundary rounding code: $offset > $disk_size - $zone_size
 test38() {
     local bs off size
 
+    prep_write
     size=$((logical_block_size))
     off=$((disk_size - logical_block_size))
     bs=$((logical_block_size))
-    run_one_fio_job --offset=$off --size=$size --ioengine=psync        --iodepth=1 \
-                   --rw=write --do_verify=1 --verify=md5 --bs=$bs          \
-                   --zonemode=zbd --zonesize="${zone_size}"                \
+    run_one_fio_job --offset=$off --size=$size "$(ioengine "psync")"   \
+                   --iodepth=1 --rw=write --do_verify=1 --verify=md5   \
+                   --bs=$bs --zonemode=zbd --zonesize="${zone_size}"   \
                    >> "${logfile}.${test_number}" 2>&1 && return 1
     grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
 }
@@ -668,7 +808,7 @@ read_one_block() {
     local bs
 
     bs=$((logical_block_size))
-    run_one_fio_job --rw=read --ioengine=psync --bs=$bs --size=$bs "$@" 2>&1 |
+    run_one_fio_job --rw=read "$(ioengine "psync")" --bs=$bs --size=$bs "$@" 2>&1 |
        tee -a "${logfile}.${test_number}"
 }
 
@@ -706,8 +846,9 @@ test42() {
        grep -q 'Specifying the zone size is mandatory for regular block devices with --zonemode=zbd'
 }
 
-# Check whether fio handles --zonesize=1 correctly.
+# Check whether fio handles --zonesize=1 correctly for regular block devices.
 test43() {
+    [ -n "$is_zbd" ] && return 0
     read_one_block --zonemode=zbd --zonesize=1 |
        grep -q 'zone size must be at least 512 bytes for --zonemode=zbd'
 }
@@ -722,8 +863,9 @@ test45() {
     local bs i
 
     [ -z "$is_zbd" ] && return 0
+    prep_write
     bs=$((logical_block_size))
-    run_one_fio_job --ioengine=psync --iodepth=1 --rw=randwrite --bs=$bs\
+    run_one_fio_job "$(ioengine "psync")" --iodepth=1 --rw=randwrite --bs=$bs\
                    --offset=$((first_sequential_zone_sector * 512)) \
                    --size="$zone_size" --do_verify=1 --verify=md5 2>&1 |
        tee -a "${logfile}.${test_number}" |
@@ -734,16 +876,84 @@ test45() {
 test46() {
     local size
 
+    prep_write
     size=$((4 * zone_size))
-    run_fio_on_seq --ioengine=libaio --iodepth=64 --rw=randwrite --bs=4K \
+    run_fio_on_seq "$(ioengine "libaio")" --iodepth=64 --rw=randwrite --bs=4K \
                   --group_reporting=1 --numjobs=8 \
                   >> "${logfile}.${test_number}" 2>&1 || return $?
     check_written $((size * 8)) || return $?
 }
 
+# Check whether fio handles --zonemode=zbd --zoneskip=1 correctly.
+test47() {
+    local bs
+
+    prep_write
+    bs=$((logical_block_size))
+    run_fio_on_seq "$(ioengine "psync")" --rw=write --bs=$bs --zoneskip=1 \
+                   >> "${logfile}.${test_number}" 2>&1 && return 1
+    grep -q 'zoneskip 1 is not a multiple of the device zone size' "${logfile}.${test_number}"
+}
+
+# Multiple overlapping random write jobs for the same drive and with a
+# limited number of open zones. This is similar to test29, but uses libaio
+# to stress test zone locking.
+test48() {
+    local i jobs=16 off opts=()
+
+    off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
+    size=$((16*zone_size))
+    prep_write
+    opts=("--aux-path=/tmp" "--allow_file_create=0" "--significant_figures=10")
+    opts+=("--debug=zbd")
+    opts+=("$(ioengine "libaio")" "--rw=randwrite" "--direct=1")
+    opts+=("--time_based" "--runtime=30")
+    opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
+    opts+=("--max_open_zones=4")
+    for ((i=0;i<jobs;i++)); do
+       opts+=("--name=job$i" "--filename=$dev" "--offset=$off" "--bs=16K")
+       opts+=("--io_size=$zone_size" "--iodepth=256" "--thread=1")
+       opts+=("--size=$size" "--group_reporting=1")
+       # max_open_zones is already specified
+       opts+=($(job_var_opts_exclude "--max_open_zones"))
+    done
+
+    fio=$(dirname "$0")/../../fio
+
+    { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"
+
+    timeout -v -s KILL 45s \
+           "${dynamic_analyzer[@]}" "$fio" "${opts[@]}" \
+           >> "${logfile}.${test_number}" 2>&1 || return $?
+}
+
+# Check if fio handles --zonecapacity on a normal block device correctly
+test49() {
+
+    if [ -n "$is_zbd" ]; then
+       echo "$dev is not a regular block device" \
+            >>"${logfile}.${test_number}"
+       return 0
+    fi
+
+    size=$((2 * zone_size))
+    capacity=$((zone_size * 3 / 4))
+
+    run_one_fio_job "$(ioengine "psync")" --rw=write \
+                   --zonemode=zbd --zonesize="${zone_size}" \
+                   --zonecapacity=${capacity} \
+                   --verify=md5  --size=${size} >>"${logfile}.${test_number}" 2>&1 ||
+       return $?
+    check_written $((capacity * 2)) || return $?
+    check_read $((capacity * 2)) || return $?
+}
+
 tests=()
 dynamic_analyzer=()
 reset_all_zones=
+use_libzbc=
+zbd_debug=
+max_open_zones_opt=
 
 while [ "${1#-}" != "$1" ]; do
   case "$1" in
@@ -752,10 +962,13 @@ while [ "${1#-}" != "$1" ]; do
        shift;;
     -e) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=helgrind");
        shift;;
+    -l) use_libzbc=1; shift;;
     -r) reset_all_zones=1; shift;;
     -t) tests+=("$2"); shift; shift;;
+    -o) max_open_zones_opt="${2}"; shift; shift;;
     -v) dynamic_analyzer=(valgrind "--read-var-info=yes");
        shift;;
+    -z) zbd_debug=1; shift;;
     --) shift; break;;
   esac
 done
@@ -768,73 +981,143 @@ fi
 # shellcheck source=functions
 source "$(dirname "$0")/functions" || exit $?
 
+global_var_opts=()
+job_var_opts=()
+if [ -n "$zbd_debug" ]; then
+    global_var_opts+=("--debug=zbd")
+fi
 dev=$1
 realdev=$(readlink -f "$dev")
 basename=$(basename "$realdev")
-major=$((0x$(stat -L -c '%t' "$realdev")))
-minor=$((0x$(stat -L -c '%T' "$realdev")))
-disk_size=$(($(<"/sys/dev/block/$major:$minor/size")*512))
-# When the target is a partition device, get basename of its holder device to
-# access sysfs path of the holder device
-if [[ -r "/sys/dev/block/$major:$minor/partition" ]]; then
-       realsysfs=$(readlink "/sys/dev/block/$major:$minor")
-       basename=$(basename "${realsysfs%/*}")
-fi
-logical_block_size=$(<"/sys/block/$basename/queue/logical_block_size")
-case "$(<"/sys/class/block/$basename/queue/zoned")" in
-    host-managed|host-aware)
+
+if [[ -b "$realdev" ]]; then
+       major=$((0x$(stat -L -c '%t' "$realdev"))) || exit $?
+       minor=$((0x$(stat -L -c '%T' "$realdev"))) || exit $?
+       disk_size=$(($(<"/sys/dev/block/$major:$minor/size")*512))
+
+       # When the target is a partition device, get basename of its
+       # holder device to access sysfs path of the holder device
+       if [[ -r "/sys/dev/block/$major:$minor/partition" ]]; then
+               realsysfs=$(readlink "/sys/dev/block/$major:$minor")
+               basename=$(basename "${realsysfs%/*}")
+       fi
+       logical_block_size=$(<"/sys/block/$basename/queue/logical_block_size")
+       case "$(<"/sys/class/block/$basename/queue/zoned")" in
+       host-managed|host-aware)
+               is_zbd=true
+               if ! check_blkzone "${dev}"; then
+                       exit 1
+               fi
+               if ! result=($(first_sequential_zone "$dev")); then
+                       echo "Failed to determine first sequential zone"
+                       exit 1
+               fi
+               first_sequential_zone_sector=${result[0]}
+               sectors_per_zone=${result[1]}
+               zone_size=$((sectors_per_zone * 512))
+               if ! max_open_zones=$(max_open_zones "$dev"); then
+                       echo "Failed to determine maximum number of open zones"
+                       exit 1
+               fi
+               set_io_scheduler "$basename" deadline || exit $?
+               if [ -n "$reset_all_zones" ]; then
+                       reset_zone "$dev" -1
+               fi
+               ;;
+       *)
+               first_sequential_zone_sector=$(((disk_size / 2) &
+                                               (logical_block_size - 1)))
+               zone_size=$(max 65536 "$logical_block_size")
+               sectors_per_zone=$((zone_size / 512))
+               max_open_zones=128
+               set_io_scheduler "$basename" none || exit $?
+               ;;
+       esac
+elif [[ -c "$realdev" ]]; then
+       # For an SG node, we must have libzbc option specified
+       if [[ ! -n "$use_libzbc" ]]; then
+               echo "Character device files can only be used with -l (libzbc) option"
+               exit 1
+       fi
+
+       if ! $(is_zbc "$dev"); then
+               echo "Device is not a ZBC disk"
+               exit 1
+       fi
        is_zbd=true
+
+       if ! disk_size=($(( $(zbc_disk_sectors "$dev") * 512))); then
+               echo "Failed to determine disk size"
+               exit 1
+       fi
+       if ! logical_block_size=($(zbc_logical_block_size "$dev")); then
+               echo "Failed to determine logical block size"
+               exit 1
+       fi
        if ! result=($(first_sequential_zone "$dev")); then
-           echo "Failed to determine first sequential zone"
-           exit 1
+               echo "Failed to determine first sequential zone"
+               exit 1
        fi
        first_sequential_zone_sector=${result[0]}
        sectors_per_zone=${result[1]}
        zone_size=$((sectors_per_zone * 512))
        if ! max_open_zones=$(max_open_zones "$dev"); then
-           echo "Failed to determine maximum number of open zones"
-           exit 1
+               echo "Failed to determine maximum number of open zones"
+               exit 1
        fi
-       echo "First sequential zone starts at sector $first_sequential_zone_sector; zone size: $((zone_size >> 20)) MB"
-       set_io_scheduler "$basename" deadline || exit $?
        if [ -n "$reset_all_zones" ]; then
-           reset_zone "$dev" -1
+               reset_zone "$dev" -1
        fi
-       ;;
-    *)
-       first_sequential_zone_sector=$(((disk_size / 2) &
-                                       (logical_block_size - 1)))
-       zone_size=$(max 65536 "$logical_block_size")
-       sectors_per_zone=$((zone_size / 512))
-       max_open_zones=128
-       set_io_scheduler "$basename" none || exit $?
-       ;;
-esac
+fi
+
+if [[ -n ${max_open_zones_opt} ]]; then
+       # Override max_open_zones with the script option value
+       max_open_zones="${max_open_zones_opt}"
+       job_var_opts+=("--max_open_zones=${max_open_zones_opt}")
+fi
+
+echo -n "First sequential zone starts at sector $first_sequential_zone_sector;"
+echo " zone size: $((zone_size >> 20)) MB"
 
 if [ "${#tests[@]}" = 0 ]; then
-    for ((i=1;i<=46;i++)); do
-       tests+=("$i")
-    done
+    readarray -t tests < <(declare -F | grep "test[0-9]*" | \
+                                  tr -c -d "[:digit:]\n" | sort -n)
 fi
 
 logfile=$0.log
 
 passed=0
 failed=0
+if [ -t 1 ]; then
+    red="\e[1;31m"
+    green="\e[1;32m"
+    end="\e[m"
+else
+    red=""
+    green=""
+    end=""
+fi
 rc=0
+
+intr=0
+trap 'intr=1' SIGINT
+
 for test_number in "${tests[@]}"; do
     rm -f "${logfile}.${test_number}"
-    echo -n "Running test $test_number ... "
-    if eval "test$test_number"; then
+    echo -n "Running test $(printf "%02d" $test_number) ... "
+    if eval "test$test_number" && check_log $test_number; then
        status="PASS"
+       cc_status="${green}${status}${end}"
        ((passed++))
     else
        status="FAIL"
+       cc_status="${red}${status}${end}"
        ((failed++))
        rc=1
     fi
-    echo "$status"
+    echo -e "$cc_status"
     echo "$status" >> "${logfile}.${test_number}"
+    [ $intr -ne 0 ] && exit 1
 done
 
 echo "$passed tests passed"