-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdtread.c
1520 lines (1414 loc) · 52.5 KB
/
dtread.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) 1988 - 2023 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: dtread.c
* Author: Robin T. Miller
*
* Description:
* Read routines for generic data test program.
*
* Modification History:
*
* September 20th, 2023 by Robin T. Miller
* For all random access devices, limit the data read to what was
* written. Previously this was enabled only for file systems, but it's
* possible for direct acces disks to create a premature end of data
* condition. For example during retries the disk driver may fail Read
* Capacity during error recovery/disk discovery returning an ENOSPC.
* I believe thinly provisioned disks may also return ENOSPC when
* there's insufficient backend disk space (over provisioned).
*
* August 5th, 2021 by Robin T. Miller
* Added support for NVMe disks.
*
* March 21st, 2021 by Robin T. Miller
* Add support for forcing FALSE data corruptiong for debugging.
*
* May 5th, 2020 by Robin T. Miller
* Use high resolution timer for more accurate I/O timing. This is
* implemented on Windows, but Unix systems still use gettimeofday() API.
*
* March 7th, 2020 by Robin T. Miller
* Apply new logic in FindCapacity() to properly handle file position.
*
* March 6th, 2020 by Robin T. Miller
* Update SetupCapacityPercentage() to better handle slices with a file
* position and capacity percentage, Starting offset is for mixed FS/disk test.
*
* May 28th, 2019 by Robin T. Miller
* Don't adjust offset when read error occurs (count is -1), since this
* causes the wrong offset when we've specified an error limit.
*
* May 27th, 2019 by Robin T. Miller
* Add support for capacity percentage. This is to help exceeding backend
* storage when volumes are over-provisioned (thin provisioned LUNs).
*
* December 28th, 2017 by Robin T. Miller
* Added support for multiple threads via I/O lock and shared data.
*
* September 1st, 2017 by Robin T. Miller
* Add support for random read percentage only.
*
* December 21st, 2016 by Robin T. Miller
* Only use pread() for random access devices, and normal read() for
* other device types such as pipes or tapes. This update allows dt to read
* from stdin ('if=-') for verifying data from other tools or itself across
* network sockets, etc. Trying to keep dt general purpose! ;)
*
* November 4th, 2016 by Robin T. Miller
* Add support for random percentages.
*
* February 5th, 2016 by Robin T. Miller
* Update read_record() to initialize the buffer prior to reads.
* This helps diagnose when the actual read data is NOT returned.
* Currently, the previosu read data is reported, can be misleading.
*
* December 13th, 2015 by Robin T. Miller
* Switch from incr_position() to set_position() now that we are
* using pread() API, and the internal file offset is *not* updated.
* Note: This goes away altogether once we cleanup get_position() usage.
* Without this change, the step= option was broken. :-(
*
* June 13th, 2015 by Robin T. Miller
* Update verify_record() to use the data buffer rather than the
* pattern buffer for verifying source device data. This is required for
* block tags (btags), but also to prevent misleading corruption reporting.
*
* June 9th, 2015 by Robin T. Miller
* Added support for block tags (btags).
*
* February 5th, 2015 by Robin T. Miller
* Add file locking support.
*
* September 23rd, 2014 by Robin T. Miller
* In check_read(), report errors for short reads with random I/O,
* to avoid false data corruptions. Also sanity check the updated file
* descriptor offset after short reads, again to avoid false corruption.
* Solaris VM w/ESX on NFS is returning short reads and writes, and for
* reads my analysis indicates the fd offset is incorrect for short read!
* The offset was updated with the request size, leading to wrong data!
*
* March 20th, 2014, by Robin T. Miller
* In read_record() see if we're in read or write mode, since this API
* is used during write mode when doing read-after-write (enable=raw). Also,
* add a file position argument, which we need for an accurate AIO history.
* We do not wish to use di_offset, since this points to the next offset.
*
* June 20th, 2013 by Robin T Miller
* Mostly a rewrite for multithreaded IO, so starting with new history!
*/
#include "dt.h"
#if !defined(_QNX_SOURCE) && !defined(WIN32)
# include <sys/file.h>
#endif /* !defined(_QNX_SOURCE) && !defined(WIN32) */
#include <sys/stat.h>
/*
* Forward References:
*/
int read_data_iolock(struct dinfo *dip);
large_t SetupCapacityPercentage(dinfo_t *dip, large_t bytes);
/* ---------------------------------------------------------------------- */
int
check_last_write_info(dinfo_t *dip, Offset_t offset, size_t bsize, size_t dsize)
{
int status = SUCCESS;
/*
* SANITY CHECK: Make sure the read offset matches the last write offset.
* Would rather die now, than report a false data corruption!
*/
if ( (dip->di_last_write_offset != offset) ||
((ssize_t)dip->di_last_write_size > 0) && (dip->di_last_write_size != bsize) ) {
/* Check for partial read request for matching last write offset! */
if ( dip->di_last_write_offset == (offset + (Offset_t)bsize) ) {
return(status);
}
ReportErrorNumber(dip);
Fprintf(dip, "Programming ERROR: Incorrect I/O offset or size for last write!\n");
Fprintf(dip, "Expected (write) offset: " FUF ", attempted: %d, actual: %d\n",
dip->di_last_write_offset, (int)dip->di_last_write_attempted,
(int)dip->di_last_write_size);
Fprintf(dip, " Current (read) offset: " FUF ", attempting: %d, actual: %d\n",
offset, (int)dsize, (int)bsize);
if (dip->di_history_size) {
dump_history_data(dip);
}
status = FAILURE;
}
return (status);
}
/************************************************************************
* *
* read_data() - Read and optionally verify data read. *
* *
* Inputs: dip = The device information pointer. *
* *
* Outputs: Returns SUCCESS/FAILURE = Ok/Error. *
* *
************************************************************************/
int
read_data(struct dinfo *dip)
{
dinfo_t *odip = dip->di_output_dinfo; /* For copy/verify modes. */
#if defined(DT_IOLOCK)
io_global_data_t *iogp = dip->di_job->ji_opaque;
#endif
register ssize_t count;
register size_t bsize, dsize;
large_t data_limit;
Offset_t sequential_offset;
int status = SUCCESS;
struct dtfuncs *dtf = dip->di_funcs;
Offset_t lock_offset = 0;
hbool_t lock_full_range = False;
hbool_t check_rwbytes = False;
hbool_t check_write_limit = False;
lbdata_t lba;
iotype_t iotype = dip->di_io_type;
uint32_t loop_usecs;
struct timeval loop_start_time, loop_end_time;
int probability_random = 0;
int random_percentage = (dip->di_random_rpercentage) ? dip->di_random_rpercentage : dip->di_random_percentage;
#if defined(DT_IOLOCK)
/* Note: Temporary until we define a new I/O behavior! */
if (iogp) {
return( read_data_iolock(dip) );
}
#endif /* defined(DT_IOLOCK) */
dsize = get_data_size(dip, READ_OP);
data_limit = get_data_limit(dip);
if (dip->di_random_access) {
if ( (dip->di_io_type == SEQUENTIAL_IO) && (dip->di_io_dir == REVERSE) ) {
dip->di_offset = set_position(dip, (Offset_t)dip->di_rdata_limit, False);
if (odip) {
odip->di_offset = set_position(odip, (Offset_t)odip->di_rdata_limit, False);
}
}
lba = get_lba(dip);
sequential_offset = dip->di_offset = get_position(dip);
if (odip && odip->di_random_access) {
odip->di_offset = get_position(odip);
}
} else {
lba = make_lbdata(dip, dip->di_offset);
}
if ( dip->di_last_fbytes_written && dip->di_random_access ) {
if ( dip->di_files_read == (dip->di_last_files_written - 1) ) {
check_write_limit = True;
if (dip->di_eDebugFlag) {
Printf(dip, "DEBUG: Limiting data read on file #%d to " FUF " bytes from last written.\n",
(dip->di_files_read + 1), dip->di_last_fbytes_written);
}
}
}
/* Prime the common btag data, except for IOT pattern. */
if ( (dip->di_btag_flag == True) && (dip->di_iot_pattern == False) ) {
update_btag(dip, dip->di_btag, dip->di_offset,
(uint32_t)0, (size_t)0, (dip->di_records_read + 1));
}
if ( (dip->di_lock_files == True) && dt_test_lock_mode(dip, LOCK_RANGE_FULL) ) {
lock_full_range = True;
lock_offset = dip->di_offset;
status = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_READ, lock_offset, (Offset_t)data_limit);
if (status == FAILURE) return(status);
}
if (dip->di_iops && (dip->di_iops_type == IOPS_MEASURE_EXACT) ) {
dip->di_actual_total_usecs = 0;
dip->di_target_total_usecs = 0;
}
/*
* Now read and optionally verify the input records.
*/
while ( (dip->di_error_count < dip->di_error_limit) &&
(dip->di_fbytes_read < data_limit) &&
(dip->di_records_read < dip->di_record_limit) ) {
PAUSE_THREAD(dip);
if ( THREAD_TERMINATING(dip) ) break;
if (dip->di_terminating) break;
if (dip->di_iops && (dip->di_iops_type == IOPS_MEASURE_EXACT) ) {
highresolutiontime(&loop_start_time, NULL);
if (dip->di_records_read) {
/* Adjust the actual usecs to adjust for possible usleep below! */
dip->di_actual_total_usecs += timer_diff(&loop_end_time, &loop_start_time);
}
}
if ( dip->di_max_data && (dip->di_maxdata_read >= dip->di_max_data) ) {
dip->di_maxdata_reached = True;
break;
}
if ( dip->di_volumes_flag &&
(dip->di_multi_volume >= dip->di_volume_limit) &&
(dip->di_volume_records >= dip->di_volume_records)) {
break;
}
if (random_percentage) {
probability_random = (int)(get_random(dip) % 100);
if (probability_random < random_percentage) {
iotype = RANDOM_IO;
} else {
iotype = SEQUENTIAL_IO;
dip->di_offset = sequential_offset;
}
}
if (dip->di_read_delay) { /* Optional read delay. */
mySleep(dip, dip->di_read_delay);
}
/*
* If data limit was specified, ensure we don't exceed it.
*/
if ( (dip->di_fbytes_read + dsize) > data_limit) {
bsize = (size_t)(data_limit - dip->di_fbytes_read);
} else {
bsize = dsize;
}
if ( (iotype == SEQUENTIAL_IO) && (dip->di_io_dir == REVERSE) ) {
bsize = (size_t)MIN((dip->di_offset - dip->di_file_position), (Offset_t)bsize);
dip->di_offset = set_position(dip, (Offset_t)(dip->di_offset - bsize), False);
if (odip) {
odip->di_offset = set_position(odip, (Offset_t)(odip->di_offset - bsize), False);
}
} else if (iotype == RANDOM_IO) {
/*
* BEWARE: The size *must* match the write size, or you'll get
* a different offset, since the size is used in calculations.
*/
dip->di_offset = do_random(dip, True, bsize);
if (odip) {
odip->di_offset = dip->di_offset;
set_position(odip, odip->di_offset, False);
}
}
/*
* If we wrote data, ensure we don't read more than we wrote.
*/
if (check_write_limit) {
if ( (dip->di_fbytes_read + bsize) > dip->di_last_fbytes_written) {
dsize = bsize; /* Save the original intended size. */
bsize = (size_t)(dip->di_last_fbytes_written - dip->di_fbytes_read);
check_rwbytes = True;
if (bsize == (size_t) 0) {
set_Eof(dip);
break;
}
status = check_last_write_info(dip, dip->di_offset, bsize, dsize);
if (status == FAILURE) break;
}
}
if (dip->di_debug_flag && (bsize != dsize) && !dip->di_variable_flag) {
Printf (dip, "Record #%lu, Reading a partial record of %lu bytes...\n",
(dip->di_records_read + 1), bsize);
}
if (dip->di_iot_pattern || dip->di_lbdata_flag) {
lba = make_lbdata(dip, (Offset_t)(dip->di_volume_bytes + dip->di_offset));
}
/*
* If requested, rotate the data buffer through ROTATE_SIZE bytes
* to force various unaligned buffer accesses.
*/
if (dip->di_rotate_flag) {
dip->di_data_buffer = (dip->di_base_buffer + (dip->di_rotate_offset++ % ROTATE_SIZE));
}
/*
* If we'll be doing a data compare after the read, then
* fill the data buffer with the inverted pattern to ensure
* the buffer actually gets written into (driver debug mostly).
*/
if ( (dip->di_io_mode == TEST_MODE) && (dip->di_compare_flag == True) ) {
/* Note: Initializing the data buffer moved to read_record()! */
init_padbytes(dip->di_data_buffer, bsize, ~dip->di_pattern);
if (dip->di_iot_pattern) {
if (dip->di_btag) {
update_buffer_btags(dip, dip->di_btag, dip->di_offset,
dip->di_pattern_buffer, bsize, (dip->di_records_read + 1));
}
lba = init_iotdata(dip, dip->di_pattern_buffer, bsize, lba, dip->di_lbdata_size);
}
}
if (dip->di_Debug_flag) {
report_io(dip, READ_MODE, dip->di_data_buffer, bsize, dip->di_offset);
}
if ( (dip->di_lock_files == True) && (lock_full_range == False) ) {
lock_offset = dip->di_offset;
/* Lock a partial byte range! */
status = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_READ, lock_offset, (Offset_t)bsize);
if (status == FAILURE) break;
}
dip->di_retry_count = 0;
do {
count = read_record(dip, dip->di_data_buffer, bsize, dsize, dip->di_offset, &status);
} while (status == RETRYABLE);
if (dip->di_end_of_file) break; /* Stop reading at end of file. */
if (status == FAILURE) {
if (dip->di_error_count >= dip->di_error_limit) break;
} else if (dip->di_io_mode == COPY_MODE) {
ssize_t wcount = copy_record(odip, dip->di_data_buffer, count, odip->di_offset, &status);
/* TODO: Need to cleanup multiple device support! */
/* For now, propagate certain information to reader. */
if (odip->di_end_of_file) {
dip->di_end_of_file = odip->di_end_of_file;
if (dip->di_fsfile_flag) {
/* Note: Not trying to handle file system full, too messy! */
/* Failing at this point is a must, to avoid false corruptions! */
Eprintf(dip, "The file system is full, failing the copy operation!\n");
return(FAILURE);
} else {
break; /* For disks, stop I/O at end of media. */
}
}
if (status == FAILURE) { /* Write failed! */
dip->di_error_count++;
} else if (wcount != count) {
Wprintf(dip, "Partial write, write count %d < read count %d, failing!\n", wcount, count);
Eprintf(dip, "Partial writes are NOT supported, failing the copy operation!\n");
return(FAILURE);
}
if ( (dip->di_error_count >= dip->di_error_limit) || dip->di_end_of_file) break;
} else if (dip->di_io_mode == VERIFY_MODE) {
ssize_t rcount = verify_record(odip, dip->di_data_buffer, count, odip->di_offset, &status);
if (status == FAILURE) {
dip->di_error_count++;
} else if (odip->di_end_of_file) {
dip->di_end_of_file = odip->di_end_of_file;
}
if ( (dip->di_error_count >= dip->di_error_limit) || dip->di_end_of_file) break;
}
/*
* Verify the data (unless disabled).
*/
if ( (status != FAILURE) && dip->di_compare_flag && (dip->di_io_mode == TEST_MODE) ) {
ssize_t vsize = count;
status = (*dtf->tf_verify_data)(dip, dip->di_data_buffer, vsize, dip->di_pattern, &lba, False);
/*
* Verify the pad bytes (if enabled).
*/
if ( (status == SUCCESS) && dip->di_pad_check) {
(void) verify_padbytes(dip, dip->di_data_buffer, vsize, ~dip->di_pattern, bsize);
}
}
/*
* If we had a partial transfer, perhaps due to an error, adjust
* the logical block address in preparation for the next request.
*/
if ( (status != FAILURE) && dip->di_iot_pattern && ((size_t)count < bsize)) {
size_t resid = (bsize - count);
lba -= (lbdata_t)howmany((lbdata_t)resid, dip->di_lbdata_size);
}
/*
* For variable length records, adjust the next record size.
*/
if (dip->di_min_size) {
if (dip->di_variable_flag) {
dsize = get_variable(dip);
} else {
dsize += dip->di_incr_count;
if (dsize > dip->di_max_size) dsize = dip->di_min_size;
}
}
dip->di_records_read++;
dip->di_volume_records++;
if (dip->di_io_dir == FORWARD) {
if (count > 0) {
dip->di_offset += count; /* Maintain our own position too! */
if (odip) odip->di_offset += count;
}
} else if ( (iotype == SEQUENTIAL_IO) &&
(dip->di_offset == (Offset_t)dip->di_file_position) ) {
set_Eof(dip);
break;
}
if (dip->di_step_offset) {
if (dip->di_io_dir == FORWARD) {
dip->di_offset = set_position(dip, (dip->di_offset + dip->di_step_offset), True);
if (odip) odip->di_offset = set_position(dip, (odip->di_offset + odip->di_step_offset), True);
/* Linux returns EINVAL when seeking too far! */
if (dip->di_offset == (Offset_t)-1) {
set_Eof(dip);
break;
}
/* Note: See comments in dtwrite.c WRT this logic! */
if ( dip->di_slices &&
((dip->di_offset + (Offset_t)dsize) >= dip->di_end_position) ) {
set_Eof(dip);
break;
}
} else {
dip->di_offset -= dip->di_step_offset;
if (dip->di_offset <= (Offset_t)dip->di_file_position) {
set_Eof(dip);
break;
}
if (odip) {
odip->di_offset -= odip->di_step_offset;
if (odip->di_offset <= (Offset_t)odip->di_file_position) {
set_Eof(dip); /* Stop reading! */
break;
}
}
}
}
/*
* For regular files, if we've read as much as we've written,
* then set a fake EOF to stop this read pass.
*/
if ( check_rwbytes &&
(dip->di_fbytes_read == dip->di_last_fbytes_written) ) {
set_Eof(dip);
break;
}
/* Maintain our offset for sequential/random percentages. */
if (iotype == SEQUENTIAL_IO) {
sequential_offset = dip->di_offset;
}
if ( (dip->di_lock_files == True) && (lock_full_range == False) ) {
/* Unlock a partial byte range! */
status = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_UNLOCK, lock_offset, (Offset_t)bsize);
if (status == FAILURE) break;
}
/* For IOPS, track usecs and delay as necessary. */
if (dip->di_iops && (dip->di_iops_type == IOPS_MEASURE_EXACT) ) {
highresolutiontime(&loop_end_time, NULL);
loop_usecs = (uint32_t)timer_diff(&loop_start_time, &loop_end_time);
dip->di_target_total_usecs += dip->di_iops_usecs;
dip->di_actual_total_usecs += loop_usecs;
if (dip->di_target_total_usecs > dip->di_actual_total_usecs) {
unsigned int usecs = (unsigned int)(dip->di_target_total_usecs - dip->di_actual_total_usecs);
mySleep(dip, usecs);
}
}
}
if (lock_full_range == True) {
int rc = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_UNLOCK, lock_offset, (Offset_t)data_limit);
if (rc == FAILURE) status = rc;
}
return(status);
}
#if defined(DT_IOLOCK)
/************************************************************************
* *
* read_data_iolock() - Read and optionally verify data read. *
* *
* Description: *
* This function supports reading with multiple threads. *
* *
* Inputs: dip = The device information pointer. *
* *
* Outputs: Returns SUCCESS/FAILURE = Ok/Error. *
* *
************************************************************************/
int
read_data_iolock(struct dinfo *dip)
{
io_global_data_t *iogp = dip->di_job->ji_opaque;
register ssize_t count;
register size_t bsize, dsize;
large_t data_limit;
int status = SUCCESS;
struct dtfuncs *dtf = dip->di_funcs;
Offset_t lock_offset = 0;
hbool_t lock_full_range = False;
hbool_t check_rwbytes = False;
u_long io_record = 0;
lbdata_t lba;
iotype_t iotype = dip->di_io_type;
uint32_t loop_usecs;
struct timeval loop_start_time, loop_end_time;
int probability_random = 0;
int random_percentage = (dip->di_random_rpercentage) ? dip->di_random_rpercentage : dip->di_random_percentage;
dsize = get_data_size(dip, READ_OP);
data_limit = get_data_limit(dip);
(void)dt_acquire_iolock(dip, iogp);
if (dip->di_random_access) {
if (iogp->io_initialized == False) {
if ( (dip->di_io_type == SEQUENTIAL_IO) && (dip->di_io_dir == REVERSE) ) {
dip->di_offset = set_position(dip, (Offset_t)dip->di_rdata_limit, False);
}
lba = get_lba(dip);
iogp->io_starting_offset = iogp->io_sequential_offset = dip->di_offset = get_position(dip);
iogp->io_initialized = True;
} else {
lba = make_lbdata(dip, iogp->io_starting_offset);
}
} else {
lba = make_lbdata(dip, dip->di_offset);
iogp->io_starting_offset = iogp->io_sequential_offset = dip->di_offset;
}
(void)dt_release_iolock(dip, iogp);
/* Prime the common btag data, except for IOT pattern. */
if ( (dip->di_btag_flag == True) && (dip->di_iot_pattern == False) ) {
update_btag(dip, dip->di_btag, dip->di_offset, (uint32_t)0, (size_t)0, io_record);
}
if ( (dip->di_lock_files == True) && dt_test_lock_mode(dip, LOCK_RANGE_FULL) ) {
lock_full_range = True;
lock_offset = dip->di_offset;
status = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_READ, lock_offset, (Offset_t)data_limit);
if (status == FAILURE) return(status);
}
if (dip->di_iops && (dip->di_iops_type == IOPS_MEASURE_EXACT) ) {
dip->di_actual_total_usecs = 0;
dip->di_target_total_usecs = 0;
}
/*
* Now read and optionally verify the input records.
*/
while ( (iogp->io_end_of_file == False) &&
(dip->di_error_count < dip->di_error_limit) &&
(iogp->io_bytes_read < data_limit) &&
(iogp->io_records_read < dip->di_record_limit) ) {
PAUSE_THREAD(dip);
if ( THREAD_TERMINATING(dip) ) break;
if (dip->di_terminating) break;
if (dip->di_iops && (dip->di_iops_type == IOPS_MEASURE_EXACT) ) {
highresolutiontime(&loop_start_time, NULL);
if (dip->di_records_read) {
/* Adjust the actual usecs to adjust for possible usleep below! */
dip->di_actual_total_usecs += timer_diff(&loop_end_time, &loop_start_time);
}
}
if ( dip->di_max_data && (dip->di_maxdata_read >= dip->di_max_data) ) {
dip->di_maxdata_reached = True;
break;
}
if ( dip->di_volumes_flag &&
(dip->di_multi_volume >= dip->di_volume_limit) &&
(dip->di_volume_records >= dip->di_volume_records)) {
break;
}
(void)dt_acquire_iolock(dip, iogp);
/*
* Setup the random/sequential percentages (if enabled).
*/
if (random_percentage) {
probability_random = (int)(get_random(dip) % 100);
if (probability_random < random_percentage) {
iotype = RANDOM_IO;
} else {
iotype = SEQUENTIAL_IO;
dip->di_offset = iogp->io_sequential_offset;
}
}
if (dip->di_read_delay) { /* Optional read delay. */
mySleep(dip, dip->di_read_delay);
}
/*
* With multiple threads, we must check limits after unlocking.
*/
if ( (iogp->io_end_of_file == True) ||
(iogp->io_bytes_read >= data_limit) ||
(iogp->io_records_read >= dip->di_record_limit) ) {
set_Eof(dip);
iogp->io_end_of_file = dip->di_end_of_file;
(void)dt_release_iolock(dip, iogp);
break;
}
/*
* If data limit was specified, ensure we don't exceed it.
*/
if ( (iogp->io_bytes_read + dsize) > data_limit) {
bsize = (size_t)(data_limit - iogp->io_bytes_read);
} else {
bsize = dsize;
}
if (iotype == SEQUENTIAL_IO) {
dip->di_offset = iogp->io_sequential_offset;
if (dip->di_io_dir == REVERSE) {
bsize = (size_t)MIN((dip->di_offset - dip->di_file_position), (Offset_t)bsize);
dip->di_offset = set_position(dip, (Offset_t)(dip->di_offset - bsize), False);
iogp->io_sequential_offset = dip->di_offset;
} else {
iogp->io_sequential_offset += bsize;
}
} else if (iotype == RANDOM_IO) {
/*
* BEWARE: The size *must* match the write size, or you'll get
* a different offset, since the size is used in calculations.
*/
dip->di_offset = do_random(dip, True, bsize);
}
iogp->io_bytes_read += bsize;
iogp->io_records_read++;
io_record = iogp->io_records_read;
if ( (iotype == SEQUENTIAL_IO) && dip->di_step_offset) {
Offset_t offset = iogp->io_sequential_offset;
if (dip->di_io_dir == FORWARD) {
/* Note: Useful for debug, but we don't need to set position! */
offset = set_position(dip, (offset + dip->di_step_offset), True);
/* Linux returns EINVAL when seeking too far! */
if (offset == (Offset_t)-1) {
set_Eof(dip);
break;
}
/*
* This check prevents us from writing past the end of a slice.
* Note: Without slices, we expect to encounter end of file/media.
*/
if ( dip->di_slices &&
((offset + (Offset_t)dsize) >= dip->di_end_position) ) {
set_Eof(dip);
break;
}
} else { /* io_dir = REVERSE */
offset -= dip->di_step_offset;
if (offset <= (Offset_t) dip->di_file_position) {
set_Eof(dip);
dip->di_beginning_of_file = True;
break;
}
}
iogp->io_sequential_offset = offset;
}
(void)dt_release_iolock(dip, iogp);
if (dip->di_debug_flag && (bsize != dsize) && !dip->di_variable_flag) {
Printf(dip, "Record #%lu, Reading a partial record of %lu bytes...\n",
io_record, bsize);
}
if (dip->di_iot_pattern || dip->di_lbdata_flag) {
lba = make_lbdata(dip, (Offset_t)(dip->di_volume_bytes + dip->di_offset));
}
/*
* If requested, rotate the data buffer through ROTATE_SIZE bytes
* to force various unaligned buffer accesses.
*/
if (dip->di_rotate_flag) {
dip->di_data_buffer = (dip->di_base_buffer + (dip->di_rotate_offset++ % ROTATE_SIZE));
}
/*
* If we'll be doing a data compare after the read, then
* fill the data buffer with the inverted pattern to ensure
* the buffer actually gets written into (driver debug mostly).
*/
if ( (dip->di_io_mode == TEST_MODE) && (dip->di_compare_flag == True) ) {
/* Note: Initializing the data buffer moved to read_record()! */
init_padbytes(dip->di_data_buffer, bsize, ~dip->di_pattern);
if (dip->di_iot_pattern) {
if (dip->di_btag) {
update_buffer_btags(dip, dip->di_btag, dip->di_offset,
dip->di_pattern_buffer, bsize, io_record);
}
lba = init_iotdata(dip, dip->di_pattern_buffer, bsize, lba, dip->di_lbdata_size);
}
}
if (dip->di_Debug_flag) {
large_t iolba = make_lbdata(dip, dip->di_offset);
long files = (dip->di_files_read + 1);
report_record(dip, files, io_record,
iolba, dip->di_offset,
READ_MODE, dip->di_data_buffer, bsize);
}
if ( (dip->di_lock_files == True) && (lock_full_range == False) ) {
lock_offset = dip->di_offset;
/* Lock a partial byte range! */
status = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_READ, lock_offset, (Offset_t)bsize);
if (status == FAILURE) break;
}
dip->di_retry_count = 0;
do {
count = read_record(dip, dip->di_data_buffer, bsize, dsize, dip->di_offset, &status);
} while (status == RETRYABLE);
if (status == FAILURE) {
if (dip->di_error_count >= dip->di_error_limit) break;
}
/*
* Verify the data (unless disabled).
*/
if ( (status != FAILURE) && dip->di_compare_flag && (dip->di_io_mode == TEST_MODE) ) {
ssize_t vsize = count;
status = (*dtf->tf_verify_data)(dip, dip->di_data_buffer, vsize, dip->di_pattern, &lba, False);
/*
* Verify the pad bytes (if enabled).
*/
if ( (status == SUCCESS) && dip->di_pad_check) {
int rc = verify_padbytes(dip, dip->di_data_buffer, vsize, ~dip->di_pattern, bsize);
if (rc == FAILURE) status = rc;
}
}
/*
* If we had a partial transfer, perhaps due to an error, adjust
* the logical block address in preparation for the next request.
*/
if (dip->di_iot_pattern && ((size_t)count < bsize)) {
size_t resid = (bsize - count);
lba -= (lbdata_t)howmany((lbdata_t)resid, dip->di_lbdata_size);
}
/*
* For variable length records, adjust the next record size.
*/
if (dip->di_min_size) {
if (dip->di_variable_flag) {
dsize = get_variable(dip);
} else {
dsize += dip->di_incr_count;
if (dsize > dip->di_max_size) dsize = dip->di_min_size;
}
}
dip->di_records_read++;
dip->di_volume_records++;
if (dip->di_io_dir == FORWARD) {
dip->di_offset += count; /* Maintain our own position too! */
} else if ( (iotype == SEQUENTIAL_IO) &&
(dip->di_offset == (Offset_t)dip->di_file_position) ) {
set_Eof(dip);
break;
}
/*
* For regular files, if we've read as much as we've written,
* then set a fake EOF to stop this read pass.
*/
if ( check_rwbytes &&
(dip->di_fbytes_read == dip->di_last_fbytes_written) ) {
set_Eof(dip);
break;
}
if ( (dip->di_lock_files == True) && (lock_full_range == False) ) {
/* Unlock a partial byte range! */
status = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_UNLOCK, lock_offset, (Offset_t)bsize);
if (status == FAILURE) break;
}
/* For IOPS, track usecs and delay as necessary. */
if (dip->di_iops && (dip->di_iops_type == IOPS_MEASURE_EXACT) ) {
highresolutiontime(&loop_end_time, NULL);
loop_usecs = (uint32_t)timer_diff(&loop_start_time, &loop_end_time);
dip->di_target_total_usecs += dip->di_iops_usecs;
dip->di_actual_total_usecs += loop_usecs;
if (dip->di_target_total_usecs > dip->di_actual_total_usecs) {
unsigned int usecs = (unsigned int)(dip->di_target_total_usecs - dip->di_actual_total_usecs);
mySleep(dip, usecs);
}
}
}
/* Propagate end of file for other threads and outer loops. */
if (dip->di_end_of_file == False) {
set_Eof(dip);
}
iogp->io_end_of_file = dip->di_end_of_file;
if (lock_full_range == True) {
int rc = dt_lock_unlock(dip, dip->di_dname, &dip->di_fd,
LOCK_TYPE_UNLOCK, lock_offset, (Offset_t)data_limit);
if (rc == FAILURE) status = rc;
}
return(status);
}
#endif /* defined(DT_IOLOCK) */
/************************************************************************
* *
* check_read() - Check status of last read operation. *
* *
* Inputs: dip = The device information pointer. *
* count = Number of bytes read. *
* size = Number of bytes expected. *
* *
* Outputs: Returns SUCCESS/FAILURE/WARNING = Ok/Error/Warning *
* *
************************************************************************/
int
check_read(struct dinfo *dip, ssize_t count, size_t size)
{
int status = SUCCESS;
if ((size_t)count != size) {
if (count == FAILURE) {
INIT_ERROR_INFO(eip, dip->di_dname, OS_READ_FILE_OP, READ_OP, &dip->di_fd, dip->di_oflags,
dip->di_offset, size, os_get_error(), logLevelError, PRT_SYSLOG, RPT_NOFLAGS);
if (dip->di_retrying == True) {
eip->ei_prt_flags = PRT_NOFLAGS;
eip->ei_rpt_flags = (RPT_NODEVINFO|RPT_NOHISTORY);
}
status = ReportRetryableError(dip, eip, "Failed reading %s", dip->di_dname);
if (status == RETRYABLE) return(status);
if (dip->di_retrying == False) {
if ( (dip->di_trigger_control == TRIGGER_ON_ALL) ||
(dip->di_trigger_control == TRIGGER_ON_ERRORS) ) {
(void)ExecuteTrigger(dip, "read");
}
}
} else {
FILE *fp;
int prt_flags;
logLevel_t log_level;
hbool_t short_read_error;
/*
* Short reads with random I/O are now turned into errors, since
* continuing will report a *false* data corruption, when reading
* too much past the last record written! (misleading and wrong)
* Note: The random I/O offset should no longer encounter EOM.
*/
if ( ((size_t)count < size) && (dip->di_io_type == RANDOM_IO) ) {
fp = dip->di_efp;
log_level = logLevelError;
prt_flags = PRT_SYSLOG;
short_read_error = True;
} else {
fp = dip->di_ofp;
log_level = logLevelWarn;
prt_flags = PRT_NOFLAGS;
short_read_error = False;
}
/*
* For reads at end of file or reads at end of block
* devices, we'll read less than the requested count.
* In this case, we'll treat this as a warning since
* this is to be expected. In the case of tape, the
* next read will indicate end of tape (in my driver).
*
* NOTE: The raw device should be used for disks.
*/
if ( (dip->di_debug_flag || dip->di_verbose_flag || ((size_t)count > size)) &&
(dip->di_io_mode == TEST_MODE) ) {
if (dip->di_multiple_files) {
LogMsg(dip, fp, log_level, prt_flags,
"File %s, record #%lu, offset "FUF", attempted to read %lu bytes, read only %lu bytes.\n",
dip->di_dname, (dip->di_records_read + 1), dip->di_offset, size, count);
} else {
LogMsg(dip, fp, log_level, prt_flags,
"Record #%lu, offset "FUF", attempted to read %lu bytes, read only %lu bytes.\n",
(dip->di_records_read + 1), dip->di_offset, size, count);
}
}
if (short_read_error == True) {
INIT_ERROR_INFO(eip, dip->di_dname, OS_READ_FILE_OP, READ_OP, &dip->di_fd, dip->di_oflags,
dip->di_offset, size, SUCCESS, log_level, prt_flags, RPT_NOFLAGS);
(void)ReportErrorInfoX(dip, eip, NULL);
} else {
if ((size_t)count < size) { /* Partial read is a warning. */
dip->di_warning_errors++;
return (WARNING);
}
ReportDeviceInfo(dip, count, 0, False, NotMismatchedData);
RecordErrorTimes(dip, True);
}
}
dip->di_read_errors++;
status = FAILURE;
}
return (status);
}
/*
* This function is envoked when reading multiple tape files, to
* position past an expected file mark. This is especially important
* when using the lbdata or iot options, since encountering an expected
* EOF throws off the offset being maintained, resulting in an lba error.
*/
int
read_eof(struct dinfo *dip)
{
ssize_t count;
size_t bsize = dip->di_block_size;
int status = SUCCESS;
if (dip->di_debug_flag) {
Printf(dip, "Processing end of file... [file #%lu, record #%lu]\n",
(dip->di_files_read + 1), (dip->di_records_read + 1));
}
dip->di_eof_processing = True;
dip->di_retry_count = 0;
do {
count = read_record(dip, dip->di_data_buffer, bsize, bsize, dip->di_offset, &status);
} while (status == RETRYABLE);