summaryrefslogtreecommitdiff
path: root/usr/src/cmd/avs/sdbc/sd_diag.c
blob: 706f547226f3cd2e60f747ad482f9c15f9fcb783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* #include <version.h> SKK */
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/inttypes.h>
#include <stdio.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#include <nsctl.h>

#include <sys/nsctl/sd_cache.h>
#include <sys/nsctl/sd_conf.h>

#include <stdlib.h>
#include <thread.h>
#include <synch.h>

#define	MAXPARTS	100	/* Max disks */
#define	MAXBUF	65536	/* Max buffer size in long words */
#define	DISKLIST	"disk_config"	/* Default config file */
#define	DEF_SIZE	8192	/* Default buffer size */
#define	DEF_LOOP	1000	/* Loops for test */
#define	RAND_LOOPS	DEF_LOOP	/* # of random ios to do */

/*
 *  >>>>>>>>> USER LEVEL SD CACHE DIAGNOSTICS <<<<<<<<<<
 *
 *  Write and read data blocks w/multiple processes
 *  Starts one process for each partition specified in
 *  the config file
 */

int  buf1[MAXBUF];
int  buf2[MAXBUF];
char name[MAXPARTS][80];
int  pattern[MAXPARTS];
int  bufsize = DEF_SIZE;
int  fba_num_bufsize;
nsc_size_t  loops   = DEF_LOOP;
nsc_size_t  r_loops   = RAND_LOOPS;
int  fsize   = -1;
int  readercount = 3;
int  Rflag = O_EXCL;
char config_file[32];

int
read_parts()
{
	FILE *dfile;
	int   partitions = 0;
	int i;

	dfile = fopen(config_file, "r");
	if (dfile == NULL) {
		(void) printf("cannot open file: %s\n", config_file);
		perror("fopen");
		exit(errno);
	}
	for (i = 0; i < MAXPARTS; i++) {
		if (fscanf(dfile, "%s %x", name[i], (uint_t *)&pattern[i]) ==
		    EOF) {
			break;
		} else
			if (name[i][0] == '#' || strchr(name[i], '/') == NULL) {
				i--;
				continue;
			}
		partitions++;
	}
	(void) fclose(dfile);
	(void) printf("No. of partitions listed in file '%s' = %d\n\n",
			config_file, partitions);
	return (partitions);
}

void
print_usage()
{
	(void) printf("Usage:\n");
	(void) printf(
"sd_diag [-R] [-b <bufsize>] [-d <datasize>] [-l <loops>] [-r <readers>]\n");
	(void) printf(
"        [-f <disk_config_file>] <test#>\n");
	(void) printf(" test 1 = random read/write\n");
	(void) printf("      2 = random read/write/verify, read after write\n");
	(void) printf("      3 = random read/write/verify,");
	(void) printf(" all reads after all writes\n");
	(void) printf("      4 = sequential read/write\n");
	(void) printf("      5 = sequential write/read/verify,");
	(void) printf(" all reads after all writes\n");
	(void) printf(
	"      6 = altenating top/bottom sequential read/write/verify\n");
	(void) printf("      7 = multiple readers/1 random writer\n");
	(void) printf("      8 = random writes\n");
	(void) printf("      9 = sequential write of known data\n");
	(void) printf("      10 = sequential copy of datasize disk/verify\n");
	(void) printf("      11 = sequential read/verify test 9 data -");
	(void) printf(" then clear data with timestamp\n");
	(void) printf("      12 = sequential read/verify test 9 data -");
	(void) printf(" no clear data\n");
	(void) printf("\n");
	(void) printf("  <bufsize> in bytes (minimum is 512 bytes)\n");
	(void) printf("  <datasize> in Mbytes per disk\n");
	(void) printf("  <loops> is count of reads/writes,\n");
	(void) printf("          loops = 0 tests entire datasize disk");
	(void) printf(" for sequential tests.\n");
	(void) printf("          loops = 0 performs %d I/Os for the random "
	    "tests\n", RAND_LOOPS);
	(void) printf("  <readers> is count of readers for test #7 (default "
	    "is 3).\n");
	(void) printf(" [ defaults: bufsize = %d bytes, loops = %d,",
			DEF_SIZE, DEF_LOOP);
	(void) printf(" datasize = disksize ]\n");
	(void) printf("\n");
	(void) printf("  -R : do nsc_reserve(), nsc_release(0 around each "
	    "I/O\n");
}

