-
Notifications
You must be signed in to change notification settings - Fork 15
/
dtutil.c
3203 lines (2940 loc) · 86.3 KB
/
dtutil.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 - 2021 *
* 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: dtutil.c
* Author: Robin T. Miller
*
* Description:
* Utility routines for generic data test program.
*
* Modification History:
*
* June 16th, 2021 by Robin T. Miller
* Update execute triggers to handle the SCSI trigger device.
*
* March 16th, 2021 by Robin T. Miller
* Add corrupt_buffer() in support of forcing corruptions for debug.
*
* October 27th, 2020 by Robin T. Miller
* Add functions in support for additional trigger functionality.
*
* September 6th, 2020 by Robin T. Miller
* For disk devices, when multiple threads are chosen, convert threads
* to slices to avoid *false* data corruptions, due to thread overwrites.
* This is happening due to folks selecting FS workloads instead of disk!
*
* May 20th, 2020 by Robin T. Miller
* Update API's using popen to capture stderr along with stdout!
*
* May 19th, 2020 by Robin T. Miller
* Update ExecuteCommand() to add prefix flag to control log prefix.
*
* May 19th, 2020 by Robin T. Miller
* When not compiled with SCSI library, use dt's spt path as default.
*
* May 13th, 2020 by Robin T. Miller
* Add the thread number to the pass command, since oftentimes we only
* wish to perform an action on the first thread, and not all threads.
*
* August 17th, 2019 by Robin T. Miller
* Update OS block tick conversion to avoid negative values, seen after
* ~597 hours on Windows, calculated values went negative for "%et" format.
* The new format is dayshoursminutessecondsfraction or ddhhmmss.ff
*
* September 1st, 2017 by Robin T. Miller
* Update get_data_size() to handle separate read/write block sizes.
*
* June 9th, 2015 by Robin T. Miller
* Added support for block tags (btags).
*
* February 7th, 2015 by Robin T. Miller
* Updated pattern file function to use OS specific API's.
*
* February 5th, 2015 by Robin T. Miller
* Added several functions for file locking support.
*
* August 6th, 2014 by Robin T. Miller
* Update variable I/O request function, get_variable(), to ensure
* the length is modulo the disk device size, not the pattern buffer size.
* This caused invalid requests when using 4 byte data patterns (IOT Ok).
*
* February 20th, 2014 by Robin T. Miller
* For triggers panic'ing *all* nodes, set the return status to
* TRIGACT_TERMINATE, so jobs/threads will get stopped. This is not done
* for all triggers, since we may wish to continue on one node panic'ing
* and/or when performing triage. Note: Triage will use its' own status.
* Note: This is *only* used for noprog's, errors have their own limit.
*
* December 13th, 2013 by Robin T. Miller
* Simplify and fix random offset calculations in do_random(). The bug
* was rounding up the offset to the random limit, so writes would encounter
* a premature end of media (ENOSPC) with raw disks. The rounding also caused
* occasional short writes, which could result in reading too much and causing
* a false data corrruption (reading more than written, will do that! :-().
*
* June 20th, 2013 by Robin T Miller
* Mostly a rewrite for multithreaded IO, so starting with new history!
*/
#include "dt.h"
#include <ctype.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h>
#if !defined(WIN32)
# include <pthread.h>
# include <poll.h>
# include <signal.h>
# include <strings.h>
# include <sys/param.h>
# include <netdb.h> /* for MAXHOSTNAMELEN */
# include <sys/time.h> /* for gettimeofday() */
# include <sys/wait.h>
#endif /* !defined(WIN32) */
/*
* External References:
*/
extern char *spt_path; /* Defined in dtscsi.c */
/*
* Forward References:
*/
void convert_clock_ticks(clock_t ticks, clock_t *days, clock_t *hours,
clock_t *minutes, clock_t *seconds, clock_t *frac);
int ExecuteBuffered(dinfo_t *dip, char *cmd, char *buffer, int bufsize);
int vSprintf(char *bufptr, const char *msg, va_list ap);
void report_EofMsg(dinfo_t *dip, ssize_t count, os_error_t error);
static char *bad_conversion_str = "Warning: unexpected conversion size of %d bytes.\n";
/*
* mySleep() - Sleep in seconds and/or microseconds.
*
* Inputs:
* dip = The device information pointer.
* sleep_time = The number of seconds/useconds to sleep.
* The global micro_delay flag if set selects microdelays.
*
* Return Value:
* void
*/
/* Note: RAND_MAX is 32767, so call it twice! */
#define os_random() ( (rand() << 16) + rand() )
/* TODO: Add sleep_min and sleep_max options! */
static unsigned int sleep_max = 10; /* Random max seconds to sleep. */
void
mySleep(dinfo_t *dip, unsigned int sleep_time)
{
unsigned int ms, timeout;
/*
* Allow a random delay.
*/
if (sleep_time == RANDOM_DELAY_VALUE) {
/* Note: Not using get_random() to avoid our random generator. */
/* The reason being, we too may get called randomly! */
sleep_time = (unsigned int)os_random();
if (dip->di_sleep_res == SLEEP_USECS) {
sleep_time %= uSECS_PER_SEC;
} else if (dip->di_sleep_res == SLEEP_MSECS) {
sleep_time %= mSECS_PER_SEC;
} else { /* SLEEP_DEFAULT or SLEEP_SECS */
sleep_time %= sleep_max;
}
}
/*
* Note: Convert microseconds (us) to milliseconds (ms), until
* we have true microsecond delays again (see notes below).
*/
if (dip->di_sleep_res == SLEEP_MSECS) {
timeout = sleep_time; /* Sleep is in msecs. */
} else if (dip->di_sleep_res == SLEEP_USECS) {
timeout = (sleep_time / MSECS); /* Convert usecs to msecs. */
} else { /* SLEEP_DEFAULT or SLEEP_SECS */
timeout = (sleep_time * MSECS); /* Convert secs to msecs. */
}
if (timeout == 0) timeout++; /* Minimum of one msec! */
if (dip->di_timerDebugFlag) {
Printf(dip, "Delaying for %ums (or %.2f secs)...\n",
timeout, ((float)timeout / (float)MSECS));
}
do {
/* Do short sleeps so we can detect when we're terminating! */
ms = min(MSECS, timeout);
(void)os_msleep(ms);
if ( PROGRAM_TERMINATING || THREAD_TERMINATING(dip) ) break;
timeout -= ms;
} while (timeout);
return;
}
/*
* Simple function to sleeping in seconds (optionally random).
*
* Note: We don't use sleep() to avoid signal issues!
*/
void
SleepSecs(dinfo_t *dip, unsigned int sleep_time)
{
unsigned int ms, timeout;
/*
* Allow a random delay.
*/
if (sleep_time == RANDOM_DELAY_VALUE) {
sleep_time = (unsigned int)os_random();
sleep_time %= sleep_max;
}
timeout = (sleep_time * MSECS); /* convert secs to ms */
if (timeout == 0) timeout++; /* minimum of one ms! */
if (dip->di_timerDebugFlag) {
Printf(dip, "Delaying for %ums (or %.2f secs)...\n",
timeout, ((float)timeout / (float)MSECS));
}
do {
/* Do short sleeps so we can detect when we're terminating! */
ms = min(MSECS, timeout);
(void)os_msleep(ms);
if ( PROGRAM_TERMINATING || THREAD_TERMINATING(dip) ) break;
timeout -= ms;
} while (timeout);
return;
}
/* Return the difference in usecs, of two timers. */
uint64_t
timer_diff(struct timeval *start, struct timeval *end)
{
struct timeval delta;
delta.tv_sec = end->tv_sec - start->tv_sec;
delta.tv_usec = end->tv_usec - start->tv_usec;
if (delta.tv_usec < 0) {
delta.tv_sec -= 1;
delta.tv_usec += 1000000;
}
return (((uint64_t)delta.tv_sec * (uint64_t)1000000) + (uint64_t)delta.tv_usec);
}
/* Return the difference in usecs, of previous timer vs. now. */
uint64_t
timer_now(struct timeval *timer)
{
struct timeval now, delta;
(void)gettimeofday(&now, NULL);
delta.tv_sec = now.tv_sec - timer->tv_sec;
delta.tv_usec = now.tv_usec - timer->tv_usec;
if (delta.tv_usec < 0) {
delta.tv_sec -= 1;
delta.tv_usec += 1000000;
}
return (((uint64_t)delta.tv_sec * (uint64_t)1000000) + (uint64_t)delta.tv_usec);
}
/************************************************************************
* *
* fill_buffer() - Fill Buffer with a Data Pattern. *
* *
* Description: *
* If a pattern_buffer exists, then this data is used to fill the *
* buffer instead of the data pattern specified. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = Pointer to buffer to fill. *
* byte_count = Number of bytes to fill. *
* pattern = Data pattern to fill buffer with. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
void
fill_buffer ( dinfo_t *dip,
void *buffer,
size_t byte_count,
u_int32 pattern)
{
register unsigned char *bptr = buffer;
register unsigned char *pptr, *pend;
register size_t bcount = byte_count;
register lbdata_t lbdata_size = dip->di_lbdata_size;
register size_t i;
pptr = dip->di_pattern_bufptr;
pend = dip->di_pattern_bufend;
/*
* Initialize the buffer with a data pattern and optional prefix.
*/
if ( dip->di_btag_flag && (dip->di_fprefix_string == NULL) ) {
size_t btag_size = getBtagSize(dip->di_btag);
for (i = 0; i < bcount; ) {
if ((i % lbdata_size) == 0) {
i += btag_size;
bptr += btag_size;
continue;
}
*bptr++ = *pptr++; i++;
if (pptr == pend) {
pptr = dip->di_pattern_buffer;
}
}
} else if (dip->di_fprefix_string == NULL) {
while (bcount--) {
*bptr++ = *pptr++;
if (pptr == pend) {
pptr = dip->di_pattern_buffer;
}
}
} else {
/*
* Fill the buffer with a prefix and a data pattern.
*/
for (i = 0; i < bcount; ) {
/*
* Please Note: This code is a major performance bottleneck! ;(
* Also Note: 64-bit Linux binary is slower than 32-bit version!
* Note: The IOT loops are faster due to aligned 32-bit copies.
* Optimizing is for another day, but a block tag is preferred!
* Obviously, optimized compiled code also make a big difference.
* FWIW: Inlining the prefix copy, made very little difference.
*/
if ((i % lbdata_size) == 0) {
size_t pcount;
if (dip->di_btag) {
btag_t *btag = dip->di_btag;
size_t btag_size = sizeof(*btag) + btag->btag_opaque_data_size;
i += btag_size;
bptr += btag_size;
}
pcount = copy_prefix(dip, bptr, (bcount - i));
i += pcount;
bptr += pcount;
continue;
}
*bptr++ = *pptr++; i++;
if (pptr == pend) {
pptr = dip->di_pattern_buffer;
}
}
}
dip->di_pattern_bufptr = pptr;
return;
}
/************************************************************************
* *
* init_buffer() - Initialize Buffer with a Data Pattern. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = Pointer to buffer to init. *
* count = Number of bytes to initialize. *
* pattern = Data pattern to init buffer with. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
void
init_buffer( dinfo_t *dip,
void *buffer,
size_t count,
u_int32 pattern )
{
register unsigned char *bp;
union {
u_char pat[sizeof(u_int32)];
u_int32 pattern;
} p;
register size_t i;
/*
* Initialize the buffer with a data pattern.
*/
p.pattern = pattern;
bp = buffer;
for (i = 0; i < count; i++) {
*bp++ = p.pat[i & (sizeof(u_int32) - 1)];
}
return;
}
/************************************************************************
* *
* corrupt_buffer() - Corrupt Buffer. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = Pointer to buffer to corrupt. *
* length = The data buffer length. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
/* Note: RAND_MAX is 32767, so call it twice! */
/* Consider adding one of these: https://prng.di.unimi.it/ */
/* FYI: This is yet another POSIX weakness! (IMHO) */
#define os_random() ( (rand() << 16) + rand() )
#define CRANDOM(lower, upper) \
( lower + (int32_t)( ((double)(upper - lower + 1) * os_random()) / (UINT32_MAX + 1.0)) )
void
corrupt_buffer( dinfo_t *dip, void *buffer, int32_t length, uint64_t record)
{
uint8_t *bp = (uint8_t *)buffer;
uint8_t *eptr = (bp + length);
int32_t cindex = dip->di_corrupt_index;
int32_t clength = dip->di_corrupt_length;
int32_t base = 0;
union {
u_char pat[sizeof(u_int32)];
u_int32 pattern;
} p;
p.pattern = dip->di_corrupt_pattern;
if (dip->di_random_seed == 0) {
dip->di_random_seed = os_create_random_seed();
srand((unsigned int)dip->di_random_seed);
}
if (clength == 0) {
clength = CRANDOM(base, length);
}
/* Corrupt the entire buffer, if corrupt length is larger than request. */
if (clength > length) {
cindex = 0;
clength = length;
}
if (cindex == UNINITIALIZED) {
cindex = CRANDOM(base, (length - clength));
}
Wprintf(dip, "Record #"LUF", Corrupting buffer "LLPXFMT" at index %u, length %d, pattern 0x%x, step %u...\n",
record, bp, cindex, clength, p.pattern, dip->di_corrupt_step);
bp += cindex; /* Set the starting buffer offset. */
/* Corrupt the buffer. */
while (clength && (bp < eptr)) {
register int32_t i;
/* Byte at a time, buffer may be misaligned! */
for (i = 0; clength && (bp < eptr) && (i < sizeof(p.pattern)); i++, --clength) {
*bp++ = p.pat[i & (sizeof(uint32_t) - 1)];
}
/* Allow multiple corruption sections via buffer step. */
/* Note: This only has effect with large corruption lengths! */
if (dip->di_corrupt_step) {
bp += dip->di_corrupt_step;
}
}
return;
}
/************************************************************************
* *
* poision_buffer() - Poison Buffer with a Data Pattern. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = Pointer to buffer to init. *
* count = Number of bytes to initialize. *
* pattern = Data pattern to init buffer with. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
void
poison_buffer( dinfo_t *dip,
void *buffer,
size_t count,
u_int32 pattern )
{
uint8_t *bp = buffer;
uint8_t *eptr = (bp + count) - sizeof(pattern);
uint32_t dsize = (dip->di_dsize) ? dip->di_dsize : BLOCK_SIZE;
union {
u_char pat[sizeof(u_int32)];
u_int32 pattern;
} p;
/* Varible length file records may be too short! */
if (count < sizeof(pattern)) {
return;
}
p.pattern = pattern;
/*
* Initialize the buffer with a data pattern, every dsize bytes.
*/
while (bp < eptr) {
register uint8_t *bptr = bp;
register size_t i;
/* Byte at a time, buffer may be misaligned! */
for (i = 0; i < sizeof(pattern); i++) {
*bptr++ = p.pat[i & (sizeof(uint32_t) - 1)];
}
bp += dsize;
}
return;
}
#if _BIG_ENDIAN_
/************************************************************************
* *
* init_swapped() - Initialize Buffer with a Swapped Data Pattern. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = Pointer to buffer to init. *
* count = Number of bytes to initialize. *
* pattern = Data pattern to init buffer with. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
void
init_swapped ( dinfo_t *dip,
void *buffer,
size_t count,
u_int32 pattern )
{
register unsigned char *bp;
union {
u_char pat[sizeof(u_int32)];
u_int32 pattern;
} p;
register size_t i;
/*
* Initialize the buffer with a data pattern.
*/
p.pattern = pattern;
bp = buffer;
for (i = count; i ; ) {
*bp++ = p.pat[--i & (sizeof(u_int32) - 1)];
}
return;
}
#endif /* _BIG_ENDIAN_ */
/************************************************************************
* *
* init_lbdata() - Initialize Data Buffer with Logical Block Data. *
* *
* Description: *
* This function takes the starting logical block address, and *
* inserts it every logical block size bytes, overwriting the first 4 *
* bytes of each logical block with its' address. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = The data buffer to initialize. *
* count = The data buffer size (in bytes). *
* lba = The starting logical block address. *
* lbsize = The logical block size (in bytes). *
* *
* Outputs: *
* Returns the next lba to use. *
* *
************************************************************************/
u_int32
init_lbdata (
struct dinfo *dip,
void *buffer,
size_t count,
u_int32 lba,
u_int32 lbsize )
{
unsigned char *bp = buffer;
register ssize_t i;
/*
* Initialize the buffer with logical block data.
*/
if (dip->di_fprefix_string) {
register size_t pcount = 0, scount = lbsize;
/*
* The lba is encoded after the prefix string.
*/
pcount = MIN((size_t)dip->di_fprefix_size, count);
scount -= pcount;
for (i = 0; (i+pcount+sizeof(lba)) <= count; ) {
bp += pcount;
htos(bp, lba, sizeof(lba));
i += lbsize;
bp += scount;
lba++;
}
} else {
for (i = 0; (i+sizeof(lba)) <= count; ) {
htos (bp, lba, sizeof(lba));
i += lbsize;
bp += lbsize;
lba++;
}
}
return(lba);
}
#if defined(TIMESTAMP)
/************************************************************************
* *
* init_timestamp() - Initialize Data Buffer with a Timestamp. *
* *
* Description: *
* This function places a timestamp in the first 4 bytes of each *
* data block. *
* *
* Inputs: *
* dip = The device information pointer. *
* buffer = The data buffer to initialize. *
* count = The data buffer size (in bytes). *
* lbsize = The logical block size (in bytes). *
* *
* Outputs: Returns the next lba to use. *
* *
************************************************************************/
void
init_timestamp (
dinfo_t *dip,
void *buffer,
size_t count,
u_int32 lbsize )
{
unsigned char *bptr = buffer;
register ssize_t i;
register iotlba_t timestamp = (iotlba_t)time((time_t *)0);
/*
* Initialize the buffer with a timestamp (in seconds).
*/
if (dip->di_fprefix_string || dip->di_btag_flag) {
register size_t pcount = 0, scount = lbsize;
if ( dip->di_btag_flag ) {
pcount = getBtagSize(dip->di_btag);
}
/*
* The timestamp is encoded after the prefix string.
*/
pcount += MIN((size_t)dip->di_fprefix_size, count);
scount -= pcount;
for (i = 0; (i+pcount+sizeof(timestamp)) <= count; ) {
bptr += pcount;
htos(bptr, (large_t)timestamp, sizeof(timestamp));
i += lbsize;
bptr += scount;
}
} else {
for (i = 0; (i+sizeof(timestamp)) <= count; ) {
htos(bptr, (large_t)timestamp, sizeof(timestamp));
i += lbsize;
bptr += lbsize;
}
}
return;
}
#endif /* defined(TIMESTAMP) */
#if !defined(INLINE_FUNCS)
/*
* Calculate the starting logical block number.
*/
u_int32
make_lba(
struct dinfo *dip,
Offset_t pos )
{
return( (u_int32)((pos == (Offset_t)0) ? (u_int32)0 : (pos/dip->di_lbdata_size)) );
}
Offset_t
make_offset(struct dinfo *dip, u_int32 lba)
{
return( (Offset_t)(lba * dip->di_lbdata_size) );
}
/*
* Calculate the starting lbdata block number.
*/
u_int32
make_lbdata(
struct dinfo *dip,
Offset_t pos )
{
return( (u_int32)((pos == (Offset_t)0) ? (u_int32)0 : (pos/dip->di_lbdata_size)) );
}
#endif /* !defined(INLINE_FUNCS) */
u_int32
winit_lbdata(
struct dinfo *dip,
Offset_t pos,
u_char *buffer,
size_t count,
u_int32 lba,
u_int32 lbsize )
{
if (dip->di_user_lbdata) {
/* Using user defined lba, not file position! */
return(init_lbdata (dip, buffer, count, lba, lbsize));
} else if (pos == (Offset_t) 0) {
return(init_lbdata (dip, buffer, count, (u_int32) 0, lbsize));
} else {
return(init_lbdata (dip, buffer, count, (u_int32)(pos / lbsize), lbsize));
}
}
/************************************************************************
* *
* init_padbytes() - Initialize pad bytes at end of data buffer. *
* *
* Inputs: buffer = Pointer to start of data buffer. *
* offset = Offset to where pad bytes start. *
* pattern = Data pattern to init buffer with. *
* *
* Return Value: *
* Void. *
* *
************************************************************************/
void
init_padbytes ( u_char *buffer,
size_t offset,
u_int32 pattern )
{
size_t i;
u_char *bptr;
union {
u_char pat[sizeof(u_int32)];
u_int32 pattern;
} p;
p.pattern = pattern;
bptr = buffer + offset;
for (i = 0; i < PADBUFR_SIZE; i++) {
*bptr++ = p.pat[i & (sizeof(u_int32) - 1)];
}
}
/*
* copy_prefix() - Copy Prefix String to Buffer.
*
* Inputs:
* dip = The device information pointer.
* buffer = Pointer to buffer to copy prefix.
* bcount = Count of remaining buffer bytes.
*
* Outputs:
* Returns number of prefix bytes copied.
*/
size_t
copy_prefix( dinfo_t *dip, u_char *buffer, size_t bcount )
{
size_t pcount = MIN((size_t)dip->di_fprefix_size, bcount);
(void)memcpy(buffer, dip->di_fprefix_string, pcount);
return(pcount);
}
/************************************************************************
* *
* process_pfile() - Process a pattern file. *
* *
* Inputs: *
* dip = The device information pointer. *
* file = Pointer to pattern file name. *
* *
* Outputs: Returns on success, exits on open failure. *
* *
* Return Value: *
* Returns SUCCESS / FAILURE *
* *
************************************************************************/
int
process_pfile(dinfo_t *dip, char *file)
{
HANDLE fd;
int oflags = O_RDONLY;
size_t count, size;
large_t filesize;
u_char *buffer;
if ( os_file_information(file, &filesize, NULL, NULL) == FAILURE ) {
Fprintf(dip, "The pattern file '%s', cannot be accessed!\n", file);
ReportErrorInfo(dip, file, os_get_error(), OS_GET_FILE_ATTR_OP, GETATTR_OP, True);
return(FAILURE);
}
size = (size_t)filesize;
fd = dt_open_file(dip, file, oflags, 0, NULL, NULL, True, False);
if (fd == NoFd) {
return(FAILURE);
}
buffer = malloc_palign(dip, size, 0);
if ( (count = os_read_file(fd, buffer, size)) != size) {
Fprintf (dip, "Pattern file '%s' read error!\n", file);
if ((ssize_t)count == FAILURE) {
ReportErrorInfo(dip, file, os_get_error(), OS_READ_FILE_OP, READ_OP, True);
return(FAILURE);
} else {
Eprintf(dip, "Attempted to read %d bytes, read only %d bytes.", size, count);
return(FAILURE);
}
}
setup_pattern(dip, buffer, size, True);
(void)os_close_file(fd);
return(SUCCESS);
}
/*
* process_iotune() - Process Parmaters from an IO Tune file.
*
* Inputs:
* dip = The device information pointer.
* file = Pointer to IO tune file name.
*
* Outputs:
* Sets up new IO delays.
*
* Return Value:
* none
*/
void
process_iotune(dinfo_t *dip, char *file)
{
FILE *fp;
ssize_t count;
int size = STRING_BUFFER_SIZE;
char buffer[STRING_BUFFER_SIZE];
char *mode = "r";
struct stat statbuf, *sb = &statbuf;
int status;
if (stat(file, sb) == FAILURE) return;
if (dip->di_iotune_mtime == sb->st_mtime) return;
dip->di_iotune_mtime = sb->st_mtime;
if (dip->di_debug_flag || dip->di_tDebugFlag) {
Printf(dip, "Processing I/O tune file '%s'...\n", file);
}
if ( (fp = fopen (file, mode)) == (FILE *) 0) {
Perror(dip, "Unable to open script file '%s' for reading", file);
return;
}
do {
memset(buffer, '\0', size);
if (fgets (buffer, size, fp) == NULL) {
count = -1; /* Error or EOF, stop reading! */
} else {
char *p;
if (p = strrchr(buffer, '\r')) *p = '\0';
if (p = strrchr(buffer, '\n')) *p = '\0';
count = (ssize_t)strlen(buffer);
}
/* Format: [jobid]|[tag] *_delay=value enable=flag... */
if (count > 0 ) {
status = modify_jobs(dip, (job_id_t) 0, NULL, buffer);
if (status == FAILURE) break;
}
} while ( count > 0 );
#if defined(DEBUG)
if (dip->di_debug_flag || dip->di_tDebugFlag) {
Printf(dip, "Finished processing I/O tune file...\n");
}
#endif
(void)fclose(fp);
return;
}
/*
* setup_pattern() - Setup pattern variables.
*
* Inputs:
* dip = The device information pointer.
* buffer = Pointer to pattern buffer.
* size = Size of pattern buffer.
*
* Outputs:
* pattern variables setup with 1st 4 bytes of pattern. (if request)
*
* Return Value:
* void
*/
void
setup_pattern(dinfo_t *dip, uint8_t *buffer, size_t size, hbool_t init_pattern)
{
dip->di_pattern_buffer = buffer;
dip->di_pattern_bufptr = buffer;
dip->di_pattern_bufend = (buffer + size);
dip->di_pattern_bufsize = size;
if (init_pattern == False) return;
dip->di_pattern = (uint32_t)0;
switch (size) {
case sizeof(u_char):
dip->di_pattern = (uint32_t)buffer[0];
break;
case sizeof(u_short):
dip->di_pattern = ( ((uint32_t)buffer[1] << 8) | (uint32_t)buffer[0] );
break;
case 0x03:
dip->di_pattern = ( ((uint32_t)buffer[2] << 16) | ((uint32_t)buffer[1] << 8) |
(uint32_t) buffer[0] );
break;
default:
dip->di_pattern = ( ((uint32_t)buffer[3] << 24) | ((uint32_t)buffer[2] << 16) |
((uint32_t)buffer[1] << 8) | (uint32_t)buffer[0]);
break;
}
return;
}
void
reset_pattern(dinfo_t *dip)
{
if (dip->di_pattern_buffer) {
free_palign(dip, dip->di_pattern_buffer);
dip->di_pattern_buffer = NULL;
dip->di_pattern_bufptr = NULL;
dip->di_pattern_bufend = NULL;
dip->di_pattern_bufsize = 0;
}
return;
}
/*
* Copy pattern bytes to pattern buffer with proper byte ordering.
*/
void
copy_pattern (u_int32 pattern, u_char *buffer)
{
buffer[0] = (u_char) pattern;
buffer[1] = (u_char) (pattern >> 8);
buffer[2] = (u_char) (pattern >> 16);
buffer[3] = (u_char) (pattern >> 24);
}
/*
* convert_clock_ticks() - Convert OS clock ticks to time values.
*
* Note: clock_ticks is defined as long on Linux and Windows, but this
* value is in system click ticks from sysconf(_SC_CLK_TCK), which on
* Linux is 100 while on Windows is 1000, so wrapping occurs sooner on
* Windows causing tme values to become negative. Therefore, the signed
* clock_t os cast to an unsigned long to avoid negative time values.
* In fact, the times() API used will return -1 when it fails.
*
* Clearly using times() needs replaced with a better method (see below):
*
* On Linux, the "arbitrary point in the past" from which the return value of times()
* is measured has varied across kernel versions. On Linux 2.4 and earlier this point
* is the moment the system was booted. Since Linux 2.6, this point is (2^32/HZ) - 300
* (i.e., about 429 million) seconds before system boot time. This variability across
* kernel versions (and across UNIX implementations), combined with the fact that the
* returned value may overflow the range of clock_t, means that a portable application
* would be wise to avoid using this value.
* To measure changes in elapsed time, use clock_gettime(2) instead.
*
* TODO:
* int clock_getres(clockid_t clk_id, struct timespec *res);
* int clock_gettime(clockid_t clk_id, struct timespec *tp);
*
* The above API's do not exist on Windows, see this URL for guideance:
* https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
*/
void
convert_clock_ticks(clock_t ticks, clock_t *days, clock_t *hours,
clock_t *minutes, clock_t *seconds, clock_t *frac)
{
unsigned long clock_ticks = (unsigned long)ticks; /* Bandaide, may help! */
*frac = (clock_ticks % hertz);
*frac = (*frac * 100) / hertz;
clock_ticks /= hertz;
*seconds = (clock_ticks % SECS_PER_MIN);
clock_ticks /= SECS_PER_MIN;
*minutes = (clock_ticks % MINS_PER_HOUR);
clock_ticks /= MINS_PER_HOUR;
*hours = (clock_ticks % HOURS_PER_DAY);
clock_ticks /= HOURS_PER_DAY;
*days = clock_ticks;
return;
}
/*