summaryrefslogtreecommitdiff
path: root/t/zbd/test-zbd-support
blob: 5d079a8b787375d8b9e0bb6572ff909975be2fb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#!/bin/bash
#
# Copyright (C) 2018 Western Digital Corporation or its affiliates.
#
# This file is released under the GPL.

usage() {
    echo "Usage: $(basename "$0") [-d] [-e] [-r] [-v] [-t <test>] <SMR drive device node>"
}

max() {
    if [ "$1" -gt "$2" ]; then
	echo "$1"
    else
	echo "$2"
    fi
}

min() {
    if [ "$1" -lt "$2" ]; then
	echo "$1"
    else
	echo "$2"
    fi
}

set_io_scheduler() {
    local dev=$1 sched=$2

    [ -e "/sys/block/$dev" ] || return $?
    if [ -e "/sys/block/$dev/mq" ]; then
	case "$sched" in
	    noop)        sched=none;;
	    deadline)    sched=mq-deadline;;
	esac
    else
	case "$sched" in
	    none)        sched=noop;;
	    mq-deadline) sched=deadline;;
	esac
    fi

    echo "$sched" >"/sys/block/$dev/queue/scheduler"
}

check_read() {
    local read

    read=$(fio_read <"${logfile}.${test_number}")
    echo "read: $read <> $1" >> "${logfile}.${test_number}"
    [ "$read" = "$1" ]
}

check_written() {
    local written

    written=$(fio_written <"${logfile}.${test_number}")
    echo "written: $written <> $1" >> "${logfile}.${test_number}"
    [ "$written" = "$1" ]
}

# Compare the reset count from the log file with reset count $2 using operator
# $1 (=, -ge, -gt, -le, -lt).
check_reset_count() {
    local reset_count

    reset_count=$(fio_reset_count <"${logfile}.${test_number}")
    echo "reset_count: test $reset_count $1 $2" >> "${logfile}.${test_number}"
    eval "[ '$reset_count' '$1' '$2' ]"
}