void
parse_opts(int argc, char *argv[])
{
	extern char *optarg;
	int c;

	while ((c = getopt(argc, argv, "b:d:l:r:Rf:")) != -1) {
		switch (c) {
			case 'f':
			/* printf("\n%s", optarg); */
			(void) strcpy(config_file, optarg);
			break;
		case 'b':
			/* bufsize between 1*512 and 512*512 */
			bufsize = strtol(optarg, 0, 0);
			if (bufsize > (MAXBUF*4))
				bufsize = MAXBUF*4;
			else if (bufsize < FBA_SIZE(1))
			    bufsize = FBA_SIZE(1);
			break;
		case 'd':
			/* convert datasize from Mb's to fba */
			fsize = strtol(optarg, 0, 0) *  FBA_NUM(1 << 20);
			break;
		case 'l':
			loops = (nsc_size_t)strtoll(optarg, 0, 0);
			break;
		case 'r':
			/* count of readers for test 7 */
			readercount = strtol(optarg, 0, 0);
			break;
		case 'R':
			/* do reserve, release on a per io basis */
			Rflag = 0;
			break;
		case '?':
			print_usage();
			exit(0);
		}
	}
	bufsize &= ~FBA_MASK; /* multiple of 512 bytes for SECTMODE I/O */
	fba_num_bufsize = FBA_NUM(bufsize);

	/*  set #ios for random io tests */
	if (loops != 0)
		r_loops = loops;

}

nsc_size_t
set_part_size(char *path, nsc_fd_t *sdfd)
{
	nsc_size_t filesize;
	int rc;

	rc = nsc_partsize(sdfd, &filesize); /* partsize in FBAs (512 bytes) */
	if (rc < 0 || filesize == 0) {
		(void) fprintf(stderr,
		    "set_part_size: cannot access partition size");
		(void) fprintf(stderr, " for %s\n", path);
		(void) nsc_close(sdfd);
		exit(1);
	}

	(void) printf("Partition %s, size:%" NSC_SZFMT " blocks\n", path,
	    filesize);

	if (fsize != -1 && fsize < filesize)
		filesize = fsize;
	filesize -= fba_num_bufsize;
	if (filesize < fba_num_bufsize) {
		(void) printf("ERROR: Max block size %" NSC_SZFMT "\n",
		    filesize);
		(void) nsc_close(sdfd);
		exit(0);
	}

	return (filesize);
}

int
do_sdtest1(int fd, nsc_size_t loops, nsc_size_t filesize)
{
	nsc_off_t seekpos;
	nsc_size_t i;
	ssize_t r;

	for (i = 0; i < loops; i++) {
		seekpos = (
#ifdef NSC_MULTI_TERABYTE
		    ((nsc_off_t)rand() << 48) | ((nsc_off_t)rand() << 32) |
#endif
		    (rand() << 16) | rand()) % filesize;
		r = pwrite(fd, buf1, bufsize, (off_t)(seekpos << SCTRSHFT));
		if (r <= 0) {
			perror("Test1: write");
			return (1);
		}
		seekpos = (
#ifdef NSC_MULTI_TERABYTE
		    ((nsc_off_t)rand() << 48) | ((nsc_off_t)rand() << 32) |
#endif
		    (rand() << 16) | rand()) % filesize;
		r = pread(fd, buf2, bufsize, (off_t)(seekpos << SCTRSHFT));
		if (r <= 0) {
			perror("Test1: read");
			return (1);
		}
	}
	return (0);
}

void
gen_data(int *buffer, int size)
{
	int i;

	size /= 4;
	for (i = 0; i < size; i++)
		buffer[i] = rand() << 16 | rand();
}

