generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / src / seek_sanity_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2011 Oracle.  All rights reserved.
4  * Copyright (C) 2011 Red Hat.  All rights reserved.
5  */
6
7 #define _XOPEN_SOURCE 500
8 #define _FILE_OFFSET_BITS 64
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/vfs.h>
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <assert.h>
19 #include "global.h"
20
21 #ifndef SEEK_DATA
22 #define SEEK_DATA      3
23 #define SEEK_HOLE      4
24 #endif
25
26 static blksize_t alloc_size;
27 int allow_default_behavior = 1;
28 int default_behavior = 0;
29 int unwritten_extents = 0;
30 int punch_hole = 0;
31 char *base_file_path;
32
33 static void get_file_system(int fd)
34 {
35         struct statfs buf;
36
37         if (!fstatfs(fd, &buf)) {
38                 fprintf(stdout, "File system magic#: 0x%lx\n",
39                                 (unsigned long int)buf.f_type);
40         }
41 }
42
43 /* Compute the file allocation unit size for an XFS file. */
44 static int detect_xfs_alloc_unit(int fd)
45 {
46         struct fsxattr fsx;
47         struct xfs_fsop_geom fsgeom;
48         int ret;
49
50         ret = ioctl(fd, XFS_IOC_FSGEOMETRY, &fsgeom);
51         if (ret)
52                 return -1;
53
54         ret = ioctl(fd, XFS_IOC_FSGETXATTR, &fsx);
55         if (ret)
56                 return -1;
57
58         alloc_size = fsgeom.blocksize;
59         if (fsx.fsx_xflags & XFS_XFLAG_REALTIME)
60                 alloc_size *= fsgeom.rtextsize;
61
62         return 0;
63 }
64
65 static int get_io_sizes(int fd)
66 {
67         off_t pos = 0, offset = 1;
68         struct stat buf;
69         int shift, ret;
70         int pagesz = sysconf(_SC_PAGE_SIZE);
71
72         ret = detect_xfs_alloc_unit(fd);
73         if (!ret)
74                 goto done;
75
76         ret = fstat(fd, &buf);
77         if (ret) {
78                 fprintf(stderr, "  ERROR %d: Failed to find io blocksize\n",
79                         errno);
80                 return ret;
81         }
82
83         /* st_blksize is typically also the allocation size */
84         alloc_size = buf.st_blksize;
85
86         /* try to discover the actual alloc size */
87         while (pos == 0 && offset < alloc_size) {
88                 offset <<= 1;
89                 ftruncate(fd, 0);
90                 pwrite(fd, "a", 1, offset);
91                 pos = lseek(fd, 0, SEEK_DATA);
92                 if (pos == -1)
93                         goto fail;
94         }
95
96         /* bisect */
97         shift = offset >> 2;
98         while (shift && offset < alloc_size) {
99                 ftruncate(fd, 0);
100                 pwrite(fd, "a", 1, offset);
101                 pos = lseek(fd, 0, SEEK_DATA);
102                 if (pos == -1)
103                         goto fail;
104                 offset += pos ? -shift : shift;
105                 shift >>= 1;
106         }
107         if (!shift)
108                 offset += pos ? 0 : 1;
109         alloc_size = offset;
110 done:
111         fprintf(stdout, "Allocation size: %ld\n", alloc_size);
112         return 0;
113
114 fail:
115         fprintf(stderr, "Kernel does not support llseek(2) extension "
116                 "SEEK_DATA. Aborting.\n");
117         return -1;
118 }
119
120 #define do_free(x)     do { if(x) free(x); } while(0);
121
122 static void *do_malloc(size_t size)
123 {
124        void *buf;
125
126        buf = malloc(size);
127        if (!buf)
128                fprintf(stderr, "  ERROR: Unable to allocate %ld bytes\n",
129                        (long)size);
130
131        return buf;
132 }
133
134 static int do_truncate(int fd, off_t length)
135 {
136        int ret;
137
138        ret = ftruncate(fd, length);
139        if (ret)
140                fprintf(stderr, "  ERROR %d: Failed to extend file "
141                        "to %ld bytes\n", errno, (long)length);
142        return ret;
143 }
144
145 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
146 {
147         int ret;
148
149         ret = fallocate(fd, mode, offset, length);
150         if (ret)
151                 fprintf(stderr, "  ERROR %d: Failed to %s of %ld bytes\n",
152                         errno, (mode & FALLOC_FL_PUNCH_HOLE) ? "punch hole" :
153                         "preallocate space", (long) length);
154
155         return ret;
156 }
157
158 /*
159  * Synchnorize all dirty pages in the file range starting from
160  * offset to nbytes length.
161  */
162 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
163 {
164         int ret;
165
166         ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
167         if (ret)
168                 fprintf(stderr, "  ERROR %d: Failed to sync out dirty "
169                         "pages\n", errno);
170
171         return ret;
172 }
173
174 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
175 {
176         ssize_t ret, written = 0;
177
178         while (count > written) {
179                 ret = pwrite(fd, buf + written, count - written, offset + written);
180                 if (ret < 0) {
181                         /* Don't warn about too large file. It's fs dependent. */
182                         if (errno == EFBIG)
183                                 return ret;
184                         fprintf(stderr, "  ERROR %d: Failed to write %ld "
185                                 "bytes\n", errno, (long)count);
186                         return ret;
187                 }
188                 written += ret;
189         }
190
191         return 0;
192 }
193
194 #define do_close(x)     do { if ((x) > -1) close(x); } while(0);
195
196 static int do_create(const char *filename)
197 {
198         int fd;
199
200         fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
201         if (fd < 0)
202                 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
203                         errno, filename);
204
205         return fd;
206 }
207
208 static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
209                     off_t set, off_t exp)
210 {
211         off_t pos, exp2;
212         int x, ret;
213
214         assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
215
216         /*
217          * The file pointer can be set to different values depending
218          * on the implementation. For SEEK_HOLE, EOF could be a valid
219          * value. For SEEK_DATA, supplied offset could be the valid
220          * value.
221          */
222         exp2 = exp;
223         if (origin == SEEK_HOLE && exp2 != -1)
224                 exp2 = filsz;
225         if (origin == SEEK_DATA && default_behavior && set < filsz)
226                 exp2 = set;
227
228         pos = lseek(fd, set, origin);
229
230         if (pos == -1 && exp == -1) {
231                 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
232                             testnum, subtest,
233                             (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
234                             -ENXIO, -errno);
235                 ret = !(errno == ENXIO);
236         } else {
237
238                 x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
239                             testnum, subtest,
240                             (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
241                             (long long)exp, (long long)exp2, (long long)pos);
242                 ret = !(pos == exp || pos == exp2);
243         }
244
245         fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
246
247         return ret;
248 }
249
250 static int huge_file_test(int fd, int testnum, off_t filsz)
251 {
252         char *buf = NULL;
253         int bufsz = alloc_size * 16;    /* XFS seems to round allocated size */
254         off_t off = filsz - bufsz;
255         int ret = -1;
256
257         buf = do_malloc(bufsz);
258         if (!buf)
259                 goto out;
260         memset(buf, 'a', bufsz);
261
262         /* |- DATA -|- HUGE HOLE -|- DATA -| */
263         ret = do_pwrite(fd, buf, bufsz, 0);
264         if (ret)
265                 goto out;
266         ret = do_pwrite(fd, buf, bufsz, off);
267         if (ret) {
268                 /*
269                  * Report success. Filesystem just cannot handle so large
270                  * offsets and correctly reports it.
271                  */
272                 if (errno == EFBIG) {
273                         fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
274                         ret = 0;
275                 }
276                 goto out;
277         }
278
279         /* offset at the beginning */
280         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
281         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
282         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
283         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
284
285         /* offset around eof */
286         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off, off + bufsz);
287         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off, off);
288         ret += do_lseek(testnum,  7, fd, filsz, SEEK_DATA, off + 1, off + 1);
289         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off - bufsz, off);
290
291 out:
292         do_free(buf);
293         return ret;
294 }
295
296 /*
297  * Make sure hole size is properly reported when punched in the middle of a file
298  */
299 static int test21(int fd, int testnum)
300 {
301         char *buf = NULL;
302         int bufsz, filsz;
303         int ret = 0;
304
305         if (!punch_hole) {
306                 fprintf(stdout, "Test skipped as fs doesn't support punch hole.\n");
307                 goto out;
308         }
309
310         if (default_behavior) {
311                 fprintf(stdout, "Test skipped as fs doesn't support seeking a punched hole.\n");
312                 goto out;
313         }
314
315         bufsz = alloc_size * 3;
316         buf = do_malloc(bufsz);
317         if (!buf) {
318                 ret = -1;
319                 goto out;
320         }
321         memset(buf, 'a', bufsz);
322
323         ret = do_pwrite(fd, buf, bufsz, 0);
324         if (ret)
325                 goto out;
326
327         filsz = bufsz;
328         ret = do_fallocate(fd, alloc_size, alloc_size,
329                            FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
330         if (ret < 0)
331                 goto out;
332
333         ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
334         ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, alloc_size);
335         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, alloc_size, alloc_size * 2);
336 out:
337         if (buf)
338                 free(buf);
339         return ret;
340 }
341
342 /*
343  * Make sure hole size is properly reported when starting in the middle of a
344  * hole in ext? doubly indirect tree
345  */
346 static int test20(int fd, int testnum)
347 {
348         int ret = -1;
349         char *buf = NULL;
350         loff_t bufsz, filsz;
351
352         bufsz = alloc_size;
353         buf = do_malloc(bufsz);
354         if (!buf)
355                 goto out;
356         memset(buf, 'a', bufsz);
357
358         /* Magic size in the middle of ext[23] triple indirect tree */
359         filsz = (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + 2 * bufsz / 4 + 5) * bufsz;
360         ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
361         if (ret) {
362                 /*
363                  * Report success. Filesystem just cannot handle so large
364                  * offsets and correctly reports it.
365                  */
366                 if (errno == EFBIG) {
367                         fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
368                         ret = 0;
369                 }
370                 goto out;
371         }
372
373         /* Offset inside ext[23] indirect block */
374         ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 14 * bufsz, filsz - bufsz);
375         /* Offset inside ext[23] doubly indirect block */
376         ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, (12 + 2 * bufsz / 4) * bufsz, filsz - bufsz);
377         /* Offsets inside ext[23] triply indirect block */
378         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
379                 (12 + bufsz / 4 + bufsz / 4 * bufsz / 4 + 3 * bufsz / 4 + 5) * bufsz, filsz - bufsz);
380         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
381                 (12 + bufsz / 4 + 7 * bufsz / 4 * bufsz / 4 + 5 * bufsz / 4) * bufsz, filsz - bufsz);
382         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
383                 (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + bufsz / 4 + 11) * bufsz, filsz - bufsz);
384 out:
385         if (buf)
386                 free(buf);
387         return ret;
388 }
389 /*
390  * Make sure hole size is properly reported when starting in the middle of a
391  * hole in ext? indirect tree
392  */
393 static int test19(int fd, int testnum)
394 {
395         int ret = -1;
396         char *buf = NULL;
397         int bufsz, filsz;
398
399         bufsz = alloc_size;
400         buf = do_malloc(bufsz);
401         if (!buf)
402                 goto out;
403         memset(buf, 'a', bufsz);
404
405         /* Magic size just beyond ext[23] indirect tree size */
406         filsz = (12 + bufsz / 4 + 1) * bufsz;
407         ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
408         if (ret)
409                 goto out;
410
411         ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
412         ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 12*bufsz, filsz - bufsz);
413         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, (12 + bufsz / 4 - 8)*bufsz, filsz - bufsz);
414 out:
415         if (buf)
416                 free(buf);
417         return ret;
418 }
419
420 /* Make sure we get ENXIO if we pass in a negative offset. */
421 static int test18(int fd, int testnum)
422 {
423         int ret = 0;
424
425         /* file size doesn't matter in this test, set to 0 */
426         ftruncate(fd, 0);
427
428         ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
429         ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
430         ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, LLONG_MIN, -1);
431         ret += do_lseek(testnum, 4, fd, 0, SEEK_DATA, LLONG_MIN, -1);
432
433         return ret;
434 }
435
436 static int test17(int fd, int testnum)
437 {
438         char *buf = NULL;
439         int pagesz = sysconf(_SC_PAGE_SIZE);
440         int bufsz, filsz;
441         int ret = 0;
442
443         if (!unwritten_extents) {
444                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
445                 goto out;
446         }
447
448         if (pagesz < 4 * alloc_size) {
449                 fprintf(stdout, "Test skipped as page size (%d) is less than "
450                         "four times allocation size (%d).\n",
451                         pagesz, (int)alloc_size);
452                 goto out;
453         }
454         bufsz = alloc_size;
455         filsz = 3 * bufsz;
456
457         buf = do_malloc(bufsz);
458         if (!buf) {
459                 ret = -1;
460                 goto out;
461         }
462         memset(buf, 'a', bufsz);
463
464         ret = do_fallocate(fd, 0, filsz, 0);
465         if (ret < 0)
466                 goto out;
467
468         ret = do_pwrite(fd, buf, bufsz, 0);
469         if (ret)
470                 goto out;
471
472         ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
473         if (ret)
474                 goto out;
475
476         ret += do_lseek(testnum,  1, fd, filsz, SEEK_DATA, 0, 0);
477         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 0, bufsz);
478         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 1, 1);
479         ret += do_lseek(testnum,  4, fd, filsz, SEEK_HOLE, 1, bufsz);
480         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
481         ret += do_lseek(testnum,  6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
482         ret += do_lseek(testnum,  7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
483         ret += do_lseek(testnum,  8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
484         ret += do_lseek(testnum,  9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
485         ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
486         ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
487         ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
488
489         filsz += bufsz;
490         ret += do_fallocate(fd, 0, filsz, 0);
491
492         ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
493         ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
494         ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
495         ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
496
497 out:
498         do_free(buf);
499         return ret;
500 }
501
502 /*
503  * test file with unwritten extents, having non-contiguous dirty pages in
504  * the unwritten extent.
505  */
506 static int test16(int fd, int testnum)
507 {
508         int ret = 0;
509         char *buf = NULL;
510         int bufsz = sysconf(_SC_PAGE_SIZE);
511         int filsz = 4 << 20;
512
513         if (!unwritten_extents) {
514                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
515                 goto out;
516         }
517
518         /* HOLE - unwritten DATA in dirty page */
519         /* Each unit is bufsz */
520         buf = do_malloc(bufsz);
521         if (!buf)
522                 goto out;
523         memset(buf, 'a', bufsz);
524
525         /* preallocate 4M space to file */
526         ret = do_fallocate(fd, 0, filsz, 0);
527         if (ret < 0)
528                 goto out;
529
530         ret = do_pwrite(fd, buf, bufsz, 0);
531         if (ret)
532                 goto out;
533
534         ret = do_pwrite(fd, buf, bufsz, filsz/2);
535         if (ret)
536                 goto out;
537
538         /* offset at the beginning */
539         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
540         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
541         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
542         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
543         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
544         ret += do_lseek(testnum,  6, fd, filsz, SEEK_HOLE, filsz/2,
545                         filsz/2 + bufsz);
546
547 out:
548         do_free(buf);
549         return ret;
550 }
551
552 /*
553  * test file with unwritten extents, having page just after end of unwritten
554  * extent.
555  */
556 static int test15(int fd, int testnum)
557 {
558         int ret = 0;
559         char *buf = NULL;
560         int bufsz = sysconf(_SC_PAGE_SIZE);
561         int filsz = 4 << 20;
562
563         if (!unwritten_extents) {
564                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
565                 goto out;
566         }
567
568         /* HOLE - unwritten DATA in dirty page */
569         /* Each unit is bufsz */
570         buf = do_malloc(bufsz);
571         if (!buf)
572                 goto out;
573         memset(buf, 'a', bufsz);
574
575         /* preallocate 4M space to file */
576         ret = do_fallocate(fd, 0, filsz, 0);
577         if (ret < 0)
578                 goto out;
579
580         ret = do_pwrite(fd, buf, bufsz, 0);
581         if (ret)
582                 goto out;
583
584         /* One page written just after end of unwritten extent... */
585         ret = do_pwrite(fd, buf, bufsz, filsz);
586         if (ret)
587                 goto out;
588
589         /* update file size */
590         filsz += bufsz;
591
592         /* offset at the beginning */
593         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
594         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
595         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
596         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
597         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
598
599 out:
600         do_free(buf);
601         return ret;
602 }
603
604 /*
605  * test file with unwritten extents, only have pagevec worth of dirty pages
606  * in page cache, a hole and then another page.
607  */
608 static int test14(int fd, int testnum)
609 {
610         int ret = 0;
611         char *buf = NULL;
612         int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
613         int filsz = 4 << 20;
614
615         if (!unwritten_extents) {
616                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
617                 goto out;
618         }
619
620         /* HOLE - unwritten DATA in dirty page */
621         /* Each unit is bufsz */
622         buf = do_malloc(bufsz);
623         if (!buf)
624                 goto out;
625         memset(buf, 'a', bufsz);
626
627         /* preallocate 4M space to file */
628         ret = do_fallocate(fd, 0, filsz, 0);
629         if (ret < 0)
630                 goto out;
631
632         ret = do_pwrite(fd, buf, bufsz, 0);
633         if (ret)
634                 goto out;
635
636         ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
637         if (ret)
638                 goto out;
639
640         /* offset at the beginning */
641         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
642         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
643         ret += do_lseek(testnum,  3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
644         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 0, 0);
645         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, 1, 1);
646         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
647
648 out:
649         do_free(buf);
650         return ret;
651 }
652
653 /*
654  * test file with unwritten extents, only have pagevec worth of dirty pages
655  * in page cache.
656  */
657 static int test13(int fd, int testnum)
658 {
659         int ret = 0;
660         char *buf = NULL;
661         int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
662         int filsz = 4 << 20;
663
664         if (!unwritten_extents) {
665                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
666                 goto out;
667         }
668
669         /* HOLE - unwritten DATA in dirty page */
670         /* Each unit is bufsz */
671         buf = do_malloc(bufsz);
672         if (!buf)
673                 goto out;
674         memset(buf, 'a', bufsz);
675
676         /* preallocate 4M space to file */
677         ret = do_fallocate(fd, 0, filsz, 0);
678         if (ret < 0)
679                 goto out;
680
681         ret = do_pwrite(fd, buf, bufsz, 0);
682         if (ret)
683                 goto out;
684
685         /* offset at the beginning */
686         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
687         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
688         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
689         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
690
691 out:
692         do_free(buf);
693         return ret;
694 }
695
696 /*
697  * Test huge file to check for overflows of block counts due to usage of
698  * 32-bit types.
699  */
700 static int test12(int fd, int testnum)
701 {
702         return huge_file_test(fd, testnum,
703                                 ((long long)alloc_size << 32) + (1 << 20));
704 }
705
706 /*
707  * Test huge file to check for overflows of block counts due to usage of
708  * signed types
709  */
710 static int test11(int fd, int testnum)
711 {
712         return huge_file_test(fd, testnum,
713                                 ((long long)alloc_size << 31) + (1 << 20));
714 }
715
716 /* Test an 8G file to check for offset overflows at 1 << 32 */
717 static int test10(int fd, int testnum)
718 {
719         return huge_file_test(fd, testnum, 8ULL << 30);
720 }
721
722 /*
723  * test file with unwritten extents, have both dirty and
724  * writeback pages in page cache.
725  */
726 static int test09(int fd, int testnum)
727 {
728         int ret = 0;
729         char *buf = NULL;
730         int bufsz = alloc_size;
731         int filsz = bufsz * 100 + bufsz;
732
733         if (!unwritten_extents) {
734                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
735                 goto out;
736         }
737
738         /*
739          * HOLE - unwritten DATA in dirty page - HOLE -
740          * unwritten DATA in writeback page
741          */
742
743         /* Each unit is bufsz */
744         buf = do_malloc(bufsz);
745         if (!buf)
746                 goto out;
747         memset(buf, 'a', bufsz);
748
749         /* preallocate 8M space to file */
750         ret = do_fallocate(fd, 0, filsz, 0);
751         if (ret < 0)
752                 goto out;
753
754         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
755         if (!ret) {
756                 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
757                 if (ret)
758                         goto out;
759         }
760
761         /*
762          * Sync out dirty pages from bufsz * 100, this will convert
763          * the dirty page to writeback.
764          */
765         ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
766         if (ret)
767                 goto out;
768
769         /* offset at the beginning */
770         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
771         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
772         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
773         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
774
775 out:
776         do_free(buf);
777         return ret;
778 }
779
780 /* test file with unwritten extent, only have writeback page */
781 static int test08(int fd, int testnum)
782 {
783         int ret = 0;
784         char *buf = NULL;
785         int bufsz = alloc_size;
786         int filsz = bufsz * 10 + bufsz;
787
788         if (!unwritten_extents) {
789                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
790                 goto out;
791         }
792
793         /* HOLE - unwritten DATA in writeback page */
794         /* Each unit is bufsz */
795         buf = do_malloc(bufsz);
796         if (!buf)
797                 goto out;
798         memset(buf, 'a', bufsz);
799
800         /* preallocate 4M space to file */
801         ret = do_fallocate(fd, 0, filsz, 0);
802         if (ret < 0)
803                 goto out;
804
805         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
806         if (ret)
807                 goto out;
808
809         /* Sync out all file */
810         ret = do_sync_dirty_pages(fd, 0, 0);
811         if (ret)
812                 goto out;
813
814         /* offset at the beginning */
815         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
816         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
817         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
818         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
819
820 out:
821         do_free(buf);
822         return ret;
823 }
824
825 /*
826  * test file with unwritten extents, only have dirty pages
827  * in page cache.
828  */
829 static int test07(int fd, int testnum)
830 {
831         int ret = 0;
832         char *buf = NULL;
833         int bufsz = alloc_size;
834         int filsz = bufsz * 10 + bufsz;
835
836         if (!unwritten_extents) {
837                 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
838                 goto out;
839         }
840
841         /* HOLE - unwritten DATA in dirty page */
842         /* Each unit is bufsz */
843         buf = do_malloc(bufsz);
844         if (!buf)
845                 goto out;
846         memset(buf, 'a', bufsz);
847
848         /* preallocate 4M space to file */
849         ret = do_fallocate(fd, 0, filsz, 0);
850         if (ret < 0)
851                 goto out;
852
853         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
854         if (ret)
855                 goto out;
856
857         /* offset at the beginning */
858         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
859         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
860         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
861         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
862
863 out:
864         do_free(buf);
865         return ret;
866 }
867
868 /* test hole data hole data */
869 static int test06(int fd, int testnum)
870 {
871         int ret = -1;
872         char *buf = NULL;
873         int bufsz = alloc_size;
874         int filsz = bufsz * 4;
875         int off;
876
877         /* HOLE - DATA - HOLE - DATA */
878         /* Each unit is bufsz */
879
880         buf = do_malloc(bufsz);
881         if (!buf)
882                 goto out;
883
884         memset(buf, 'a', bufsz);
885
886         ret = do_pwrite(fd, buf, bufsz, bufsz);
887         if (!ret)
888                 do_pwrite(fd, buf, bufsz, bufsz * 3);
889         if (ret)
890                 goto out;
891
892         /* offset at the beginning */
893         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
894         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
895         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz);
896         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz);
897
898         /* offset around first hole-data boundary */
899         off = bufsz;
900         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
901         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off - 1, off);
902         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, off,     bufsz * 2);
903         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off,     off);
904         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
905         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
906
907         /* offset around data-hole boundary */
908         off = bufsz * 2;
909         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
910         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
911         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off,     off);
912         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off,     bufsz * 3);
913         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
914         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
915
916         /* offset around second hole-data boundary */
917         off = bufsz * 3;
918         ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
919         ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
920         ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off,     filsz);
921         ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off,     off);
922         ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
923         ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
924
925         /* offset around the end of file */
926         off = filsz;
927         ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
928         ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
929         ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
930         ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
931         ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
932         ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
933
934 out:
935         do_free(buf);
936         return ret;
937 }
938
939 /* test file with data at the beginning and a hole at the end */
940 static int test05(int fd, int testnum)
941 {
942         int ret = -1;
943         char *buf = NULL;
944         int bufsz = alloc_size;
945         int filsz = bufsz * 4;
946
947         /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
948
949         buf = do_malloc(bufsz);
950         if (!buf)
951                 goto out;
952         memset(buf, 'a', bufsz);
953
954         ret = do_truncate(fd, filsz);
955         if (!ret)
956                 ret = do_pwrite(fd, buf, bufsz, 0);
957         if (ret)
958                 goto out;
959
960         /* offset at the beginning */
961
962         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
963         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
964
965         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
966         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
967
968         /* offset around data-hole boundary */
969         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
970         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
971
972         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     bufsz);
973         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
974         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
975         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
976
977         /* offset around eof */
978         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
979         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
980         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
981         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
982         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
983         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
984 out:
985         do_free(buf);
986         return ret;
987 }
988 /* test hole begin and data end */
989 static int test04(int fd, int testnum)
990 {
991         int ret;
992         char *buf = "ABCDEFGH";
993         int bufsz, holsz, filsz;
994
995         bufsz = strlen(buf);
996         holsz = alloc_size * 2;
997         filsz = holsz + bufsz;
998
999         /* |- HOLE -|- HOLE -|- DATA -| */
1000
1001         ret = do_pwrite(fd, buf, bufsz, holsz);
1002         if (ret)
1003                 goto out;
1004
1005         /* offset at the beginning */
1006         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
1007         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
1008         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, holsz);
1009         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, holsz);
1010         /* offset around hole-data boundary */
1011         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
1012         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
1013         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, holsz,     filsz);
1014         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, holsz,     holsz);
1015         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
1016         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
1017
1018         /* offset around eof */
1019         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
1020         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
1021         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
1022         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
1023         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
1024         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
1025 out:
1026         return ret;
1027 }
1028
1029 /* test a larger full file */
1030 static int test03(int fd, int testnum)
1031 {
1032         char *buf = NULL;
1033         int bufsz = alloc_size * 2 + 100;
1034         int filsz = bufsz;
1035         int ret = -1;
1036
1037         buf = do_malloc(bufsz);
1038         if (!buf)
1039                 goto out;
1040         memset(buf, 'a', bufsz);
1041
1042         ret = do_pwrite(fd, buf, bufsz, 0);
1043         if (ret)
1044                 goto out;
1045
1046         /* offset at the beginning */
1047         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
1048         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
1049         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
1050         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
1051
1052         /* offset around eof */
1053         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
1054         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1055         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     -1);
1056         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
1057         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1058         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1059
1060 out:
1061         do_free(buf);
1062         return ret;
1063 }
1064
1065 /* test tiny full file */
1066 static int test02(int fd, int testnum)
1067 {
1068         int ret;
1069         char buf[] = "ABCDEFGH";
1070         int bufsz, filsz;
1071
1072         bufsz = strlen(buf);
1073         filsz = bufsz;
1074
1075         /* |- DATA -| */
1076
1077         ret = do_pwrite(fd, buf, bufsz, 0);
1078         if (ret)
1079                 goto out;
1080
1081         ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
1082         ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
1083         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
1084         ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
1085         ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1086         ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz,     -1);
1087         ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz,     -1);
1088         ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1089         ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1090
1091 out:
1092         return ret;
1093 }
1094
1095 /* test empty file */
1096 static int test01(int fd, int testnum)
1097 {
1098         int ret = 0;
1099
1100         ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
1101         ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
1102         ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
1103
1104         return ret;
1105 }
1106
1107 struct testrec {
1108        int     test_num;
1109        int     (*test_func)(int fd, int testnum);
1110        char    *test_desc;
1111 };
1112
1113 struct testrec seek_tests[] = {
1114        {  1, test01, "Test empty file" },
1115        {  2, test02, "Test a tiny full file" },
1116        {  3, test03, "Test a larger full file" },
1117        {  4, test04, "Test file hole at beg, data at end" },
1118        {  5, test05, "Test file data at beg, hole at end" },
1119        {  6, test06, "Test file hole data hole data" },
1120        {  7, test07, "Test file with unwritten extents, only have dirty pages" },
1121        {  8, test08, "Test file with unwritten extents, only have unwritten pages" },
1122        {  9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
1123        { 10, test10, "Test a huge file for offset overflow" },
1124        { 11, test11, "Test a huge file for block number signed" },
1125        { 12, test12, "Test a huge file for block number overflow" },
1126        { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
1127        { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
1128        { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
1129        { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
1130        { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
1131        { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
1132        { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
1133        { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
1134        { 21, test21, "Test file SEEK_HOLE that was created by PUNCH_HOLE" },
1135 };
1136
1137 static int run_test(struct testrec *tr)
1138 {
1139         int ret = 0, fd = -1;
1140         char filename[255];
1141
1142         snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
1143
1144         fd = do_create(filename);
1145         if (fd > -1) {
1146                 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
1147                 ret = tr->test_func(fd, tr->test_num);
1148                 printf("\n");
1149         }
1150
1151         do_close(fd);
1152         return ret;
1153 }
1154
1155 static int test_basic_support(void)
1156 {
1157         int ret = -1, fd;
1158         off_t pos;
1159         char *buf = NULL;
1160         int bufsz, filsz;
1161
1162         fd = do_create(base_file_path);
1163         if (fd == -1)
1164                 goto out;
1165
1166         get_file_system(fd);
1167
1168         ret = get_io_sizes(fd);
1169         if (ret)
1170                 goto out;
1171
1172         ftruncate(fd, 0);
1173         bufsz = alloc_size * 2;
1174         filsz = bufsz * 2;
1175
1176         buf = do_malloc(bufsz);
1177         if (!buf)
1178                 goto out;
1179         memset(buf, 'a', bufsz);
1180
1181         /* File with 2 allocated blocks.... */
1182         ret = do_pwrite(fd, buf, bufsz, 0);
1183         if (ret)
1184                 goto out;
1185
1186         /* followed by a hole... */
1187         ret = do_truncate(fd, filsz);
1188         if (ret)
1189                 goto out;
1190
1191         /* Is SEEK_DATA supported in the kernel? */
1192         pos = lseek(fd, 0, SEEK_DATA);
1193         if (pos == -1) {
1194                 fprintf(stderr, "Kernel does not support llseek(2) extension "
1195                         "SEEK_DATA. Aborting.\n");
1196                 ret = -1;
1197                 goto out;
1198         }
1199
1200         /* Is SEEK_HOLE supported in the kernel? */
1201         pos = lseek(fd, 0, SEEK_HOLE);
1202         if (pos == -1) {
1203                 fprintf(stderr, "Kernel does not support llseek(2) extension "
1204                         "SEEK_HOLE. Aborting.\n");
1205                 ret = -1;
1206                 goto out;
1207         }
1208
1209         if (pos == filsz) {
1210                 default_behavior = 1;
1211                 fprintf(stderr, "File system supports the default behavior.\n");
1212         }
1213
1214         if (default_behavior && !allow_default_behavior) {
1215                 fprintf(stderr, "Default behavior is not allowed. Aborting.\n");
1216                 ret = -1;
1217                 goto out;
1218         }
1219
1220         /* Is unwritten extent supported? */
1221         ftruncate(fd, 0);
1222         if (fallocate(fd, 0, 0, alloc_size * 2) == -1) {
1223                 if (errno == EOPNOTSUPP) {
1224                         fprintf(stderr, "File system does not support fallocate.\n");
1225                 } else {
1226                         fprintf(stderr, "ERROR %d: Failed to preallocate "
1227                                 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1228                         ret = -1;
1229                 }
1230                 goto out;
1231         }
1232
1233         pos = lseek(fd, 0, SEEK_DATA);
1234         if (pos == 0)
1235                 fprintf(stderr, "File system does not support unwritten extents.\n");
1236         else
1237                 unwritten_extents = 1;
1238
1239         /* Is punch hole supported? */
1240         if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1241                       0, alloc_size) == -1)
1242                 fprintf(stderr, "File system does not support punch hole.\n");
1243         else
1244                 punch_hole = 1;
1245
1246         printf("\n");
1247
1248 out:
1249         do_free(buf);
1250         do_close(fd);
1251         return ret;
1252 }
1253
1254 void usage(char *cmd)
1255 {
1256         fprintf(stdout, "Usage: %s [-tf] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1257         exit(1);
1258 }
1259
1260 int main(int argc, char **argv)
1261 {
1262         int ret = -1;
1263         int i = 0;
1264         int opt;
1265         int check_support = 0;
1266         int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1267         int teststart, testend;
1268
1269         /*
1270          * First twelve tests are used by generic/285. To run these is the
1271          * default. Further tests are run e.g. by generic/436. They are not
1272          * run by default in order to not regress older kernels.
1273          */
1274         teststart = 1;
1275         testend = 12;
1276
1277         while ((opt = getopt(argc, argv, "tfs:e:")) != -1) {
1278                 switch (opt) {
1279                 case 't':
1280                         check_support++;
1281                         break;
1282                 case 'f':
1283                         allow_default_behavior = 0;
1284                         break;
1285                 case 's':
1286                         teststart = strtol(optarg, NULL, 10);
1287                         if (teststart <= 0 || teststart > numtests) {
1288                                 fprintf(stdout, "Invalid starting test: %s\n",
1289                                         optarg);
1290                                 usage(argv[0]);
1291                         }
1292                         break;
1293                 case 'e':
1294                         testend = strtol(optarg, NULL, 10);
1295                         if (testend <= 0 || testend > numtests) {
1296                                 fprintf(stdout, "Invalid final test: %s\n",
1297                                         optarg);
1298                                 usage(argv[0]);
1299                         }
1300                         break;
1301                 default:
1302                         usage(argv[0]);
1303                 }
1304         }
1305
1306         /* should be exactly one arg left, the filename */
1307         if (optind != argc - 1)
1308                 usage(argv[0]);
1309
1310         if (teststart > testend) {
1311                 fprintf(stdout, "Starting test is larger than final test!\n");
1312                 usage(argv[0]);
1313         }
1314
1315         base_file_path = (char *)strdup(argv[optind]);
1316
1317         ret = test_basic_support();
1318         if (ret || check_support)
1319                 goto out;
1320
1321         for (i = 0; i < numtests; ++i) {
1322                 if (seek_tests[i].test_num >= teststart &&
1323                     seek_tests[i].test_num <= testend) {
1324                         ret = run_test(&seek_tests[i]);
1325                         if (ret)
1326                                 break;
1327                 }
1328         }
1329
1330 out:
1331         free(base_file_path);
1332         return ret;
1333 }