# Whether or not $1 (/dev/...) is a SCSI device.
is_scsi_device() {
    local d f

    d=$(basename "$dev")
    for f in /sys/class/scsi_device/*/device/block/"$d"; do
	[ -e "$f" ] && return 0
    done
    return 1
}

run_fio() {
    local fio opts

    fio=$(dirname "$0")/../../fio

    opts=("--aux-path=/tmp" "--allow_file_create=0" \
			    "--significant_figures=10" "$@")
    { echo; echo "fio ${opts[*]}"; echo; } >>"${logfile}.${test_number}"

    "${dynamic_analyzer[@]}" "$fio" "${opts[@]}"
}

run_one_fio_job() {
    local r

    r=$(((RANDOM << 16) | RANDOM))
    run_fio --name="$dev" --filename="$dev" "$@" --randseed="$r"	\
	    --thread=1 --direct=1
}

# Run fio on the first four sequential zones of the disk.
run_fio_on_seq() {
    local opts=()

    opts+=("--offset=$((first_sequential_zone_sector * 512))")
    opts+=("--size=$((4 * zone_size))" "--zonemode=zbd")
    if [ -z "$is_zbd" ]; then
	opts+=("--zonesize=${zone_size}")
    fi
    run_one_fio_job "${opts[@]}" "$@"
}

# Check whether buffered writes are refused.
test1() {
    run_fio --name=job1 --filename="$dev" --rw=write --direct=0 --bs=4K	\
	    --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'
    local fio_rc=${PIPESTATUS[0]} grep_rc=${PIPESTATUS[2]}
    case "$fio_rc" in
	0|1) ;;
	*)   return "$fio_rc"
    esac
    if [ -n "$is_zbd" ]; then
	[ "$grep_rc" = 0 ]
    else
	[ "$grep_rc" != 0 ]
    fi
}

# Block size exceeds zone size.
test2() {
    local bs off opts=() rc

    off=$(((first_sequential_zone_sector + 2 * sectors_per_zone) * 512))
    bs=$((2 * zone_size))
    opts+=("--name=job1" "--filename=$dev" "--rw=write" "--direct=1")
    opts+=("--zonemode=zbd" "--offset=$off" "--bs=$bs" "--size=$bs")
    if [ -z "$is_zbd" ]; then
	opts+=("--zonesize=${zone_size}")
    fi
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
    ! grep -q 'WRITE:' "${logfile}.${test_number}"
}

# Run fio against an empty zone. This causes fio to report "No I/O performed".
test3() {
    local off opts=() rc

    off=$((first_sequential_zone_sector * 512 + 128 * zone_size))
    size=$((zone_size))
    [ -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")
    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
}

# Run fio with --read_beyond_wp=1 against an empty zone.
test4() {
    local off opts=()

    off=$((first_sequential_zone_sector * 512 + 129 * zone_size))
    size=$((zone_size))
    [ -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+=("--zonemode=zbd" "--zonesize=${zone_size}")
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
    check_read $size || return $?
}

# Sequential write to sequential zones.
test5() {
    local size

    size=$((4 * zone_size))
    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 $?
}

# Sequential read from sequential zones. Must be run after test5.
test6() {
    local size

    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 $?
}

# Random write to sequential zones, libaio, queue depth 1.
test7() {
    local size=$((zone_size))

    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 $?
}

# Random write to sequential zones, libaio, queue depth 64.
test8() {
    local size

    size=$((4 * zone_size))
    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 $?
}

# Random write to sequential zones, sg, queue depth 1.
test9() {
    local size

    if ! is_scsi_device "$dev"; then
	echo "$dev is not a SCSI device" >>"${logfile}.${test_number}"
	return 0
    fi

    size=$((4 * zone_size))
    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 $?
    check_read $size || return $?
}

# Random write to sequential zones, sg, queue depth 64.
test10() {
    local size

    if ! is_scsi_device "$dev"; then
	echo "$dev is not a SCSI device" >>"${logfile}.${test_number}"
	return 0
    fi

    size=$((4 * zone_size))
    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 $?
    check_read $size || return $?
}

# Random write to sequential zones, libaio, queue depth 64, random block size.
test11() {
    local size

    size=$((4 * zone_size))
    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 $?
}

# Random write to sequential zones, libaio, queue depth 64, max 1 open zone.
test12() {
    local size

    size=$((8 * zone_size))
    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 $?
}

# Random write to sequential zones, libaio, queue depth 64, max 4 open zones.
test13() {
    local size

    size=$((8 * zone_size))
    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 $?
}

# Random write to conventional zones.
test14() {
    local size

    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 \
		    --zonemode=zbd --zonesize="${zone_size}" --do_verify=1 \
		    --verify=md5 --size=$size				   \
		    >>"${logfile}.${test_number}" 2>&1 || return $?
    check_written $((size)) || return $?
    check_read $((size)) || return $?
}

# Sequential read on a mix of empty and full zones.
test15() {
    local i off size

    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 $?
    off=$((first_sequential_zone_sector * 512))
    size=$((4 * zone_size))
    run_one_fio_job --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
}

# Random read on a mix of empty and full zones. Must be run after test15.
test16() {
    local off size

    off=$((first_sequential_zone_sector * 512))
    size=$((4 * zone_size))
    run_one_fio_job --ioengine=libaio --iodepth=64 --rw=randread --bs=16K \
		    --zonemode=zbd --zonesize="${zone_size}" --offset=$off \
		    --size=$size >>"${logfile}.${test_number}" 2>&1 || return $?
    check_read $size || return $?
}

# Random reads and writes in the last zone.
test17() {
    local io off read size written

    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	\
		    --zonemode=zbd --zonesize="${zone_size}"		\
		    --offset=$off --loops=2 --norandommap=1\
		    >>"${logfile}.${test_number}" 2>&1 || return $?
    written=$(fio_written <"${logfile}.${test_number}")
    read=$(fio_read <"${logfile}.${test_number}")
    io=$((written + read))
    echo "Total number of bytes read and written: $io <> $size" \
	 >>"${logfile}.${test_number}"
    [ $io = $((size * 2)) ];
}

# Out-of-range zone reset threshold and frequency parameters.
test18() {
    run_fio_on_seq --zone_reset_threshold=-1 |&
	tee -a "${logfile}.${test_number}"   |
	    grep -q 'value out of range' || return $?
}

test19() {
    run_fio_on_seq --zone_reset_threshold=2  |&
	tee -a "${logfile}.${test_number}"   |
	grep -q 'value out of range' || return $?
}

test20() {
    run_fio_on_seq --zone_reset_threshold=.4:.6 |&
	tee -a "${logfile}.${test_number}"   |
	grep -q 'the list exceeding max length' || return $?
}

test21() {
    run_fio_on_seq --zone_reset_frequency=-1 |&
	tee -a "${logfile}.${test_number}"   |
	grep -q 'value out of range' || return $?
}

test22() {
    run_fio_on_seq --zone_reset_frequency=2  |&
	tee -a "${logfile}.${test_number}"   |
	grep -q 'value out of range' || return $?
}

test23() {
    run_fio_on_seq --zone_reset_frequency=.4:.6  |&
	tee -a "${logfile}.${test_number}"   |
	grep -q 'the list exceeding max length' || return $?
}

test24() {
    local bs loops=9 size=$((zone_size))

    bs=$(min $((256*1024)) "$zone_size")
    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_reset_count -eq 8 ||
	check_reset_count -eq 9 ||
	check_reset_count -eq 10 || return $?
}

# Multiple non-overlapping sequential write jobs for the same drive.
test25() {
    local i opts=()

    for ((i=0;i<16;i++)); do
        [ -n "$is_zbd" ] &&
	    reset_zone "$dev" $((first_sequential_zone_sector + i*sectors_per_zone))
    done
    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+=("--zonemode=zbd" "--zonesize=${zone_size}" "--group_reporting=1")
    done
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
}

write_to_first_seq_zone() {
    local loops=4 r

    r=$(((RANDOM << 16) | RANDOM))
    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"		\
	    --zonemode=zbd --zonesize="${zone_size}" --group_reporting=1	\
	    --gtod_reduce=1 >> "${logfile}.${test_number}" 2>&1 || return $?
    check_written $((loops * zone_size)) || return $?
}

# Overwrite the first sequential zone four times sequentially.
test26() {
    write_to_first_seq_zone write
}

# Overwrite the first sequential zone four times using random writes.
test27() {
    write_to_first_seq_zone randwrite
}

# Multiple overlapping random write jobs for the same drive.
test28() {
    local i jobs=16 off opts

    off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
    [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
    opts=("--debug=zbd")
    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+=("--thread=1" "--direct=1" "--zonemode=zbd")
	opts+=("--zonesize=${zone_size}" "--group_reporting=1")
    done
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
    check_written $((jobs * zone_size)) || return $?
    check_reset_count -eq $jobs ||
	check_reset_count -eq $((jobs - 1)) ||
	return $?
}

# Multiple overlapping random write jobs for the same drive and with a limited
# number of open zones.
test29() {
    local i jobs=16 off opts=()

    off=$((first_sequential_zone_sector * 512 + 64 * zone_size))
    size=$((16*zone_size))
    [ -n "$is_zbd" ] && reset_zone "$dev" $((off / 512))
    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+=("--max_open_zones=4" "--group_reporting=1")
	opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
    done
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
    check_written $((jobs * zone_size)) || return $?
}

# Random reads and writes across the entire disk for 30s.
test30() {
    local off

    off=$((first_sequential_zone_sector * 512))
    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\
		    >>"${logfile}.${test_number}" 2>&1
}

# Random reads across all sequential zones for 30s. This is not only a fio
# test but also allows to verify the performance of a drive.
test31() {
    local bs inc nz off opts size

    # Start with writing 128 KB to 128 sequential zones.
    bs=128K
    nz=128
    # shellcheck disable=SC2017
    inc=$(((disk_size - (first_sequential_zone_sector * 512)) / (nz * zone_size)
	   * zone_size))
    opts=()
    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+=("--rw=write" "--direct=1" "--thread=1" "--stats=0")
	opts+=("--zonemode=zbd" "--zonesize=${zone_size}")
    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+=("--thread=1" "--time_based" "--runtime=30" "--zonemode=zbd")
    opts+=("--zonesize=${zone_size}")
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
}

# Random writes across all sequential zones. This is not only a fio test but
# also allows to verify the performance of a drive.
test32() {
    local off opts=() size

    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+=("--thread=1" "--time_based" "--runtime=30")
    opts+=("--max_open_zones=$max_open_zones" "--zonemode=zbd")
    opts+=("--zonesize=${zone_size}")
    run_fio "${opts[@]}" >> "${logfile}.${test_number}" 2>&1 || return $?
}

# Write to sequential zones with a block size that is not a divisor of the
# zone size.
test33() {
    local bs io_size size

    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				\
		   >> "${logfile}.${test_number}" 2>&1 || return $?
    check_written $(((io_size + bs - 1) / bs * bs)) || return $?
}

# Write to sequential zones with a block size that is not a divisor of the
# zone size and with data verification enabled.
test34() {
    local size

    size=$((2 * zone_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}"
}

# Test 1/4 for the I/O boundary rounding code: $size < $zone_size.
test35() {
    local bs off io_size size

    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}"		    \
		    >> "${logfile}.${test_number}" 2>&1 && return 1
    grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
}

# Test 2/4 for the I/O boundary rounding code: $size < $zone_size.
test36() {
    local bs off io_size size

    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}"		    \
		    >> "${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

    if [ "$first_sequential_zone_sector" = 0 ]; then
	off=0
    else
	off=$(((first_sequential_zone_sector - 1) * 512))
    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}"		    \
		    >> "${logfile}.${test_number}" 2>&1
    check_written $((zone_size)) || return $?
}

# Test 4/4 for the I/O boundary rounding code: $offset > $disk_size - $zone_size
test38() {
    local bs off size

    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}"		    \
		    >> "${logfile}.${test_number}" 2>&1 && return 1
    grep -q 'io_size must be at least one zone' "${logfile}.${test_number}"
}

# Read one block from a block device.
read_one_block() {
    local bs

    bs=$((logical_block_size))
    run_one_fio_job --rw=read --ioengine=psync --bs=$bs --size=$bs "$@" 2>&1 |
	tee -a "${logfile}.${test_number}"
}

# Check whether fio accepts --zonemode=none for zoned block devices.
test39() {
    [ -n "$is_zbd" ] || return 0
    read_one_block --zonemode=none >/dev/null || return $?
    check_read $((logical_block_size)) || return $?
}

# Check whether fio accepts --zonemode=strided for zoned block devices.
test40() {
    local bs

    bs=$((logical_block_size))
    [ -n "$is_zbd" ] || return 0
    read_one_block --zonemode=strided |
	grep -q 'fio: --zonesize must be specified when using --zonemode=strided' ||
	return $?
    read_one_block --zonemode=strided --zonesize=$bs >/dev/null || return $?
    check_read $bs || return $?
}

# Check whether fio checks the zone size for zoned block devices.
test41() {
    [ -n "$is_zbd" ] || return 0
    read_one_block --zonemode=zbd --zonesize=$((2 * zone_size)) |
	grep -q 'job parameter zonesize.*does not match disk zone size'
}

# Check whether fio handles --zonesize=0 correctly for regular block devices.
test42() {
    [ -n "$is_zbd" ] && return 0
    read_one_block --zonemode=zbd --zonesize=0 |
	grep -q 'Specifying the zone size is mandatory for regular block devices with --zonemode=zbd'
}

# 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'
}

# Check whether fio handles --zonemode=none --zonesize=1 correctly.
test44() {
    read_one_block --zonemode=none --zonesize=1 |
	grep -q 'fio: --zonemode=none and --zonesize are not compatible'
}

test45() {
    local bs i

    [ -z "$is_zbd" ] && return 0
    bs=$((logical_block_size))
    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}" |
	grep -q "fio: first I/O failed. If .* is a zoned block device, consider --zonemode=zbd"
}

# Random write to sequential zones, libaio, 8 jobs, queue depth 64 per job
test46() {
    local size

    size=$((4 * zone_size))
    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

    [ -z "$is_zbd" ] && return 0
    bs=$((logical_block_size))
    run_one_fio_job --ioengine=psync --rw=write --bs=$bs \
		    --zonemode=zbd --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}"
}

tests=()
dynamic_analyzer=()
reset_all_zones=

while [ "${1#-}" != "$1" ]; do
  case "$1" in
    -d) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=drd"
			  "--show-confl-seg=no");
	shift;;
    -e) dynamic_analyzer=(valgrind "--read-var-info=yes" "--tool=helgrind");
	shift;;
    -r) reset_all_zones=1; shift;;
    -t) tests+=("$2"); shift; shift;;
    -v) dynamic_analyzer=(valgrind "--read-var-info=yes");
	shift;;
    --) shift; break;;
  esac
done

if [ $# != 1 ]; then
    usage
    exit 1
fi

# shellcheck source=functions
source "$(dirname "$0")/functions" || exit $?

dev=$1
realdev=$(readlink -f "$dev")
basename=$(basename "$realdev")
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 ! 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
	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
	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

if [ "${#tests[@]}" = 0 ]; then
    readarray -t tests < <(declare -F | grep "test[0-9]*" | \
				   tr -c -d "[:digit:]\n" | sort -n)
fi

logfile=$0.log

passed=0
failed=0
rc=0
for test_number in "${tests[@]}"; do
    rm -f "${logfile}.${test_number}"
    echo -n "Running test $test_number ... "
    if eval "test$test_number"; then
	status="PASS"
	((passed++))
    else
	status="FAIL"
	((failed++))
	rc=1
    fi
    echo "$status"
    echo "$status" >> "${logfile}.${test_number}"
done

echo "$passed tests passed"
if [ $failed -gt 0 ]; then
    echo " and $failed tests failed"
fi
exit $rc