int
do_sdtest2(int fd, nsc_size_t loops, nsc_size_t filesize, int h)
{
	nsc_off_t seekpos;
	int err = 0;
	ssize_t r;
	nsc_size_t i;

	for (i = 0; i < loops; i++) {
		seekpos = (
#ifdef NSC_MULTI_TERABYTE
		    ((nsc_off_t)rand() << 48) | ((nsc_off_t)rand() << 32) |
#endif
		    (rand() << 16) | rand()) % filesize;
		gen_data(buf1, bufsize);
		r = pwrite(fd, buf1, bufsize, (off_t)(seekpos << SCTRSHFT));
		if (r <= 0) {
			perror("Test2: write");
			err++;
			return (err);
		}
		r = pread(fd, buf2, bufsize, (off_t)(seekpos << SCTRSHFT));
		if (r <= 0) {
			perror("Test2: read");
			err++;
			return (err);
		}
		if (memcmp(buf1, buf2, bufsize)) {
			(void) printf("Test2: Data corruption,"
			    " fd:%s, fpos:%" PRId64 ", len:%d\n",
			    name[h], (int64_t)(seekpos << SCTRSHFT),
			    bufsize);
			err++;
		}
	}
	return (err);
}

int
do_sdtest3(int fd, nsc_size_t loops, nsc_size_t filesize, int h, nsc_fd_t *sdfd)
{
	nsc_off_t *seekpos;
	int err = 0;
	nsc_size_t i;
	ssize_t r;

	seekpos = malloc(loops*sizeof (nsc_off_t));
	if (seekpos == NULL) {
		perror("Test3: malloc");
		(void) nsc_close(sdfd);
		exit(errno);
	}
	gen_data(buf1, bufsize);

	for (i = 0; i < loops; i++) {
		seekpos[i] = (
#ifdef NSC_MULTI_TERABYTE
		    ((nsc_off_t)rand() << 48) | ((nsc_off_t)rand() << 32) |
#endif
		    (rand() << 16) | rand()) % filesize;
		seekpos[i] -= seekpos[i] % fba_num_bufsize;
		r = pwrite(fd, buf1, bufsize, (off_t)(seekpos[i] << SCTRSHFT));
		if (r <= 0) {
			perror("Test3: write");
			err++;
			goto cleanup;
		}
	}
	for (i = 0; i < loops; i++) {
		buf2[0] = '\0';	/* clear buf to make sure something is read */
		r = pread(fd, buf2, bufsize, (off_t)(seekpos[i] << SCTRSHFT));
		if (r <= 0) {
			perror("Test3: read");
			err++;
			goto cleanup;
		}
		if (memcmp(buf1, buf2, bufsize)) {
			(void) printf("Data corruption, fd:%s, fpos:%" PRId64
			    ", len:%d\n", name[h],
			    (int64_t)(seekpos[i] << SCTRSHFT), bufsize);
			err++;
		}
	}

cleanup:
	free(seekpos);
	return (err);
}

int
do_sdtest4(int fd, nsc_size_t loops, nsc_size_t filesize)
{
	ssize_t r;
	nsc_size_t i;

	/*
	 * Do sequential reads/writes for loops number
	 * of bufsize chunks, unless loops == 0, then do
	 * entire disk.
	 * 1. sequential reads from the top down,
	 * 2. sequential writes from the top down,
	 * 3. sequential reads from the bottom up,
	 * 4. sequential writes from the bottom up.
	 */
	if ((loops > (filesize / fba_num_bufsize)) || (!loops))
	    loops = filesize / fba_num_bufsize; /* entire disk */

	for (i = 0; i < loops; i++) {
		r = pread(fd, buf2, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test4: read");
			return (1);
		}
	}
	for (i = 0; i < loops; i++) {
		r = pwrite(fd, buf1, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test4: write");
			return (1);
		}
	}
	for (i = loops - 1; i + 1 > 0; i--) {
		r = pread(fd, buf2, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test4: read");
			return (1);
		}
	}
	for (i = loops - 1; i + 1 > 0; i--) {
		r = pwrite(fd, buf1, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test4: write");
			return (1);
		}
	}
	return (0);
}

