fsstress: translate flags in fiemap_f
[xfstests-dev.git] / src / seek_sanity_test.c
1 /*
2  * Copyright (C) 2011 Oracle.  All rights reserved.
3  * Copyright (C) 2011 Red Hat.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public
7  * License v2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 021110-1307, USA.
18  */
19
20 #define _XOPEN_SOURCE 500
21 #define _FILE_OFFSET_BITS 64
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/vfs.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <assert.h>
32
33 #ifndef SEEK_DATA
34 #define SEEK_DATA      3
35 #define SEEK_HOLE      4
36 #endif
37
38 static blksize_t alloc_size;
39 int default_behavior = 0;
40 char *base_file_path;
41
42 static void get_file_system(int fd)
43 {
44         struct statfs buf;
45
46         if (!fstatfs(fd, &buf)) {
47                 fprintf(stdout, "File system magic#: 0x%lx\n",
48                                 (unsigned long int)buf.f_type);
49         }
50 }
51
52 static int get_io_sizes(int fd)
53 {
54        struct stat buf;
55        int ret;
56
57        ret = fstat(fd, &buf);
58        if (ret)
59                fprintf(stderr, "  ERROR %d: Failed to find io blocksize\n",
60                        errno);
61
62        /* st_blksize is typically also the allocation size */
63        alloc_size = buf.st_blksize;
64        fprintf(stdout, "Allocation size: %ld\n", alloc_size);
65
66        return ret;
67 }
68
69 #define do_free(x)     do { if(x) free(x); } while(0);
70
71 static void *do_malloc(size_t size)
72 {
73        void *buf;
74
75        buf = malloc(size);
76        if (!buf)
77                fprintf(stderr, "  ERROR: Unable to allocate %ld bytes\n",
78                        (long)size);
79
80        return buf;
81 }
82
83 static int do_truncate(int fd, off_t length)
84 {
85        int ret;
86
87        ret = ftruncate(fd, length);
88        if (ret)
89                fprintf(stderr, "  ERROR %d: Failed to extend file "
90                        "to %ld bytes\n", errno, (long)length);
91        return ret;
92 }
93
94 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
95 {
96         int ret;
97
98         ret = fallocate(fd, mode, offset, length);
99         if (ret) {
100                 /* Don't warn about a filesystem w/o fallocate support */
101                 if (errno == EOPNOTSUPP)
102                         return ret;
103                 fprintf(stderr, "  ERROR %d: Failed to preallocate "
104                         "space to %ld bytes\n", errno, (long) length);
105         }
106
107         return ret;
108 }
109
110 /*
111  * Synchnorize all dirty pages in the file range starting from
112  * offset to nbytes length.
113  */
114 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
115 {
116         int ret;
117
118         ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
119         if (ret)
120                 fprintf(stderr, "  ERROR %d: Failed to sync out dirty "
121                         "pages\n", errno);
122
123         return ret;
124 }
125
126 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
127 {
128         ssize_t ret, written = 0;
129
130         while (count > written) {
131                 ret = pwrite(fd, buf + written, count - written, offset + written);
132                 if (ret < 0) {
133                         /* Don't warn about too large file. It's fs dependent. */
134                         if (errno == EFBIG)
135                                 return ret;
136                         fprintf(stderr, "  ERROR %d: Failed to write %ld "
137                                 "bytes\n", errno, (long)count);
138                         return ret;
139                 }
140                 written += ret;
141         }
142
143         return 0;
144 }
145
146 #define do_close(x)     do { if ((x) > -1) close(x); } while(0);
147
148 static int do_create(const char *filename)
149 {
150         int fd;
151
152         fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
153         if (fd < 0)
154                 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
155                         errno, filename);
156
157         return fd;
158 }
159
160 static int do_lseek(int testnum, int subtest, int fd, int filsz, int origin,
161                     off_t set, off_t exp)
162 {
163         off_t pos, exp2;
164         int x, ret;
165
166         assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
167
168         /*
169          * The file pointer can be set to different values depending
170          * on the implementation. For SEEK_HOLE, EOF could be a valid
171          * value. For SEEK_DATA, supplied offset could be the valid
172          * value.
173          */
174         exp2 = exp;
175         if (origin == SEEK_HOLE && exp2 != -1)
176                 exp2 = filsz;
177         if (origin == SEEK_DATA && default_behavior && set < filsz)
178                 exp2 = set;
179
180         pos = lseek(fd, set, origin);
181
182         if (pos == -1 && exp == -1) {
183                 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
184                             testnum, subtest,
185                             (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
186                             -ENXIO, -errno);
187                 ret = !(errno == ENXIO);
188         } else {
189
190                 x = fprintf(stdout, "%02d.%02d %s expected %ld or %ld, got %ld. ",
191                             testnum, subtest,
192                             (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
193                             (long)exp, (long)exp2, (long)pos);
194                 ret = !(pos == exp || pos == exp2);
195         }
196
197         fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
198
199         return ret;
200 }
201
202 static int huge_file_test(int fd, int testnum, off_t filsz)
203 {
204         char *buf = NULL;
205         int bufsz = alloc_size * 16;    /* XFS seems to round allocated size */
206         off_t off = filsz - 2*bufsz;
207         int ret = -1;
208
209         buf = do_malloc(bufsz);
210         if (!buf)
211                 goto out;
212         memset(buf, 'a', bufsz);
213
214         ret = do_pwrite(fd, buf, bufsz, 0);
215         if (ret)
216                 goto out;
217         ret = do_pwrite(fd, buf, bufsz, off);
218         if (ret) {
219                 /*
220                  * Report success. Filesystem just cannot handle so large
221                  * offsets and correctly reports it.
222                  */
223                 if (errno == EFBIG) {
224                         fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
225                         ret = 0;
226                 }
227                 goto out;
228         }
229
230         /* offset at the beginning */
231         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
232         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
233         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
234         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
235
236         /* offset around eof */
237         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off, off + bufsz);
238         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off, off);
239         ret += do_lseek(testnum,  7, fd, filsz, SEEK_DATA, off + 1, off + 1);
240         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off - bufsz, off);
241
242 out:
243         do_free(buf);
244         return ret;
245 }
246
247 /*
248  * Test huge file to check for overflows of block counts due to usage of
249  * 32-bit types.
250  */
251 static int test12(int fd, int testnum)
252 {
253         return huge_file_test(fd, testnum,
254                                 ((long long)alloc_size << 32) + (1 << 20));
255 }
256
257 /*
258  * Test huge file to check for overflows of block counts due to usage of
259  * signed types
260  */
261 static int test11(int fd, int testnum)
262 {
263         return huge_file_test(fd, testnum,
264                                 ((long long)alloc_size << 31) + (1 << 20));
265 }
266
267 /* Test an 8G file to check for offset overflows at 1 << 32 */
268 static int test10(int fd, int testnum)
269 {
270         return huge_file_test(fd, testnum, 8ULL << 30);
271 }
272
273 /*
274  * test file with unwritten extents, have both dirty and
275  * writeback pages in page cache.
276  */
277 static int test09(int fd, int testnum)
278 {
279         int ret = 0;
280         char *buf = NULL;
281         int bufsz = alloc_size;
282         int filsz = 8 << 20;
283
284         /*
285          * HOLE - unwritten DATA in dirty page - HOLE -
286          * unwritten DATA in writeback page
287          */
288
289         /* Each unit is bufsz */
290         buf = do_malloc(bufsz);
291         if (!buf)
292                 goto out;
293         memset(buf, 'a', bufsz);
294
295         /* preallocate 8M space to file */
296         ret = do_fallocate(fd, 0, filsz, 0);
297         if (ret < 0) {
298                 /* Report success if fs doesn't support fallocate */
299                 if (errno == EOPNOTSUPP) {
300                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
301                         ret = 0;
302                 }
303                 goto out;
304         }
305
306         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
307         if (!ret) {
308                 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
309                 if (ret)
310                         goto out;
311         }
312
313         /*
314          * Sync out dirty pages from bufsz * 100, this will convert
315          * the dirty page to writeback.
316          */
317         ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
318         if (ret)
319                 goto out;
320
321         /* offset at the beginning */
322         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
323         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
324         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
325         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
326
327 out:
328         do_free(buf);
329         return ret;
330 }
331
332 /* test file with unwritten extent, only have writeback page */
333 static int test08(int fd, int testnum)
334 {
335         int ret = 0;
336         char *buf = NULL;
337         int bufsz = alloc_size;
338         int filsz = 4 << 20;
339
340         /* HOLE - unwritten DATA in writeback page */
341         /* Each unit is bufsz */
342         buf = do_malloc(bufsz);
343         if (!buf)
344                 goto out;
345         memset(buf, 'a', bufsz);
346
347         /* preallocate 4M space to file */
348         ret = do_fallocate(fd, 0, filsz, 0);
349         if (ret < 0) {
350                 /* Report success if fs doesn't support fallocate */
351                 if (errno == EOPNOTSUPP) {
352                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
353                         ret = 0;
354                 }
355                 goto out;
356         }
357
358         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
359         if (ret)
360                 goto out;
361
362         /* Sync out all file */
363         ret = do_sync_dirty_pages(fd, 0, 0);
364         if (ret)
365                 goto out;
366
367         /* offset at the beginning */
368         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
369         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
370         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
371         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
372
373 out:
374         do_free(buf);
375         return ret;
376 }
377
378 /*
379  * test file with unwritten extents, only have dirty pages
380  * in page cache.
381  */
382 static int test07(int fd, int testnum)
383 {
384         int ret = 0;
385         char *buf = NULL;
386         int bufsz = alloc_size;
387         int filsz = 4 << 20;
388
389         /* HOLE - unwritten DATA in dirty page */
390         /* Each unit is bufsz */
391         buf = do_malloc(bufsz);
392         if (!buf)
393                 goto out;
394         memset(buf, 'a', bufsz);
395
396         /* preallocate 4M space to file */
397         ret = do_fallocate(fd, 0, filsz, 0);
398         if (ret < 0) {
399                 /* Report success if fs doesn't support fallocate */
400                 if (errno == EOPNOTSUPP) {
401                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
402                         ret = 0;
403                 }
404                 goto out;
405         }
406
407         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
408         if (ret)
409                 goto out;
410
411         /* offset at the beginning */
412         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
413         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
414         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
415         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
416
417 out:
418         do_free(buf);
419         return ret;
420 }
421
422 /* test hole data hole data */
423 static int test06(int fd, int testnum)
424 {
425         int ret = -1;
426         char *buf = NULL;
427         int bufsz = alloc_size;
428         int filsz = bufsz * 4;
429         int off;
430
431         /* HOLE - DATA - HOLE - DATA */
432         /* Each unit is bufsz */
433
434         buf = do_malloc(bufsz);
435         if (!buf)
436                 goto out;
437
438         memset(buf, 'a', bufsz);
439
440         ret = do_pwrite(fd, buf, bufsz, bufsz);
441         if (!ret)
442                 do_pwrite(fd, buf, bufsz, bufsz * 3);
443         if (ret)
444                 goto out;
445
446         /* offset at the beginning */
447         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
448         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
449         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz);
450         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz);
451
452         /* offset around first hole-data boundary */
453         off = bufsz;
454         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
455         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off - 1, off);
456         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, off,     bufsz * 2);
457         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off,     off);
458         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
459         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
460
461         /* offset around data-hole boundary */
462         off = bufsz * 2;
463         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
464         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
465         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off,     off);
466         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off,     bufsz * 3);
467         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
468         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
469
470         /* offset around second hole-data boundary */
471         off = bufsz * 3;
472         ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
473         ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
474         ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off,     filsz);
475         ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off,     off);
476         ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
477         ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
478
479         /* offset around the end of file */
480         off = filsz;
481         ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
482         ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
483         ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
484         ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
485         ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
486         ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
487
488 out:
489         do_free(buf);
490         return ret;
491 }
492
493 /* test file with data at the beginning and a hole at the end */
494 static int test05(int fd, int testnum)
495 {
496         int ret = -1;
497         char *buf = NULL;
498         int bufsz = alloc_size;
499         int filsz = bufsz * 4;
500
501         /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
502
503         buf = do_malloc(bufsz);
504         if (!buf)
505                 goto out;
506         memset(buf, 'a', bufsz);
507
508         ret = do_truncate(fd, filsz);
509         if (!ret)
510                 ret = do_pwrite(fd, buf, bufsz, 0);
511         if (ret)
512                 goto out;
513
514         /* offset at the beginning */
515
516         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
517         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
518
519         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
520         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
521
522         /* offset around data-hole boundary */
523         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
524         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
525
526         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     bufsz);
527         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
528         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
529         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
530
531         /* offset around eof */
532         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
533         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
534         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
535         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
536         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
537         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
538 out:
539         do_free(buf);
540         return ret;
541 }
542 /* test hole begin and data end */
543 static int test04(int fd, int testnum)
544 {
545         int ret;
546         char *buf = "ABCDEFGH";
547         int bufsz, holsz, filsz;
548
549         bufsz = strlen(buf);
550         holsz = alloc_size * 2;
551         filsz = holsz + bufsz;
552
553         /* |- HOLE -|- HOLE -|- DATA -| */
554
555         ret = do_pwrite(fd, buf, bufsz, holsz);
556         if (ret)
557                 goto out;
558
559         /* offset at the beginning */
560         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
561         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
562         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, holsz);
563         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, holsz);
564         /* offset around hole-data boundary */
565         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
566         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
567         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, holsz,     filsz);
568         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, holsz,     holsz);
569         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
570         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
571
572         /* offset around eof */
573         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
574         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
575         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
576         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
577         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
578         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
579 out:
580         return ret;
581 }
582
583 /* test a larger full file */
584 static int test03(int fd, int testnum)
585 {
586         char *buf = NULL;
587         int bufsz = alloc_size * 2 + 100;
588         int filsz = bufsz;
589         int ret = -1;
590
591         buf = do_malloc(bufsz);
592         if (!buf)
593                 goto out;
594         memset(buf, 'a', bufsz);
595
596         ret = do_pwrite(fd, buf, bufsz, 0);
597         if (ret)
598                 goto out;
599
600         /* offset at the beginning */
601         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
602         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
603         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
604         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
605
606         /* offset around eof */
607         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
608         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
609         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     -1);
610         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
611         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
612         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
613
614 out:
615         do_free(buf);
616         return ret;
617 }
618
619 /* test tiny full file */
620 static int test02(int fd, int testnum)
621 {
622         int ret;
623         char buf[] = "ABCDEFGH";
624         int bufsz, filsz;
625
626         bufsz = strlen(buf);
627         filsz = bufsz;
628
629         /* |- DATA -| */
630
631         ret = do_pwrite(fd, buf, bufsz, 0);
632         if (ret)
633                 goto out;
634
635         ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
636         ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
637         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
638         ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
639         ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
640         ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz,     -1);
641         ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz,     -1);
642         ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
643         ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
644
645 out:
646         return ret;
647 }
648
649 /* test empty file */
650 static int test01(int fd, int testnum)
651 {
652         int ret = 0;
653
654         ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
655         ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
656         ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
657
658         return ret;
659 }
660
661 struct testrec {
662        int     test_num;
663        int     (*test_func)(int fd, int testnum);
664        char    *test_desc;
665 };
666
667 struct testrec seek_tests[] = {
668        {  1, test01, "Test empty file" },
669        {  2, test02, "Test a tiny full file" },
670        {  3, test03, "Test a larger full file" },
671        {  4, test04, "Test file hole at beg, data at end" },
672        {  5, test05, "Test file data at beg, hole at end" },
673        {  6, test06, "Test file hole data hole data" },
674        {  7, test07, "Test file with unwritten extents, only have dirty pages" },
675        {  8, test08, "Test file with unwritten extents, only have unwritten pages" },
676        {  9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
677        { 10, test10, "Test a huge file for offset overflow" },
678        { 11, test11, "Test a huge file for block number signed" },
679        { 12, test12, "Test a huge file for block number overflow" },
680 };
681
682 static int run_test(struct testrec *tr)
683 {
684         int ret = 0, fd = -1;
685         char filename[255];
686
687         snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
688
689         fd = do_create(filename);
690         if (fd > -1) {
691                 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
692                 ret = tr->test_func(fd, tr->test_num);
693                 printf("\n");
694         }
695
696         do_close(fd);
697         return ret;
698 }
699
700 static int test_basic_support(void)
701 {
702         int ret = -1, fd;
703         off_t pos;
704         char *buf = NULL;
705         int bufsz, filsz;
706
707         fd = do_create(base_file_path);
708         if (fd == -1)
709                 goto out;
710
711         get_file_system(fd);
712
713         ret = get_io_sizes(fd);
714         if (ret)
715                 goto out;
716
717         bufsz = alloc_size * 2;
718         filsz = bufsz * 2;
719
720         buf = do_malloc(bufsz);
721         if (!buf)
722                 goto out;
723         memset(buf, 'a', bufsz);
724
725         /* File with 2 allocated blocks.... */
726         ret = do_pwrite(fd, buf, bufsz, 0);
727         if (ret)
728                 goto out;
729
730         /* followed by a hole... */
731         ret = do_truncate(fd, filsz);
732         if (ret)
733                 goto out;
734
735         /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
736         pos = lseek(fd, 0, SEEK_DATA);
737         if (pos != -1)
738                 pos = lseek(fd, 0, SEEK_HOLE);
739         if (pos == -1) {
740                 fprintf(stderr, "Kernel does not support llseek(2) extensions "
741                         "SEEK_HOLE and/or SEEK_DATA. Aborting.\n");
742                 ret = -1;
743                 goto out;
744         }
745
746         if (pos == filsz) {
747                 default_behavior = 1;
748                 fprintf(stderr, "File system supports the default behavior.\n");
749         }
750
751         printf("\n");
752
753 out:
754         do_free(buf);
755         do_close(fd);
756         return ret;
757 }
758
759 void usage(char *cmd)
760 {
761         fprintf(stdout, "Usage: %s [-t] base_file_path\n", cmd);
762         exit(1);
763 }
764
765 int main(int argc, char **argv)
766 {
767         int ret = -1;
768         int i = 0;
769         int opt;
770         int check_support = 0;
771         int numtests = sizeof(seek_tests) / sizeof(struct testrec);
772
773         while ((opt = getopt(argc, argv, "t")) != -1) {
774                 switch (opt) {
775                 case 't':
776                         check_support++;
777                         break;
778                 default:
779                         usage(argv[0]);
780                 }
781         }
782
783         /* should be exactly one arg left, the filename */
784         if (optind != argc - 1)
785                 usage(argv[0]);
786
787         base_file_path = (char *)strdup(argv[optind]);
788
789         ret = test_basic_support();
790         if (ret || check_support)
791                 goto out;
792
793         for (i = 0; i < numtests; ++i) {
794                 ret = run_test(&seek_tests[i]);
795                 if (ret)
796                         break;
797         }
798
799 out:
800         free(base_file_path);
801         return ret;
802 }