-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathzbd.c
2265 lines (1941 loc) · 57.6 KB
/
zbd.c
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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Western Digital Corporation or its affiliates.
*
* This file is released under the GPL.
*/
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "compiler/compiler.h"
#include "os/os.h"
#include "file.h"
#include "fio.h"
#include "lib/pow2.h"
#include "log.h"
#include "oslib/asprintf.h"
#include "smalloc.h"
#include "verify.h"
#include "pshared.h"
#include "zbd.h"
static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
{
return (uint64_t)(offset - f->file_offset) < f->io_size;
}
static inline unsigned int zbd_zone_idx(const struct fio_file *f,
struct fio_zone_info *zone)
{
return zone - f->zbd_info->zone_info;
}
/**
* zbd_offset_to_zone_idx - convert an offset into a zone number
* @f: file pointer.
* @offset: offset in bytes. If this offset is in the first zone_size bytes
* past the disk size then the index of the sentinel is returned.
*/
static unsigned int zbd_offset_to_zone_idx(const struct fio_file *f,
uint64_t offset)
{
uint32_t zone_idx;
if (f->zbd_info->zone_size_log2 > 0)
zone_idx = offset >> f->zbd_info->zone_size_log2;
else
zone_idx = offset / f->zbd_info->zone_size;
return min(zone_idx, f->zbd_info->nr_zones);
}
/**
* zbd_zone_end - Return zone end location
* @z: zone info pointer.
*/
static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
{
return (z+1)->start;
}
/**
* zbd_zone_capacity_end - Return zone capacity limit end location
* @z: zone info pointer.
*/
static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
{
return z->start + z->capacity;
}
/**
* zbd_zone_remainder - Return the number of bytes that are still available for
* writing before the zone gets full
* @z: zone info pointer.
*/
static inline uint64_t zbd_zone_remainder(struct fio_zone_info *z)
{
if (z->wp >= zbd_zone_capacity_end(z))
return 0;
return zbd_zone_capacity_end(z) - z->wp;
}
/**
* zbd_zone_full - verify whether a minimum number of bytes remain in a zone
* @f: file pointer.
* @z: zone info pointer.
* @required: minimum number of bytes that must remain in a zone.
*
* The caller must hold z->mutex.
*/
static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
uint64_t required)
{
assert((required & 511) == 0);
return z->has_wp && required > zbd_zone_remainder(z);
}
static void zone_lock(struct thread_data *td, const struct fio_file *f,
struct fio_zone_info *z)
{
#ifndef NDEBUG
unsigned int const nz = zbd_zone_idx(f, z);
/* A thread should never lock zones outside its working area. */
assert(f->min_zone <= nz && nz < f->max_zone);
assert(z->has_wp);
#endif
/*
* Lock the io_u target zone. The zone will be unlocked if io_u offset
* is changed or when io_u completes and zbd_put_io() executed.
* To avoid multiple jobs doing asynchronous I/Os from deadlocking each
* other waiting for zone locks when building an io_u batch, first
* only trylock the zone. If the zone is already locked by another job,
* process the currently queued I/Os so that I/O progress is made and
* zones unlocked.
*/
if (pthread_mutex_trylock(&z->mutex) != 0) {
if (!td_ioengine_flagged(td, FIO_SYNCIO))
io_u_quiesce(td);
pthread_mutex_lock(&z->mutex);
}
}
static inline void zone_unlock(struct fio_zone_info *z)
{
assert(z->has_wp);
pthread_mutex_unlock(&z->mutex);
}
static inline struct fio_zone_info *zbd_get_zone(const struct fio_file *f,
unsigned int zone_idx)
{
return &f->zbd_info->zone_info[zone_idx];
}
static inline struct fio_zone_info *
zbd_offset_to_zone(const struct fio_file *f, uint64_t offset)
{
return zbd_get_zone(f, zbd_offset_to_zone_idx(f, offset));
}
static bool accounting_vdb(struct thread_data *td, const struct fio_file *f)
{
return td->o.zrt.u.f && td_write(td);
}
/**
* zbd_get_zoned_model - Get a device zoned model
* @td: FIO thread data
* @f: FIO file for which to get model information
*/
static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
enum zbd_zoned_model *model)
{
int ret;
if (f->filetype == FIO_TYPE_PIPE) {
log_err("zonemode=zbd does not support pipes\n");
return -EINVAL;
}
/* If regular file, always emulate zones inside the file. */
if (f->filetype == FIO_TYPE_FILE) {
*model = ZBD_NONE;
return 0;
}
if (td->io_ops && td->io_ops->get_zoned_model)
ret = td->io_ops->get_zoned_model(td, f, model);
else
ret = blkzoned_get_zoned_model(td, f, model);
if (ret < 0) {
td_verror(td, errno, "get zoned model failed");
log_err("%s: get zoned model failed (%d).\n",
f->file_name, errno);
}
return ret;
}
/**
* zbd_report_zones - Get zone information
* @td: FIO thread data.
* @f: FIO file for which to get zone information
* @offset: offset from which to report zones
* @zones: Array of struct zbd_zone
* @nr_zones: Size of @zones array
*
* Get zone information into @zones starting from the zone at offset @offset
* for the device specified by @f.
*
* Returns the number of zones reported upon success and a negative error code
* upon failure. If the zone report is empty, always assume an error (device
* problem) and return -EIO.
*/
static int zbd_report_zones(struct thread_data *td, struct fio_file *f,
uint64_t offset, struct zbd_zone *zones,
unsigned int nr_zones)
{
int ret;
if (td->io_ops && td->io_ops->report_zones)
ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
else
ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
if (ret < 0) {
td_verror(td, errno, "report zones failed");
log_err("%s: report zones from sector %"PRIu64" failed (nr_zones=%d; errno=%d).\n",
f->file_name, offset >> 9, nr_zones, errno);
} else if (ret == 0) {
td_verror(td, errno, "Empty zone report");
log_err("%s: report zones from sector %"PRIu64" is empty.\n",
f->file_name, offset >> 9);
ret = -EIO;
}
return ret;
}
/**
* zbd_reset_wp - reset the write pointer of a range of zones
* @td: FIO thread data.
* @f: FIO file for which to reset zones
* @offset: Starting offset of the first zone to reset
* @length: Length of the range of zones to reset
*
* Reset the write pointer of all zones in the range @offset...@offset+@length.
* Returns 0 upon success and a negative error code upon failure.
*/
static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
uint64_t offset, uint64_t length)
{
int ret;
if (td->io_ops && td->io_ops->reset_wp)
ret = td->io_ops->reset_wp(td, f, offset, length);
else
ret = blkzoned_reset_wp(td, f, offset, length);
if (ret < 0) {
td_verror(td, errno, "resetting wp failed");
log_err("%s: resetting wp for %"PRIu64" sectors at sector %"PRIu64" failed (%d).\n",
f->file_name, length >> 9, offset >> 9, errno);
}
return ret;
}
/**
* __zbd_reset_zone - reset the write pointer of a single zone
* @td: FIO thread data.
* @f: FIO file associated with the disk for which to reset a write pointer.
* @z: Zone to reset.
*
* Returns 0 upon success and a negative error code upon failure.
*
* The caller must hold z->mutex.
*/
static int __zbd_reset_zone(struct thread_data *td, struct fio_file *f,
struct fio_zone_info *z)
{
uint64_t offset = z->start;
uint64_t length = (z+1)->start - offset;
uint64_t data_in_zone = z->wp - z->start;
int ret = 0;
if (!data_in_zone)
return 0;
assert(is_valid_offset(f, offset + length - 1));
dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
f->file_name, zbd_zone_idx(f, z));
switch (f->zbd_info->model) {
case ZBD_HOST_AWARE:
case ZBD_HOST_MANAGED:
ret = zbd_reset_wp(td, f, offset, length);
if (ret < 0)
return ret;
break;
default:
break;
}
if (accounting_vdb(td, f)) {
pthread_mutex_lock(&f->zbd_info->mutex);
f->zbd_info->wp_valid_data_bytes -= data_in_zone;
pthread_mutex_unlock(&f->zbd_info->mutex);
}
z->wp = z->start;
td->ts.nr_zone_resets++;
return ret;
}
/**
* zbd_write_zone_put - Remove a zone from the write target zones array.
* @td: FIO thread data.
* @f: FIO file that has the write zones array to remove.
* @zone_idx: Index of the zone to remove.
*
* The caller must hold f->zbd_info->mutex.
*/
static void zbd_write_zone_put(struct thread_data *td, const struct fio_file *f,
struct fio_zone_info *z)
{
uint32_t zi;
if (!z->write)
return;
for (zi = 0; zi < f->zbd_info->num_write_zones; zi++) {
if (zbd_get_zone(f, f->zbd_info->write_zones[zi]) == z)
break;
}
if (zi == f->zbd_info->num_write_zones)
return;
dprint(FD_ZBD, "%s: removing zone %u from write zone array\n",
f->file_name, zbd_zone_idx(f, z));
memmove(f->zbd_info->write_zones + zi,
f->zbd_info->write_zones + zi + 1,
(ZBD_MAX_WRITE_ZONES - (zi + 1)) *
sizeof(f->zbd_info->write_zones[0]));
f->zbd_info->num_write_zones--;
td->num_write_zones--;
z->write = 0;
}
/**
* zbd_reset_zone - reset the write pointer of a single zone and remove the zone
* from the array of write zones.
* @td: FIO thread data.
* @f: FIO file associated with the disk for which to reset a write pointer.
* @z: Zone to reset.
*
* Returns 0 upon success and a negative error code upon failure.
*
* The caller must hold z->mutex.
*/
static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
struct fio_zone_info *z)
{
int ret;
ret = __zbd_reset_zone(td, f, z);
if (ret)
return ret;
pthread_mutex_lock(&f->zbd_info->mutex);
zbd_write_zone_put(td, f, z);
pthread_mutex_unlock(&f->zbd_info->mutex);
return 0;
}
/**
* zbd_finish_zone - finish the specified zone
* @td: FIO thread data.
* @f: FIO file for which to finish a zone
* @z: Zone to finish.
*
* Finish the zone at @offset with open or close status.
*/
static int zbd_finish_zone(struct thread_data *td, struct fio_file *f,
struct fio_zone_info *z)
{
uint64_t offset = z->start;
uint64_t length = f->zbd_info->zone_size;
int ret = 0;
switch (f->zbd_info->model) {
case ZBD_HOST_AWARE:
case ZBD_HOST_MANAGED:
if (td->io_ops && td->io_ops->finish_zone)
ret = td->io_ops->finish_zone(td, f, offset, length);
else
ret = blkzoned_finish_zone(td, f, offset, length);
break;
default:
break;
}
if (ret < 0) {
td_verror(td, errno, "finish zone failed");
log_err("%s: finish zone at sector %"PRIu64" failed (%d).\n",
f->file_name, offset >> 9, errno);
} else {
z->wp = (z+1)->start;
}
return ret;
}
/**
* zbd_reset_zones - Reset a range of zones.
* @td: fio thread data.
* @f: fio file for which to reset zones
* @zb: first zone to reset.
* @ze: first zone not to reset.
*
* Returns 0 upon success and 1 upon failure.
*/
static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
struct fio_zone_info *const zb,
struct fio_zone_info *const ze)
{
struct fio_zone_info *z;
const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
int res = 0;
if (fio_unlikely(0 == min_bs))
return 1;
dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
f->file_name, zbd_zone_idx(f, zb), zbd_zone_idx(f, ze));
for (z = zb; z < ze; z++) {
if (!z->has_wp)
continue;
zone_lock(td, f, z);
if (z->wp != z->start) {
dprint(FD_ZBD, "%s: resetting zone %u\n",
f->file_name, zbd_zone_idx(f, z));
if (zbd_reset_zone(td, f, z) < 0)
res = 1;
}
zone_unlock(z);
}
return res;
}
/**
* zbd_get_max_open_zones - Get the maximum number of open zones
* @td: FIO thread data
* @f: FIO file for which to get max open zones
* @max_open_zones: Upon success, result will be stored here.
*
* A @max_open_zones value set to zero means no limit.
*
* Returns 0 upon success and a negative error code upon failure.
*/
static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
unsigned int *max_open_zones)
{
int ret;
if (td->io_ops && td->io_ops->get_max_open_zones)
ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
else
ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
if (ret < 0) {
td_verror(td, errno, "get max open zones failed");
log_err("%s: get max open zones failed (%d).\n",
f->file_name, errno);
}
return ret;
}
/**
* zbd_get_max_active_zones - Get the maximum number of active zones
* @td: FIO thread data
* @f: FIO file for which to get max active zones
*
* Returns max_active_zones limit value of the target file if it is available.
* Otherwise return zero, which means no limit.
*/
static unsigned int zbd_get_max_active_zones(struct thread_data *td,
struct fio_file *f)
{
unsigned int max_active_zones;
int ret;
if (td->io_ops && td->io_ops->get_max_active_zones)
ret = td->io_ops->get_max_active_zones(td, f,
&max_active_zones);
else
ret = blkzoned_get_max_active_zones(td, f, &max_active_zones);
if (ret < 0) {
dprint(FD_ZBD, "%s: max_active_zones is not available\n",
f->file_name);
return 0;
}
return max_active_zones;
}
/**
* __zbd_write_zone_get - Add a zone to the array of write zones.
* @td: fio thread data.
* @f: fio file that has the write zones array to add.
* @zone_idx: Index of the zone to add.
*
* Do same operation as @zbd_write_zone_get, except it adds the zone at
* @zone_idx to write target zones array even when it does not have remainder
* space to write one block.
*/
static bool __zbd_write_zone_get(struct thread_data *td,
const struct fio_file *f,
struct fio_zone_info *z)
{
struct zoned_block_device_info *zbdi = f->zbd_info;
uint32_t zone_idx = zbd_zone_idx(f, z);
bool res = true;
if (z->cond == ZBD_ZONE_COND_OFFLINE)
return false;
/*
* Skip full zones with data verification enabled because resetting a
* zone causes data loss and hence causes verification to fail.
*/
if (td->o.verify != VERIFY_NONE && zbd_zone_remainder(z) == 0)
return false;
/*
* zbdi->max_write_zones == 0 means that there is no limit on the
* maximum number of write target zones. In this case, do no track write
* target zones in zbdi->write_zones array.
*/
if (!zbdi->max_write_zones)
return true;
pthread_mutex_lock(&zbdi->mutex);
if (z->write) {
/*
* If the zone is going to be completely filled by writes
* already in-flight, handle it as a full zone instead of a
* write target zone.
*/
if (!zbd_zone_remainder(z))
res = false;
goto out;
}
res = false;
/* Zero means no limit */
if (td->o.job_max_open_zones > 0 &&
td->num_write_zones >= td->o.job_max_open_zones)
goto out;
if (zbdi->num_write_zones >= zbdi->max_write_zones)
goto out;
dprint(FD_ZBD, "%s: adding zone %u to write zone array\n",
f->file_name, zone_idx);
zbdi->write_zones[zbdi->num_write_zones++] = zone_idx;
td->num_write_zones++;
z->write = 1;
res = true;
out:
pthread_mutex_unlock(&zbdi->mutex);
return res;
}
/**
* zbd_write_zone_get - Add a zone to the array of write zones.
* @td: fio thread data.
* @f: fio file that has the open zones to add.
* @zone_idx: Index of the zone to add.
*
* Add a ZBD zone to write target zones array, if it is not yet added. Returns
* true if either the zone was already added or if the zone was successfully
* added to the array without exceeding the maximum number of write zones.
* Returns false if the zone was not already added and addition of the zone
* would cause the zone limit to be exceeded.
*/
static bool zbd_write_zone_get(struct thread_data *td, const struct fio_file *f,
struct fio_zone_info *z)
{
const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
/*
* Skip full zones with data verification enabled because resetting a
* zone causes data loss and hence causes verification to fail.
*/
if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
return false;
return __zbd_write_zone_get(td, f, z);
}
/* Verify whether direct I/O is used for all host-managed zoned block drives. */
static bool zbd_using_direct_io(void)
{
struct fio_file *f;
int j;
for_each_td(td) {
if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
continue;
for_each_file(td, f, j) {
if (f->zbd_info && f->filetype == FIO_TYPE_BLOCK &&
f->zbd_info->model == ZBD_HOST_MANAGED)
return false;
}
} end_for_each();
return true;
}
/* Whether or not the I/O range for f includes one or more sequential zones */
static bool zbd_is_seq_job(const struct fio_file *f)
{
uint32_t zone_idx, zone_idx_b, zone_idx_e;
assert(f->zbd_info);
if (f->io_size == 0)
return false;
zone_idx_b = zbd_offset_to_zone_idx(f, f->file_offset);
zone_idx_e =
zbd_offset_to_zone_idx(f, f->file_offset + f->io_size - 1);
for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
if (zbd_get_zone(f, zone_idx)->has_wp)
return true;
return false;
}
/*
* Verify whether the file offset and size parameters are aligned with zone
* boundaries. If the file offset is not aligned, align it down to the start of
* the zone containing the start offset and align up the file io_size parameter.
*/
static bool zbd_zone_align_file_sizes(struct thread_data *td,
struct fio_file *f)
{
const struct fio_zone_info *z;
uint64_t new_offset, new_end;
if (!f->zbd_info)
return true;
if (f->file_offset >= f->real_file_size)
return true;
if (!zbd_is_seq_job(f))
return true;
if (!td->o.zone_size) {
td->o.zone_size = f->zbd_info->zone_size;
if (!td->o.zone_size) {
log_err("%s: invalid 0 zone size\n",
f->file_name);
return false;
}
} else if (td->o.zone_size != f->zbd_info->zone_size) {
log_err("%s: zonesize %llu does not match the device zone size %"PRIu64".\n",
f->file_name, td->o.zone_size,
f->zbd_info->zone_size);
return false;
}
if (td->o.zone_skip % td->o.zone_size) {
log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
f->file_name, td->o.zone_skip,
td->o.zone_size);
return false;
}
if (td->o.td_ddir == TD_DDIR_READ) {
z = zbd_offset_to_zone(f, f->file_offset + f->io_size);
new_end = z->start;
if (f->file_offset + f->io_size > new_end) {
log_info("%s: rounded io_size from %"PRIu64" to %"PRIu64"\n",
f->file_name, f->io_size,
new_end - f->file_offset);
f->io_size = new_end - f->file_offset;
}
return true;
}
z = zbd_offset_to_zone(f, f->file_offset);
if (f->file_offset != z->start) {
new_offset = zbd_zone_end(z);
if (new_offset >= f->file_offset + f->io_size) {
log_info("%s: io_size must be at least one zone\n",
f->file_name);
return false;
}
log_info("%s: rounded up offset from %"PRIu64" to %"PRIu64"\n",
f->file_name, f->file_offset,
new_offset);
f->io_size -= (new_offset - f->file_offset);
f->file_offset = new_offset;
}
z = zbd_offset_to_zone(f, f->file_offset + f->io_size);
new_end = z->start;
if (f->file_offset + f->io_size != new_end) {
if (new_end <= f->file_offset) {
log_info("%s: io_size must be at least one zone\n",
f->file_name);
return false;
}
log_info("%s: rounded down io_size from %"PRIu64" to %"PRIu64"\n",
f->file_name, f->io_size,
new_end - f->file_offset);
f->io_size = new_end - f->file_offset;
}
return true;
}
/*
* Verify whether offset and size parameters are aligned with zone boundaries.
*/
static bool zbd_verify_sizes(void)
{
struct fio_file *f;
int j;
for_each_td(td) {
for_each_file(td, f, j) {
if (!zbd_zone_align_file_sizes(td, f))
return false;
}
} end_for_each();
return true;
}
static bool zbd_verify_bs(void)
{
struct fio_file *f;
int j;
for_each_td(td) {
if (td_trim(td) &&
(td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
td->o.bssplit_nr[DDIR_TRIM])) {
log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
return false;
}
for_each_file(td, f, j) {
uint64_t zone_size;
if (!f->zbd_info)
continue;
zone_size = f->zbd_info->zone_size;
if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
log_info("%s: trim block size %llu is not the zone size %"PRIu64"\n",
f->file_name, td->o.bs[DDIR_TRIM],
zone_size);
return false;
}
}
} end_for_each();
return true;
}
static int ilog2(uint64_t i)
{
int log = -1;
while (i) {
i >>= 1;
log++;
}
return log;
}
/*
* Initialize f->zbd_info for devices that are not zoned block devices. This
* allows to execute a ZBD workload against a non-ZBD device.
*/
static int init_zone_info(struct thread_data *td, struct fio_file *f)
{
uint32_t nr_zones;
struct fio_zone_info *p;
uint64_t zone_size = td->o.zone_size;
uint64_t zone_capacity = td->o.zone_capacity;
struct zoned_block_device_info *zbd_info = NULL;
int i;
if (zone_size == 0) {
log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
f->file_name);
return 1;
}
if (zone_size < 512) {
log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
f->file_name);
return 1;
}
if (zone_capacity == 0)
zone_capacity = zone_size;
if (zone_capacity > zone_size) {
log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
f->file_name, td->o.zone_capacity, td->o.zone_size);
return 1;
}
if (f->real_file_size < zone_size) {
log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
f->file_name, f->real_file_size, zone_size);
return -EINVAL;
}
nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
zbd_info = scalloc(1, sizeof(*zbd_info) +
(nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
if (!zbd_info)
return -ENOMEM;
mutex_init_pshared(&zbd_info->mutex);
zbd_info->refcount = 1;
p = &zbd_info->zone_info[0];
for (i = 0; i < nr_zones; i++, p++) {
mutex_init_pshared_with_type(&p->mutex,
PTHREAD_MUTEX_RECURSIVE);
p->start = i * zone_size;
p->wp = p->start;
p->type = ZBD_ZONE_TYPE_SWR;
p->cond = ZBD_ZONE_COND_EMPTY;
p->capacity = zone_capacity;
p->has_wp = 1;
}
/* a sentinel */
p->start = nr_zones * zone_size;
f->zbd_info = zbd_info;
f->zbd_info->zone_size = zone_size;
f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
ilog2(zone_size) : 0;
f->zbd_info->nr_zones = nr_zones;
return 0;
}
/*
* Maximum number of zones to report in one operation.
*/
#define ZBD_REPORT_MAX_ZONES 8192U
/*
* Parse the device zone report and store it in f->zbd_info. Must be called
* only for devices that are zoned, namely those with a model != ZBD_NONE.
*/
static int parse_zone_info(struct thread_data *td, struct fio_file *f)
{
int nr_zones, nrz;
struct zbd_zone *zones, *z;
struct fio_zone_info *p;
uint64_t zone_size, offset, capacity;
bool same_zone_cap = true;
struct zoned_block_device_info *zbd_info = NULL;
int i, j, ret = -ENOMEM;
zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
if (!zones)
goto out;
nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
if (nrz < 0) {
ret = nrz;
log_info("fio: report zones (offset 0) failed for %s (%d).\n",
f->file_name, -ret);
goto out;
}
zone_size = zones[0].len;
capacity = zones[0].capacity;
nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
if (td->o.zone_size == 0) {
td->o.zone_size = zone_size;
} else if (td->o.zone_size != zone_size) {
log_err("fio: %s job parameter zonesize %llu does not match disk zone size %"PRIu64".\n",
f->file_name, td->o.zone_size, zone_size);
ret = -EINVAL;
goto out;
}
dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
f->file_name, nr_zones, zone_size / 1024);
zbd_info = scalloc(1, sizeof(*zbd_info) +
(nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
if (!zbd_info)
goto out;
mutex_init_pshared(&zbd_info->mutex);
zbd_info->refcount = 1;
p = &zbd_info->zone_info[0];
for (offset = 0, j = 0; j < nr_zones;) {
z = &zones[0];
for (i = 0; i < nrz; i++, j++, z++, p++) {
mutex_init_pshared_with_type(&p->mutex,
PTHREAD_MUTEX_RECURSIVE);
p->start = z->start;
p->capacity = z->capacity;
if (capacity != z->capacity)
same_zone_cap = false;
switch (z->cond) {
case ZBD_ZONE_COND_NOT_WP:
case ZBD_ZONE_COND_FULL:
p->wp = p->start + p->capacity;
break;
default:
assert(z->start <= z->wp);
assert(z->wp <= z->start + zone_size);
p->wp = z->wp;
break;
}
switch (z->type) {
case ZBD_ZONE_TYPE_SWR:
p->has_wp = 1;
break;
default:
p->has_wp = 0;
}
p->type = z->type;
p->cond = z->cond;
if (j > 0 && p->start != p[-1].start + zone_size) {
log_info("%s: invalid zone data [%d:%d]: %"PRIu64" + %"PRIu64" != %"PRIu64"\n",
f->file_name, j, i,
p[-1].start, zone_size, p->start);
ret = -EINVAL;
goto out;
}
}
z--;
offset = z->start + z->len;
if (j >= nr_zones)
break;
nrz = zbd_report_zones(td, f, offset, zones,
min((uint32_t)(nr_zones - j),
ZBD_REPORT_MAX_ZONES));
if (nrz < 0) {
ret = nrz;
log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
offset, f->file_name, -ret);
goto out;
}
}
/* a sentinel */
zbd_info->zone_info[nr_zones].start = offset;
f->zbd_info = zbd_info;
f->zbd_info->zone_size = zone_size;
f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
ilog2(zone_size) : 0;
f->zbd_info->nr_zones = nr_zones;
f->zbd_info->max_active_zones = zbd_get_max_active_zones(td, f);
if (same_zone_cap)
dprint(FD_ZBD, "Zone capacity = %"PRIu64" KB\n",
capacity / 1024);
zbd_info = NULL;
ret = 0;
out:
sfree(zbd_info);
free(zones);
return ret;
}
static int zbd_set_max_write_zones(struct thread_data *td, struct fio_file *f)
{
struct zoned_block_device_info *zbd = f->zbd_info;
unsigned int max_open_zones;
int ret;
if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
/* Only host-managed devices have a max open limit */
zbd->max_write_zones = td->o.max_open_zones;
goto out;
}
/* If host-managed, get the max open limit */
ret = zbd_get_max_open_zones(td, f, &max_open_zones);
if (ret)
return ret;
if (!max_open_zones) {
/* No device limit */