int
do_sdtest5(int fd, nsc_size_t loops, nsc_size_t filesize, int h)
{
	int err = 0;
	ssize_t r;
	nsc_size_t i;

	/*
	 * Do sequential writes with verify reads for loops number
	 * of bufsize chunks, unless loops == 0, then do
	 * entire disk.
	 * 1. sequential writes from the top down,
	 * 2. sequential reads from the top down with verify,
	 * 3. sequential writes from the bottom up,
	 * 4. sequential reads from the bottom up with verify.
	 */
	if ((loops > (filesize / fba_num_bufsize)) || (!loops))
	    loops = filesize / fba_num_bufsize; /* entire disk */

	gen_data(buf1, bufsize);

	for (i = 0; i < loops; i++) {
		r = pwrite(fd, buf1, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test5: write");
			err++;
			return (err);
		}
	}
	for (i = 0; i < loops; i++) {
		buf2[0] = '\0';	/* clear buf to make sure something is read */
		r = pread(fd, buf2, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test5: read");
			err++;
			return (err);
		}
		if (memcmp(buf1, buf2, bufsize)) {
			(void) printf("Test5: Data corruption,"
			    " fd:%s, fpos:%" NSC_SZFMT ", len:%d\n",
			    name[h], i, bufsize);
			err++;
		}
	}

	gen_data(buf1, bufsize);

	for (i = loops - 1; i + 1 > 0; i--) {
		r = pwrite(fd, buf1, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test5: write");
			err++;
			return (err);
		}
	}
	for (i = loops - 1; i + 1 > 0; i--) {
		buf2[0] = '\0';	/* clear buf to make sure something is read */
		r = pread(fd, buf2, bufsize, (i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test5: read");
			err++;
			return (err);
		}
		if (memcmp(buf1, buf2, bufsize)) {
			(void) printf("Test5: Data corruption,"
			    " fd:%s, fpos:%" NSC_SZFMT ", len:%d\n",
			    name[h], i, bufsize);
			err++;
		}
	}
	return (err);
}


int
do_sdtest6(int fd, nsc_size_t loops, nsc_size_t filesize, int h)
{
	int err = 0;
	nsc_size_t i;
	ssize_t r;
	nsc_size_t endloop = filesize / fba_num_bufsize;
	int  buf3[MAXBUF];
	int  buf4[MAXBUF];
	nsc_off_t  top_pos, bottom_pos;

	/*
	 * Do alternating top down and bottom up sequential writes
	 * (working towards middle) and verify with reads
	 * for loops number of bufsize chunks, unless loops == 0, then do
	 * entire disk.
	 */
	if ((loops > (filesize / fba_num_bufsize)) || (!loops))
	    loops = filesize / fba_num_bufsize; /* entire disk */

	for (i = 0; i < loops; i++) {
		gen_data(buf1, bufsize);
		bottom_pos = i*fba_num_bufsize;
		r = pwrite(fd, buf1, bufsize, (off_t)(bottom_pos << SCTRSHFT));
		if (r <= 0) {
			perror("Test6: write");
			err++;
			return (err);
		}
		gen_data(buf2, bufsize);
		top_pos = (endloop - i - 1)*fba_num_bufsize;

		/* Make sure we don't collide in the middle */

		if (abs(top_pos - bottom_pos) < fba_num_bufsize)
			top_pos = bottom_pos + fba_num_bufsize;

		r = pwrite(fd, buf2, bufsize, (off_t)(top_pos << SCTRSHFT));
		if (r <= 0) {
			perror("Test6: write");
			err++;
			return (err);
		}
		r = pread(fd, buf3, bufsize, (off_t)(bottom_pos << SCTRSHFT));
		if (r <= 0) {
			perror("Test6: read");
			err++;
			return (err);
		}
		if (memcmp(buf1, buf3, bufsize)) {
			(void) printf("Data corruption(1), fd:%s, fpos:%"
			    PRId64 ", len:%d\n", name[h],
			    (int64_t)(bottom_pos << SCTRSHFT), bufsize);
			err++;
		}
		r = pread(fd, buf4, bufsize, (off_t)(top_pos << SCTRSHFT));
		if (r <= 0) {
			perror("Test6: read");
			return (1);
		}
		if (memcmp(buf2, buf4, bufsize)) {
			(void) printf("Test6: Data corruption(2),"
			    " fd:%s, fpos:%" PRId64 ", len:%d\n",
			    name[h], (int64_t)(top_pos << SCTRSHFT), bufsize);
			err++;
		}
	}
	return (err);
}

int shmid;

#define	MAXREADERS 32

struct shm_struct {
	int writebuf[MAXBUF];
	volatile nsc_off_t writepos;
	int quit;
	int err;
	mutex_t err_mutex;
	int rd_done[MAXREADERS];
	int rd_done_mask[MAXREADERS];
} *shm;

#define	WRITEBUF (shm->writebuf)
#define	WRITEPOS (shm->writepos)

#define	QUIT	(shm->quit)
#define	ERR	(shm->err)
#define	ERRMUTEX (shm->err_mutex)
#define	RD_DONE (shm->rd_done)
#define	RD_DONE_MASK (shm->rd_done_mask)

#define	LOCKWRITE
#define	LOCKREAD(i)

/*  Clear RD_DONE and Set WRITEPOS  */
#define	FREEWRITE { \
	bzero(RD_DONE, sizeof (RD_DONE)); \
	WRITEPOS = wr_pos; }

