-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathholes.c
211 lines (191 loc) · 5.25 KB
/
holes.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
/*
* Copyright (c) 2000-2001 Silicon Graphics, Inc.
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "global.h"
int verbose; /* print lots of debugging info */
void usage(char *progname);
void writeblks(int fd, long filesize, int blocksize,
int interleave, int base, int rev);
int readblks(int fd, long filesize, int blocksize,
int interleave, int count);
void
usage(char *progname)
{
fprintf(stderr, "usage: %s [-l filesize] [-b blocksize] [-i interleave]\n"
"\t\t[-c count] [-r(everse)] [-v(erbose)] filename\n",
progname);
exit(1);
}
int
main(int argc, char *argv[])
{
int interleave, blocksize, count, rev, i, ch, fd;
long filesize;
char *filename = NULL;
int errs;
filesize = 1024*1024;
blocksize = 4096;
count = interleave = 4;
rev = verbose = 0;
while ((ch = getopt(argc, argv, "b:l:i:c:rv")) != EOF) {
switch(ch) {
case 'b': blocksize = atoi(optarg); break;
case 'c': count = atoi(optarg); break;
case 'i': interleave = atoi(optarg); break;
case 'l': filesize = atol(optarg); break;
case 'v': verbose++; break;
case 'r': rev++; break;
default: usage(argv[0]); break;
}
}
if (optind == argc-1)
filename = argv[optind];
else
usage(argv[0]);
if ((filesize % (blocksize*interleave)) != 0) {
filesize -= filesize % (blocksize * interleave);
printf("filesize not a multiple of blocksize*interleave,\n");
printf("reducing filesize to %ld\n", filesize);
}
if (count > interleave) {
count = interleave;
printf("count of passes is too large, setting to %d\n", count);
} else if (count < 1) {
count = 1;
printf("count of passes is too small, setting to %d\n", count);
}
if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
perror("open");
return 1;
}
/*
* Avoid allocation patterns being perturbed by different speculative
* preallocation beyond EOF configurations by first truncating the file
* to the expected maximum file size.
*/
if (ftruncate(fd, filesize) < 0) {
perror("ftruncate");
exit(EXIT_FAILURE);
}
for (i = 0; i < count; i++) {
writeblks(fd, filesize, blocksize, interleave, i, rev);
}
errs=readblks(fd, filesize, blocksize, interleave, count);
if (close(fd) < 0) {
perror("close");
return 1;
}
if (errs) {
printf("%d errors detected during readback\n", errs);
return 1;
}
return 0;
}
void
writeblks(int fd, long filesize, int blocksize, int interleave, int base, int rev)
{
long offset;
char *buffer;
if ((buffer = calloc(1, blocksize)) == NULL) {
perror("malloc");
exit(1);
}
if (rev) {
offset = (filesize-blocksize) - (base * blocksize);
} else {
offset = base * blocksize;
}
for (;;) {
if (lseek(fd, offset, SEEK_SET) < 0) {
perror("lseek");
exit(1);
}
*(long *)buffer = *(long *)(buffer+256) = offset;
if (write(fd, buffer, blocksize) < blocksize) {
perror("write");
exit(1);
}
if (verbose) {
printf("writing data at offset=%ld, delta=%d, value 0x%lx and 0x%lx\n",
offset-base*blocksize, base,
*(long *)buffer,
*(long *)(buffer+256));
}
if (rev) {
offset -= interleave*blocksize;
if (offset < 0)
break;
} else {
offset += interleave*blocksize;
if (offset >= filesize)
break;
}
}
{
/* pad file out to full length */
char *zero="";
if (lseek(fd, filesize-1, SEEK_SET)<0) {
perror("lseek");
exit(1);
}
if (write(fd, zero, 1)!=1) {
perror("write");
exit(1);
}
}
free(buffer);
}
int
readblks(int fd, long filesize, int blocksize, int interleave, int count)
{
long offset;
char *buffer, *tmp;
int xfer, i;
int errs=0;
if ((buffer = calloc(interleave, blocksize)) == NULL) {
perror("malloc");
exit(1);
}
xfer = interleave * blocksize;
if (lseek(fd, (long)0, SEEK_SET) < 0) {
perror("lseek");
exit(1);
}
for (offset = 0; offset < filesize; offset += xfer) {
if ((i = read(fd, buffer, xfer) < xfer)) {
if (i == 0)
break;
if (i < 0)
perror("read");
printf("short read: %d of %d bytes read\n", i, xfer);
exit(1);
}
for (tmp = buffer, i = 0; i < count; i++, tmp += blocksize) {
if ( (*(long *)tmp != (offset+i*blocksize)) ||
(*(long *)(tmp+256) != (offset+i*blocksize)) ) {
printf("mismatched data at offset=%ld, delta=%d, expected 0x%lx, got 0x%lx and 0x%lx\n",
offset, i,
offset+i*blocksize,
*(long *)tmp,
*(long *)(tmp+256));
errs++;
}
}
}
free(buffer);
return errs;
}