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
|
/* Tests for truncate(2) call family - by D.C. van Moolenbroek */
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#define ITERATIONS 1
int max_error = 4;
#include "common.h"
#define TESTFILE "testfile"
#define TESTSIZE 4096
#define THRESHOLD 1048576
int main(int argc, char *argv[]);
void prepare(void);
int make_file(off_t size);
void check_file(int fd, off_t size, off_t hole_start, off_t hole_end);
void all_sizes(void (*call) (off_t osize, off_t nsize));
void test50a(void);
void test50b(void);
void test50c(void);
void test50d(void);
void sub50e(off_t osize, off_t nsize);
void test50e(void);
void sub50f(off_t osize, off_t nsize);
void test50f(void);
void sub50g(off_t osize, off_t nsize);
void test50g(void);
void sub50h(off_t osize, off_t nsize);
void test50h(void);
void sub50i(off_t size, off_t off, size_t len, int type);
void test50i(void);
/* Some of the sizes have been chosen in such a way that they should be on the
* edge of direct/single indirect/double indirect switchovers for a MINIX
* file system with 4K block size.
*/
static off_t sizes[] = {
0L, 1L, 511L, 512L, 513L, 1023L, 1024L, 1025L, 2047L, 2048L, 2049L, 3071L,
3072L, 3073L, 4095L, 4096L, 4097L, 16383L, 16384L, 16385L, 28671L, 28672L,
28673L, 65535L, 65536L, 65537L, 4222975L, 4222976L, 4222977L
};
static unsigned char *data;
int main(argc, argv)
int argc;
char *argv[];
{
int j, m = 0xFFFF;
start(50);
prepare();
if (argc == 2) m = atoi(argv[1]);
for (j = 0; j < ITERATIONS; j++) {
if (m & 00001) test50a();
if (m & 00002) test50b();
if (m & 00004) test50c();
if (m & 00010) test50d();
if (m & 00020) test50e();
if (m & 00040) test50f();
if (m & 00100) test50g();
if (m & 00200) test50h();
if (m & 00400) test50i();
}
quit();
return(-1); /* impossible */
}
void prepare()
{
size_t largest;
int i;
largest = 0;
for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
if (largest < sizes[i]) largest = sizes[i];
/* internal integrity check: this is needed for early tests */
assert(largest >= TESTSIZE);
data = malloc(largest);
if (data == NULL) e(1000);
srand(1);
for (i = 0; i < largest; i++)
data[i] = (unsigned char) (rand() % 255 + 1);
}
void all_sizes(call)
void(*call) (off_t osize, off_t nsize);
{
int i, j;
for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++)
for (j = 0; j < sizeof(sizes) / sizeof(sizes[0]); j++)
call(sizes[i], sizes[j]);
}
int make_file(size)
off_t size;
{
off_t off;
int fd, r;
if ((fd = open(TESTFILE, O_RDWR|O_CREAT|O_EXCL, 0600)) < 0) e(1001);
off = 0;
while (off < size) {
r = write(fd, data + off, size - off);
if (r != size - off) e(1002);
off += r;
}
return fd;
}
void check_file(fd, hole_start, hole_end, size)
int fd;
off_t hole_start;
off_t hole_end;
off_t size;
{
static unsigned char buf[16384];
struct stat statbuf;
off_t off;
int i, chunk;
/* The size must match. */
if (fstat(fd, &statbuf) != 0) e(1003);
if (statbuf.st_size != size) e(1004);
if (lseek(fd, 0L, SEEK_SET) != 0L) e(1005);
/* All bytes in the file must be equal to what we wrote, except for the bytes
* in the hole, which must be zero.
*/
for (off = 0; off < size; off += chunk) {
chunk = MIN(sizeof(buf), size - off);
if (read(fd, buf, chunk) != chunk) e(1006);
for (i = 0; i < chunk; i++) {
if (off + i >= hole_start && off + i < hole_end) {
if (buf[i] != 0) e(1007);
}
else {
if (buf[i] != data[off+i]) e(1008);
}
}
}
/* We must get back EOF at the end. */
if (read(fd, buf, sizeof(buf)) != 0) e(1009);
}
void test50a()
{
struct stat statbuf;
int fd;
subtest = 1;
if ((fd = open(TESTFILE, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) e(1);
if (write(fd, data, TESTSIZE) != TESTSIZE) e(2);
/* Negative sizes should result in EINVAL. */
if (truncate(TESTFILE, -1) != -1) e(3);
if (errno != EINVAL) e(4);
/* Make sure the file size did not change. */
if (fstat(fd, &statbuf) != 0) e(5);
if (statbuf.st_size != TESTSIZE) e(6);
close(fd);
if (unlink(TESTFILE) != 0) e(7);
/* An empty path should result in ENOENT. */
if (truncate("", 0) != -1) e(8);
if (errno != ENOENT) e(9);
/* A non-existing file name should result in ENOENT. */
if (truncate(TESTFILE"2", 0) != -1) e(10);
if (errno != ENOENT) e(11);
}
void test50b()
{
struct stat statbuf;
int fd;
subtest = 2;
if ((fd = open(TESTFILE, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) e(1);
if (write(fd, data, TESTSIZE) != TESTSIZE) e(2);
/* Negative sizes should result in EINVAL. */
if (ftruncate(fd, -1) != -1) e(3);
if (errno != EINVAL) e(4);
/* Make sure the file size did not change. */
if (fstat(fd, &statbuf) != 0) e(5);
if (statbuf.st_size != TESTSIZE) e(6);
close(fd);
/* Calls on an invalid file descriptor should return EBADF or EINVAL. */
if (ftruncate(fd, 0) != -1) e(7);
if (errno != EBADF && errno != EINVAL) e(8);
if ((fd = open(TESTFILE, O_RDONLY)) < 0) e(9);
/* Calls on a file opened read-only should return EBADF or EINVAL. */
if (ftruncate(fd, 0) != -1) e(10);
if (errno != EBADF && errno != EINVAL) e(11);
close(fd);
if (unlink(TESTFILE) != 0) e(12);
}
void test50c()
{
struct stat statbuf;
struct flock flock;
off_t off;
int fd;
subtest = 3;
if ((fd = open(TESTFILE, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) e(1);
if (write(fd, data, TESTSIZE) != TESTSIZE) e(2);
off = TESTSIZE / 2;
if (lseek(fd, off, SEEK_SET) != off) e(3);
flock.l_len = 0;
/* Negative sizes should result in EINVAL. */
flock.l_whence = SEEK_SET;
flock.l_start = -1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(4);
if (errno != EINVAL) e(5);
flock.l_whence = SEEK_CUR;
flock.l_start = -off - 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(6);
if (errno != EINVAL) e(7);
flock.l_whence = SEEK_END;
flock.l_start = -TESTSIZE - 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(8);
if (errno != EINVAL) e(9);
/* Make sure the file size did not change. */
if (fstat(fd, &statbuf) != 0) e(10);
if (statbuf.st_size != TESTSIZE) e(11);
/* Proper negative values should work, however. */
flock.l_whence = SEEK_CUR;
flock.l_start = -1;
if (fcntl(fd, F_FREESP, &flock) != 0) e(12);
if (fstat(fd, &statbuf) != 0) e(13);
if (statbuf.st_size != off - 1) e(14);
flock.l_whence = SEEK_END;
flock.l_start = -off + 1;
if (fcntl(fd, F_FREESP, &flock) != 0) e(15);
if (fstat(fd, &statbuf) != 0) e(16);
if (statbuf.st_size != 0L) e(17);
close(fd);
/* Calls on an invalid file descriptor should return EBADF or EINVAL. */
flock.l_whence = SEEK_SET;
flock.l_start = 0;
if (fcntl(fd, F_FREESP, &flock) != -1) e(18);
if (errno != EBADF && errno != EINVAL) e(19);
if ((fd = open(TESTFILE, O_RDONLY)) < 0) e(20);
/* Calls on a file opened read-only should return EBADF or EINVAL. */
if (fcntl(fd, F_FREESP, &flock) != -1) e(21);
if (errno != EBADF && errno != EINVAL) e(22);
close(fd);
if (unlink(TESTFILE) != 0) e(23);
}
void test50d()
{
struct stat statbuf;
struct flock flock;
off_t off;
int fd;
subtest = 4;
if ((fd = open(TESTFILE, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) e(1);
if (write(fd, data, TESTSIZE) != TESTSIZE) e(2);
off = TESTSIZE / 2;
if (lseek(fd, off, SEEK_SET) != off) e(3);
/* The given length must be positive. */
flock.l_whence = SEEK_CUR;
flock.l_start = 0;
flock.l_len = -1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(4);
if (errno != EINVAL) e(5);
/* Negative start positions are not allowed. */
flock.l_whence = SEEK_SET;
flock.l_start = -1;
flock.l_len = 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(6);
if (errno != EINVAL) e(7);
flock.l_whence = SEEK_CUR;
flock.l_start = -off - 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(8);
if (errno != EINVAL) e(9);
flock.l_whence = SEEK_END;
flock.l_start = -TESTSIZE - 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(10);
if (errno != EINVAL) e(11);
/* Start positions at or beyond the end of the file are no good, either. */
flock.l_whence = SEEK_SET;
flock.l_start = TESTSIZE;
if (fcntl(fd, F_FREESP, &flock) != -1) e(12);
if (errno != EINVAL) e(13);
flock.l_start = TESTSIZE + 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(13);
if (errno != EINVAL) e(14);
flock.l_whence = SEEK_CUR;
flock.l_start = TESTSIZE - off;
if (fcntl(fd, F_FREESP, &flock) != -1) e(15);
if (errno != EINVAL) e(16);
flock.l_whence = SEEK_END;
flock.l_start = 1;
if (fcntl(fd, F_FREESP, &flock) != -1) e(17);
if (errno != EINVAL) e(18);
/* End positions beyond the end of the file may be silently bounded. */
flock.l_whence = SEEK_SET;
flock.l_start = 0;
flock.l_len = TESTSIZE + 1;
if (fcntl(fd, F_FREESP, &flock) != 0) e(19);
flock.l_whence = SEEK_CUR;
flock.l_len = TESTSIZE - off + 1;
if (fcntl(fd, F_FREESP, &flock) != 0) e(20);
flock.l_whence = SEEK_END;
flock.l_start = -1;
flock.l_len = 2;
if (fcntl(fd, F_FREESP, &flock) != 0) e(21);
/* However, this must never cause the file size to change. */
if (fstat(fd, &statbuf) != 0) e(22);
if (statbuf.st_size != TESTSIZE) e(23);
close(fd);
/* Calls on an invalid file descriptor should return EBADF or EINVAL. */
flock.l_whence = SEEK_SET;
flock.l_start = 0;
if (fcntl(fd, F_FREESP, &flock) != -1) e(24);
if (errno != EBADF && errno != EINVAL) e(25);
if ((fd = open(TESTFILE, O_RDONLY)) < 0) e(26);
/* Calls on a file opened read-only should return EBADF or EINVAL. */
if (fcntl(fd, F_FREESP, &flock) != -1) e(27);
if (errno != EBADF && errno != EINVAL) e(28);
close(fd);
if (unlink(TESTFILE) != 0) e(29);
}
void sub50e(osize, nsize)
off_t osize;
off_t nsize;
{
int fd;
fd = make_file(osize);
if (truncate(TESTFILE, nsize) != 0) e(1);
check_file(fd, osize, nsize, nsize);
if (nsize < osize) {
if (truncate(TESTFILE, osize) != 0) e(2);
check_file(fd, nsize, osize, osize);
}
close(fd);
if (unlink(TESTFILE) != 0) e(3);
}
void test50e()
{
subtest = 5;
/* truncate(2) on a file that is open. */
all_sizes(sub50e);
}
void sub50f(osize, nsize)
off_t osize;
off_t nsize;
{
int fd;
fd = make_file(osize);
close(fd);
if (truncate(TESTFILE, nsize) != 0) e(1);
if ((fd = open(TESTFILE, O_RDONLY)) < 0) e(2);
check_file(fd, osize, nsize, nsize);
if (nsize < osize) {
close(fd);
if (truncate(TESTFILE, osize) != 0) e(3);
if ((fd = open(TESTFILE, O_RDONLY)) < 0) e(4);
check_file(fd, nsize, osize, osize);
}
close(fd);
if (unlink(TESTFILE) != 0) e(5);
}
void test50f()
{
subtest = 6;
/* truncate(2) on a file that is not open. */
all_sizes(sub50f);
}
void sub50g(osize, nsize)
off_t osize;
off_t nsize;
{
int fd;
fd = make_file(osize);
if (ftruncate(fd, nsize) != 0) e(1);
check_file(fd, osize, nsize, nsize);
if (nsize < osize) {
if (ftruncate(fd, osize) != 0) e(2);
check_file(fd, nsize, osize, osize);
}
close(fd);
if (unlink(TESTFILE) != 0) e(3);
}
void test50g()
{
subtest = 7;
/* ftruncate(2) on an open file. */
all_sizes(sub50g);
}
void sub50h(osize, nsize)
off_t osize;
off_t nsize;
{
struct flock flock;
int fd;
fd = make_file(osize);
flock.l_whence = SEEK_SET;
flock.l_start = nsize;
flock.l_len = 0;
if (fcntl(fd, F_FREESP, &flock) != 0) e(1);
check_file(fd, osize, nsize, nsize);
if (nsize < osize) {
flock.l_whence = SEEK_SET;
flock.l_start = osize;
flock.l_len = 0;
if (fcntl(fd, F_FREESP, &flock) != 0) e(2);
check_file(fd, nsize, osize, osize);
}
close(fd);
if (unlink(TESTFILE) != 0) e(3);
}
void test50h()
{
subtest = 8;
/* fcntl(2) with F_FREESP and l_len=0. */
all_sizes(sub50h);
}
void sub50i(size, off, len, type)
off_t size;
off_t off;
size_t len;
int type;
{
struct flock flock;
int fd;
fd = make_file(size);
switch (type) {
case 0:
flock.l_whence = SEEK_SET;
flock.l_start = off;
break;
case 1:
if (lseek(fd, off, SEEK_SET) != off) e(1);
flock.l_whence = SEEK_CUR;
flock.l_start = 0;
break;
case 2:
flock.l_whence = SEEK_END;
flock.l_start = off - size;
break;
default:
e(1);
}
flock.l_len = len;
if (fcntl(fd, F_FREESP, &flock) != 0) e(2);
check_file(fd, off, off + len, size);
/* Repeat the call in order to see whether the file system can handle holes
* while freeing up. If not, the server would typically crash; we need not
* check the results again.
*/
flock.l_whence = SEEK_SET;
flock.l_start = off;
if (fcntl(fd, F_FREESP, &flock) != 0) e(3);
close(fd);
if (unlink(TESTFILE) != 0) e(4);
}
void test50i()
{
off_t off;
int i, j, k, l;
subtest = 9;
/* fcntl(2) with F_FREESP and l_len>0. */
/* This loop determines the size of the test file. */
for (i = 0; i < sizeof(sizes) / sizeof(sizes[0]); i++) {
/* Big files simply take too long. We have to compromise here. */
if (sizes[i] >= THRESHOLD) continue;
/* This loop determines one of the two values for the offset. */
for (j = 0; j < sizeof(sizes) / sizeof(sizes[0]); j++) {
if (sizes[j] >= sizes[i]) continue;
/* This loop determines the other. */
for (k = 0; k < sizeof(sizes) / sizeof(sizes[0]); k++) {
if (sizes[k] > sizes[j]) continue;
/* Construct an offset by adding the two sizes. */
off = sizes[j] + sizes[k];
if (j + 1 < sizeof(sizes) / sizeof(sizes[0]) &&
off >= sizes[j + 1]) continue;
/* This loop determines the length of the hole. */
for (l = 0; l < sizeof(sizes) / sizeof(sizes[0]); l++) {
if (sizes[l] == 0 || off + sizes[l] > sizes[i])
continue;
/* This could have been a loop, too! */
sub50i(sizes[i], off, sizes[l], 0);
sub50i(sizes[i], off, sizes[l], 1);
sub50i(sizes[i], off, sizes[l], 2);
}
}
}
}
}
|