/*  Reader i+1 marks himself as finished  */
#define	FREEREAD(i) (RD_DONE[(i)] = 1)


int
do_sdtest7read(int fd, int h, int which)
{
	int err;
	ssize_t r_rd;
	nsc_off_t curr_pos;
	nsc_size_t loop_cnt;
	err = 0; curr_pos = 0; loop_cnt = 0;
	for (;;) {
		/* Already read this? */
		if (curr_pos == WRITEPOS) {
			if (!QUIT) {
				continue;
			} else {
				/*  Time to go!  */
				/* printf("Quitting [%d]\n", which+1); */
				break;
			}
		}

		/* get location to read from */
		curr_pos = WRITEPOS;

		r_rd = pread(fd, buf1, bufsize, (curr_pos << SCTRSHFT));
		loop_cnt += 1;
		if (r_rd <= 0) {
			FREEREAD(which);
			perror("Test7: read");
			err += 1;
			continue;
		}

		if (memcmp(buf1, WRITEBUF, bufsize)) {
			FREEREAD(which);
			(void) printf("\nTest7: Data corruption, reader #%d, "
			    "fd:%s, \
				fpos:%" PRId64 ", len:%d\n", which + 1, name[h],
				(int64_t)(curr_pos << SCTRSHFT), bufsize);
			err += 1;
			continue;
		}

		FREEREAD(which);
	}

	(void) printf(
	    "Partition %s, Test 7, reader #%d:  %d errors %lld loops\n",
		name[h], which+1, err, loop_cnt);

	if (err > 0) {
		(void) mutex_lock(&ERRMUTEX);
		ERR += err;
		(void) mutex_unlock(&ERRMUTEX);
	}

	if (err)
		return (1);
	else
		return (0);
}


int
do_sdtest7write(int fd, nsc_size_t filesize, int h)
{
	int err = 0;
	ssize_t r;
	nsc_off_t wr_pos;

	/*  Wait for readers to finish  */
	while (memcmp(RD_DONE, RD_DONE_MASK, readercount*sizeof (int)))
		;

	gen_data(WRITEBUF, bufsize);
	wr_pos = (
#ifdef NSC_MULTI_TERABYTE
	    ((nsc_off_t)rand() << 48) | ((nsc_off_t)rand() << 32) |
#endif
	    (rand() << 16) | rand()) % filesize;
	r = pwrite(fd, WRITEBUF, bufsize, (off_t)(wr_pos << SCTRSHFT));
	if (r <= 0) {
		FREEWRITE;
		perror("Test7: write");
		return (1);
	}
	FREEWRITE;

	/* verify write */
	r = pread(fd, buf1, bufsize, (off_t)(wr_pos << SCTRSHFT));
	if (r <= 0) {
		perror("Test7: writer: read");
		return (1);
	}


	if (memcmp(buf1, WRITEBUF, bufsize)) {
		(void) printf("\nTest7: Data corruption in writer,"
		    " fd:%s, fpos:%" PRId64 ", len:%d\n",
		    name[h], (int64_t)(wr_pos << SCTRSHFT), bufsize);
		err++;
	}


	return (err);
}

void
init_shm()
{
	int i;

	/*  Clear out everything  */
	bzero(shm, sizeof (struct shm_struct));

	(void) mutex_init(&ERRMUTEX, USYNC_PROCESS, NULL);

	/*   Set up mask (constant) to test reader doneness  */
	for (i = 0; i < readercount; i++)
		RD_DONE_MASK[i] = 1;

	/* Mark all readers done - so writer can start  */
	for (i = 0; i < readercount; i++)
		RD_DONE[i] = 1;
}

int
do_sdtest7(int fd, nsc_size_t loops, nsc_size_t filesize, int h, nsc_fd_t *sdfd)
{
	int r, i, err;
	nsc_size_t j;

	if ((shmid = shmget(IPC_PRIVATE, sizeof (struct shm_struct),
				IPC_CREAT | 0666)) < 0) {
		perror("shmget error: ");
		(void) nsc_close(sdfd);
		exit(1);
	}

	shm = (struct shm_struct *)shmat(shmid, NULL, 0);
	if (shm == (struct shm_struct *)-1) {
		perror("shmat error: ");
		(void) nsc_close(sdfd);
		exit(1); /* cleanup exits */
	}

	init_shm();

	/*  Start Readers  */
	for (i = 0; i < readercount; i++) {
		r = fork();
		if (r == 0) { /* child */
			(void) do_sdtest7read(fd, h, i);
			(void) nsc_close(sdfd);
			exit(0);
		} else
			continue;
	}

	/*  Start Writer  */
	srand(getpid()); err = 0;
	for (j = 0; j < loops; j++) {
		err += do_sdtest7write(fd, filesize, h);
	}
	QUIT = 1;

	(void) printf("\n\nPartition %s, Test 7, writer:  %d errors\n",
	    name[h], err);

	for (i = 0; i < readercount; i++)
		(void) wait(0);

	/*  No lock needed here - everybody's finished  */
	err += ERR;

	(void) mutex_destroy(&ERRMUTEX);
	(void) shmctl(shmid, IPC_RMID, 0);
	return (err);
}

int
do_sdtest8(int fd, nsc_size_t loops, nsc_size_t filesize)
{
	nsc_off_t seekpos;
	int err = 0;
	ssize_t r;
	nsc_size_t i;

	for (i = 0; i < loops; i++) {
		seekpos = (
#ifdef NSC_MULTI_TERABYTE
		    ((nsc_off_t)rand() << 48) | ((nsc_off_t)rand() << 32) |
#endif
		    (rand() << 16) | rand()) % filesize;
		gen_data(buf1, bufsize);
		r = pwrite(fd, buf1, bufsize, (off_t)(seekpos << SCTRSHFT));
		if (r <= 0) {
			perror("Test8: write");
			err++;
			return (err);
		}
	}
	return (err);
}

void
gen_data_known(int *buffer, int size, int data)
{
	int i;

	size /= 4;
	for (i = 0; i < size; i++)
		buffer[i] = data;
}

int
do_sdtest9(int fd, nsc_size_t loops, nsc_size_t filesize, int h)
{
	int err = 0;
	ssize_t r;
	nsc_off_t fba_offset;
	nsc_size_t i, wrapval;

	/*
	 * Test 9 will write a given pattern over and over Test 11 or
	 * Test 12 will read same pattern.
	 */
	/* Large loop value that would cause write overflow will wrap */

	gen_data_known(buf1, bufsize, pattern[h]);

	wrapval = filesize / fba_num_bufsize;

	if (loops == 0)
		loops = wrapval;  /* entire disk */

	for (i = 0; i < loops; i++) {
		fba_offset = i % wrapval;
		r = pwrite(fd, buf1, bufsize,
		    (off_t)(fba_offset * fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test9: write");
			err++;
			return (err);
		}
	}
	return (err);
}

int
do_sdtest10(int fd1, int fd2, nsc_size_t loops, nsc_size_t filesize1,
    nsc_size_t filesize2, int h)
{
	nsc_size_t filesize;
	int err = 0;
	nsc_size_t i;
	ssize_t r;

	/*
	 * Do sequential copy of disk1 to disk2 for loops number
	 * of bufsize chunks, unless loops == 0, then copy size of
	 * the smaller disk.
	 * Go back and verify that the two disks are identical.
	 */

	filesize = (filesize1 < filesize2) ? filesize1 : filesize2;
	if ((loops > (filesize / fba_num_bufsize)) || (!loops))
	    loops = filesize / fba_num_bufsize;

	/* copy disk1 to to disk2 */
	for (i = 0; i < loops; i++) {
		r = pread(fd1, buf1, bufsize,
		    (off_t)(i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test10: read");
			return (1);
		}
		r = pwrite(fd2, buf1, bufsize,
		    (off_t)(i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test10: write");
			return (1);
		}
	}

	/* verify disks are identical */
	for (i = 0; i < loops; i++) {
		buf1[0] = '\0';	/* clear buf to make sure something is read */
		r = pread(fd1, buf1, bufsize,
		    (off_t)(i * fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test10: read");
			return (1);
		}
		buf2[0] = 'x';	/* make sure something is read */
		r = pread(fd2, buf2, bufsize,
		    (off_t)(i * fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test10: read");
			return (1);
		}
		if (memcmp(buf1, buf2, bufsize)) {
			(void) printf("Test10: Data corruption,"
			    " fd1:%s, fd2:%s fpos:%" NSC_SZFMT ", len:%d\n",
			    name[2*h], name[2*h+1], i, bufsize);
			err++;
		}
	}
	return (err);
}

int
buffcmp(int *b1, int *b2, int size)
{
	int i;

	for (i = 0; i < size/4; i++) {
		if (b1[i] != b2[i]) {
			(void) printf("Word %d does not match b1=0x%x, "
			    "b2=0x%x\n", i, b1[i], b2[i]);
			return (1);
		}
	}
	return (0);

}

int
do_sdtest11(int fd, nsc_size_t loops, nsc_size_t filesize, int h)
{
	int err = 0;
	nsc_size_t i;
	ssize_t r;
	int buf3[MAXBUF];
	int buf4[MAXBUF];
	int timestamp;
	time_t clock;
	struct tm *tm;


	/*
	 * Test 9 will write a given pattern over and over Test 11 will read
	 * same pattern and clear with timestamp data (MM:SS).
	 */

	clock = time(NULL);
	tm  = localtime(&clock);
	(void) ascftime((char *)&timestamp, "%M""%S", tm);

	gen_data_known(buf1, bufsize, pattern[h]);
	gen_data_known(buf4, bufsize, timestamp);
	if ((loops > filesize / fba_num_bufsize) || (!loops))
		loops = filesize / fba_num_bufsize;  /* entire disk */

	for (i = 0; i < loops; i++) {
		r = pread(fd, buf3, bufsize,
		    (off_t)(i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test11: read");
			err++;
			return (err);
		}
		if (buffcmp(buf1, buf3, bufsize)) {
			(void) printf("Data corr, fd:%s, fpos:%" NSC_SZFMT
			", len:%d\n", name[h], i, bufsize);
			err++;
			return (err);
		}
		r = pwrite(fd, buf4, bufsize,
		    (off_t)(i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test11: write");
			err++;
			return (err);
		}
	}
	return (err);
}

int
do_sdtest12(int fd, nsc_size_t loops, nsc_size_t filesize, int h)
{
	int err = 0;
	nsc_size_t i;
	ssize_t r;
	int buf3[MAXBUF];

	/*
	 * Test 9 will write a given pattern over and over Test 12 will read
	 * same pattern
	 */

	gen_data_known(buf1, bufsize, pattern[h]);
	if ((loops > filesize / fba_num_bufsize) || (!loops))
		loops = filesize / fba_num_bufsize;  /* entire disk */

	for (i = 0; i < loops; i++) {
		r = pread(fd, buf3, bufsize,
		    (off_t)(i*fba_num_bufsize) << SCTRSHFT);
		if (r <= 0) {
			perror("Test12: read");
			err++;
			return (err);
		}
		if (buffcmp(buf1, buf3, bufsize)) {
			(void) printf("Data corr, fd:%s, fpos:%" NSC_SZFMT
			", len:%d\n", name[h], i, bufsize);
			err++;
			return (err);
		}
	}
	return (err);
}

#ifdef lint
int
sd_diag_lintmain(int argc, char *argv[])
#else
int
main(int argc, char *argv[])
#endif
{
	int procs;
	nsc_size_t filesize, filesize2;
	int fd, fd2, r, id, h, i;
	nsc_fd_t *sdfd, *sdfd2;

	if (argc < 2) {
		print_usage();
		exit(0);
	}
	(void) strcpy(config_file, DISKLIST);
	parse_opts(argc, argv);

	_nsc_nocheck();
	if ((procs = read_parts()) == 0)
		exit(0);

	id = strtol(argv[optind], 0, 0);
	if (id == 10) {
		/*
		 * each process gets 2 disks and copies disk1 to disk2,
		 * then goes back and verifies that the two disks are
		 * identical.
		 */
		if (procs < 2) {
		(void) printf("%s requires having at least 2 disks for test "
		    "#10.\n", config_file);
		exit(0);
		}

	    for (h = 0; h < procs/2; h++) {
		r = fork();
		if (r == 0) {
			srand(getpid());


			if (!(sdfd = nsc_open(name[2*h], NSC_CACHE,
					O_RDWR | Rflag))) {
				(void) fprintf(stderr,
				    "sd_diag: Error opening %s\n", name[2*h]);
				exit(1);
			}
			fd = nsc_fileno(sdfd);
			if (fd == -1) {
				(void) fprintf(stderr,
				    "sd_diag: Error opening %s\n", name[2*h]);
				(void) nsc_close(sdfd);
				exit(1);
			}
			filesize = set_part_size(name[2*h], sdfd);
			if (!(sdfd2 = nsc_open(name[2*h+1], NSC_CACHE,
					O_RDWR | Rflag))) {
				(void) fprintf(stderr,
				    "sd_diag: Error opening %s\n", name[2*h+1]);
				exit(1);
			}
			fd2 = nsc_fileno(sdfd2);
			if (fd2 == -1) {
				(void) fprintf(stderr,
				    "sd_diag: Error opening %s\n", name[2*h+1]);
				(void) nsc_close(sdfd2);
				exit(1);
			}
			filesize2 = set_part_size(name[2*h+1], sdfd2);
			(void) sleep(2);
			r = do_sdtest10(fd, fd2, loops, filesize, filesize2, h);

			(void) printf("Partitions %s and %s, Test %d,"
			    " Completed %d errors\n",
			    name[2*h], name[2*h+1], id, r);
			(void) nsc_close(sdfd);
			(void) nsc_close(sdfd2);
			exit(0);
		} else if (r == -1) {
			perror("fork");
			break;
		} else
			continue;
	    } /* for */
	    for (i = 0; i < h; i++)
		(void) wait(0);
	} else {

	for (h = 0; h < procs; h++) {
		r = fork();
		if (r == 0) {
			srand(getpid());

			id = strtol(argv[optind], 0, 0);
			if (!(sdfd = nsc_open(name[h], NSC_CACHE,
					O_RDWR | Rflag))) {
				(void) fprintf(stderr,
				    "sd_diag: Error opening %s\n", name[h]);
				exit(1);
			}
			fd = nsc_fileno(sdfd);

			if (fd == -1) {
				(void) fprintf(stderr,
				    "sd_diag: Error opening %s\n", name[h]);
				(void) nsc_close(sdfd);
				exit(1);
			}
			filesize = set_part_size(name[h], sdfd);

			(void) sleep(2);


			switch (id) {
			    case 1:
				r = do_sdtest1(fd, r_loops, filesize);
				break;
			    case 2:
				r = do_sdtest2(fd, r_loops, filesize, h);
				break;
			    case 3:
				r = do_sdtest3(fd, r_loops, filesize, h, sdfd);
				break;
			    case 4:
				r = do_sdtest4(fd, loops, filesize);
				break;
			    case 5:
				r = do_sdtest5(fd, loops, filesize, h);
				break;
			    case 6:
				r = do_sdtest6(fd, loops, filesize, h);
				break;
			    case 7:
				r = do_sdtest7(fd, r_loops, filesize, h, sdfd);
				break;
			    case 8:
				r = do_sdtest8(fd, r_loops, filesize);
				break;
			    case 9:
				r = do_sdtest9(fd, loops, filesize, h);
				break;
			    case 11:
				r = do_sdtest11(fd, loops, filesize, h);
				break;
			    case 12:
				r = do_sdtest12(fd, loops, filesize, h);
				break;
			    default:
				break;
			}

			(void) printf("Partition %s, Test %d, Completed %d "
			    "errors\n", name[h], id, r);
			(void) nsc_close(sdfd);
			exit(r ? 1 : 0);
		} else if (r == -1) {
			perror("fork");
			break;
		} else
			continue;
	}
	for (i = 0; i < h; i++)
		(void) wait(0);
	}

	return (0);
}