generic: new case to test getcwd(2)
[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, off_t 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 - bufsz;
207         int ret = -1;
208
209         buf = do_malloc(bufsz);
210         if (!buf)
211                 goto out;
212         memset(buf, 'a', bufsz);
213
214         /* |- DATA -|- HUGE HOLE -|- DATA -| */
215         ret = do_pwrite(fd, buf, bufsz, 0);
216         if (ret)
217                 goto out;
218         ret = do_pwrite(fd, buf, bufsz, off);
219         if (ret) {
220                 /*
221                  * Report success. Filesystem just cannot handle so large
222                  * offsets and correctly reports it.
223                  */
224                 if (errno == EFBIG) {
225                         fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
226                         ret = 0;
227                 }
228                 goto out;
229         }
230
231         /* offset at the beginning */
232         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
233         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
234         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
235         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
236
237         /* offset around eof */
238         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off, off + bufsz);
239         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off, off);
240         ret += do_lseek(testnum,  7, fd, filsz, SEEK_DATA, off + 1, off + 1);
241         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off - bufsz, off);
242
243 out:
244         do_free(buf);
245         return ret;
246 }
247
248 /*
249  * Test huge file to check for overflows of block counts due to usage of
250  * 32-bit types.
251  */
252 static int test12(int fd, int testnum)
253 {
254         return huge_file_test(fd, testnum,
255                                 ((long long)alloc_size << 32) + (1 << 20));
256 }
257
258 /*
259  * Test huge file to check for overflows of block counts due to usage of
260  * signed types
261  */
262 static int test11(int fd, int testnum)
263 {
264         return huge_file_test(fd, testnum,
265                                 ((long long)alloc_size << 31) + (1 << 20));
266 }
267
268 /* Test an 8G file to check for offset overflows at 1 << 32 */
269 static int test10(int fd, int testnum)
270 {
271         return huge_file_test(fd, testnum, 8ULL << 30);
272 }
273
274 /*
275  * test file with unwritten extents, have both dirty and
276  * writeback pages in page cache.
277  */
278 static int test09(int fd, int testnum)
279 {
280         int ret = 0;
281         char *buf = NULL;
282         int bufsz = alloc_size;
283         int filsz = 8 << 20;
284
285         /*
286          * HOLE - unwritten DATA in dirty page - HOLE -
287          * unwritten DATA in writeback page
288          */
289
290         /* Each unit is bufsz */
291         buf = do_malloc(bufsz);
292         if (!buf)
293                 goto out;
294         memset(buf, 'a', bufsz);
295
296         /* preallocate 8M space to file */
297         ret = do_fallocate(fd, 0, filsz, 0);
298         if (ret < 0) {
299                 /* Report success if fs doesn't support fallocate */
300                 if (errno == EOPNOTSUPP) {
301                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
302                         ret = 0;
303                 }
304                 goto out;
305         }
306
307         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
308         if (!ret) {
309                 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
310                 if (ret)
311                         goto out;
312         }
313
314         /*
315          * Sync out dirty pages from bufsz * 100, this will convert
316          * the dirty page to writeback.
317          */
318         ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
319         if (ret)
320                 goto out;
321
322         /* offset at the beginning */
323         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
324         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
325         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
326         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
327
328 out:
329         do_free(buf);
330         return ret;
331 }
332
333 /* test file with unwritten extent, only have writeback page */
334 static int test08(int fd, int testnum)
335 {
336         int ret = 0;
337         char *buf = NULL;
338         int bufsz = alloc_size;
339         int filsz = 4 << 20;
340
341         /* HOLE - unwritten DATA in writeback page */
342         /* Each unit is bufsz */
343         buf = do_malloc(bufsz);
344         if (!buf)
345                 goto out;
346         memset(buf, 'a', bufsz);
347
348         /* preallocate 4M space to file */
349         ret = do_fallocate(fd, 0, filsz, 0);
350         if (ret < 0) {
351                 /* Report success if fs doesn't support fallocate */
352                 if (errno == EOPNOTSUPP) {
353                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
354                         ret = 0;
355                 }
356                 goto out;
357         }
358
359         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
360         if (ret)
361                 goto out;
362
363         /* Sync out all file */
364         ret = do_sync_dirty_pages(fd, 0, 0);
365         if (ret)
366                 goto out;
367
368         /* offset at the beginning */
369         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
370         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
371         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
372         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
373
374 out:
375         do_free(buf);
376         return ret;
377 }
378
379 /*
380  * test file with unwritten extents, only have dirty pages
381  * in page cache.
382  */
383 static int test07(int fd, int testnum)
384 {
385         int ret = 0;
386         char *buf = NULL;
387         int bufsz = alloc_size;
388         int filsz = 4 << 20;
389
390         /* HOLE - unwritten DATA in dirty page */
391         /* Each unit is bufsz */
392         buf = do_malloc(bufsz);
393         if (!buf)
394                 goto out;
395         memset(buf, 'a', bufsz);
396
397         /* preallocate 4M space to file */
398         ret = do_fallocate(fd, 0, filsz, 0);
399         if (ret < 0) {
400                 /* Report success if fs doesn't support fallocate */
401                 if (errno == EOPNOTSUPP) {
402                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
403                         ret = 0;
404                 }
405                 goto out;
406         }
407
408         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
409         if (ret)
410                 goto out;
411
412         /* offset at the beginning */
413         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
414         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
415         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
416         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
417
418 out:
419         do_free(buf);
420         return ret;
421 }
422
423 /* test hole data hole data */
424 static int test06(int fd, int testnum)
425 {
426         int ret = -1;
427         char *buf = NULL;
428         int bufsz = alloc_size;
429         int filsz = bufsz * 4;
430         int off;
431
432         /* HOLE - DATA - HOLE - DATA */
433         /* Each unit is bufsz */
434
435         buf = do_malloc(bufsz);
436         if (!buf)
437                 goto out;
438
439         memset(buf, 'a', bufsz);
440
441         ret = do_pwrite(fd, buf, bufsz, bufsz);
442         if (!ret)
443                 do_pwrite(fd, buf, bufsz, bufsz * 3);
444         if (ret)
445                 goto out;
446
447         /* offset at the beginning */
448         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
449         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
450         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz);
451         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz);
452
453         /* offset around first hole-data boundary */
454         off = bufsz;
455         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
456         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off - 1, off);
457         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, off,     bufsz * 2);
458         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off,     off);
459         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
460         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
461
462         /* offset around data-hole boundary */
463         off = bufsz * 2;
464         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
465         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
466         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off,     off);
467         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off,     bufsz * 3);
468         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
469         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
470
471         /* offset around second hole-data boundary */
472         off = bufsz * 3;
473         ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
474         ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
475         ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off,     filsz);
476         ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off,     off);
477         ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
478         ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
479
480         /* offset around the end of file */
481         off = filsz;
482         ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
483         ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
484         ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
485         ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
486         ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
487         ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
488
489 out:
490         do_free(buf);
491         return ret;
492 }
493
494 /* test file with data at the beginning and a hole at the end */
495 static int test05(int fd, int testnum)
496 {
497         int ret = -1;
498         char *buf = NULL;
499         int bufsz = alloc_size;
500         int filsz = bufsz * 4;
501
502         /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
503
504         buf = do_malloc(bufsz);
505         if (!buf)
506                 goto out;
507         memset(buf, 'a', bufsz);
508
509         ret = do_truncate(fd, filsz);
510         if (!ret)
511                 ret = do_pwrite(fd, buf, bufsz, 0);
512         if (ret)
513                 goto out;
514
515         /* offset at the beginning */
516
517         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
518         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
519
520         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
521         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
522
523         /* offset around data-hole boundary */
524         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
525         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
526
527         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     bufsz);
528         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
529         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
530         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
531
532         /* offset around eof */
533         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
534         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
535         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
536         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
537         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
538         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
539 out:
540         do_free(buf);
541         return ret;
542 }
543 /* test hole begin and data end */
544 static int test04(int fd, int testnum)
545 {
546         int ret;
547         char *buf = "ABCDEFGH";
548         int bufsz, holsz, filsz;
549
550         bufsz = strlen(buf);
551         holsz = alloc_size * 2;
552         filsz = holsz + bufsz;
553
554         /* |- HOLE -|- HOLE -|- DATA -| */
555
556         ret = do_pwrite(fd, buf, bufsz, holsz);
557         if (ret)
558                 goto out;
559
560         /* offset at the beginning */
561         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
562         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
563         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, holsz);
564         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, holsz);
565         /* offset around hole-data boundary */
566         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
567         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
568         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, holsz,     filsz);
569         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, holsz,     holsz);
570         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
571         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
572
573         /* offset around eof */
574         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
575         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
576         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
577         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
578         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
579         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
580 out:
581         return ret;
582 }
583
584 /* test a larger full file */
585 static int test03(int fd, int testnum)
586 {
587         char *buf = NULL;
588         int bufsz = alloc_size * 2 + 100;
589         int filsz = bufsz;
590         int ret = -1;
591
592         buf = do_malloc(bufsz);
593         if (!buf)
594                 goto out;
595         memset(buf, 'a', bufsz);
596
597         ret = do_pwrite(fd, buf, bufsz, 0);
598         if (ret)
599                 goto out;
600
601         /* offset at the beginning */
602         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
603         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
604         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
605         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
606
607         /* offset around eof */
608         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
609         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
610         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     -1);
611         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
612         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
613         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
614
615 out:
616         do_free(buf);
617         return ret;
618 }
619
620 /* test tiny full file */
621 static int test02(int fd, int testnum)
622 {
623         int ret;
624         char buf[] = "ABCDEFGH";
625         int bufsz, filsz;
626
627         bufsz = strlen(buf);
628         filsz = bufsz;
629
630         /* |- DATA -| */
631
632         ret = do_pwrite(fd, buf, bufsz, 0);
633         if (ret)
634                 goto out;
635
636         ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
637         ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
638         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
639         ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
640         ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
641         ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz,     -1);
642         ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz,     -1);
643         ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
644         ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
645
646 out:
647         return ret;
648 }
649
650 /* test empty file */
651 static int test01(int fd, int testnum)
652 {
653         int ret = 0;
654
655         ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
656         ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
657         ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
658
659         return ret;
660 }
661
662 struct testrec {
663        int     test_num;
664        int     (*test_func)(int fd, int testnum);
665        char    *test_desc;
666 };
667
668 struct testrec seek_tests[] = {
669        {  1, test01, "Test empty file" },
670        {  2, test02, "Test a tiny full file" },
671        {  3, test03, "Test a larger full file" },
672        {  4, test04, "Test file hole at beg, data at end" },
673        {  5, test05, "Test file data at beg, hole at end" },
674        {  6, test06, "Test file hole data hole data" },
675        {  7, test07, "Test file with unwritten extents, only have dirty pages" },
676        {  8, test08, "Test file with unwritten extents, only have unwritten pages" },
677        {  9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
678        { 10, test10, "Test a huge file for offset overflow" },
679        { 11, test11, "Test a huge file for block number signed" },
680        { 12, test12, "Test a huge file for block number overflow" },
681 };
682
683 static int run_test(struct testrec *tr)
684 {
685         int ret = 0, fd = -1;
686         char filename[255];
687
688         snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
689
690         fd = do_create(filename);
691         if (fd > -1) {
692                 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
693                 ret = tr->test_func(fd, tr->test_num);
694                 printf("\n");
695         }
696
697         do_close(fd);
698         return ret;
699 }
700
701 static int test_basic_support(void)
702 {
703         int ret = -1, fd;
704         off_t pos;
705         char *buf = NULL;
706         int bufsz, filsz;
707
708         fd = do_create(base_file_path);
709         if (fd == -1)
710                 goto out;
711
712         get_file_system(fd);
713
714         ret = get_io_sizes(fd);
715         if (ret)
716                 goto out;
717
718         bufsz = alloc_size * 2;
719         filsz = bufsz * 2;
720
721         buf = do_malloc(bufsz);
722         if (!buf)
723                 goto out;
724         memset(buf, 'a', bufsz);
725
726         /* File with 2 allocated blocks.... */
727         ret = do_pwrite(fd, buf, bufsz, 0);
728         if (ret)
729                 goto out;
730
731         /* followed by a hole... */
732         ret = do_truncate(fd, filsz);
733         if (ret)
734                 goto out;
735
736         /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
737         pos = lseek(fd, 0, SEEK_DATA);
738         if (pos != -1)
739                 pos = lseek(fd, 0, SEEK_HOLE);
740         if (pos == -1) {
741                 fprintf(stderr, "Kernel does not support llseek(2) extensions "
742                         "SEEK_HOLE and/or SEEK_DATA. Aborting.\n");
743                 ret = -1;
744                 goto out;
745         }
746
747         if (pos == filsz) {
748                 default_behavior = 1;
749                 fprintf(stderr, "File system supports the default behavior.\n");
750         }
751
752         printf("\n");
753
754 out:
755         do_free(buf);
756         do_close(fd);
757         return ret;
758 }
759
760 void usage(char *cmd)
761 {
762         fprintf(stdout, "Usage: %s [-t] base_file_path\n", cmd);
763         exit(1);
764 }
765
766 int main(int argc, char **argv)
767 {
768         int ret = -1;
769         int i = 0;
770         int opt;
771         int check_support = 0;
772         int numtests = sizeof(seek_tests) / sizeof(struct testrec);
773
774         while ((opt = getopt(argc, argv, "t")) != -1) {
775                 switch (opt) {
776                 case 't':
777                         check_support++;
778                         break;
779                 default:
780                         usage(argv[0]);
781                 }
782         }
783
784         /* should be exactly one arg left, the filename */
785         if (optind != argc - 1)
786                 usage(argv[0]);
787
788         base_file_path = (char *)strdup(argv[optind]);
789
790         ret = test_basic_support();
791         if (ret || check_support)
792                 goto out;
793
794         for (i = 0; i < numtests; ++i) {
795                 ret = run_test(&seek_tests[i]);
796                 if (ret)
797                         break;
798         }
799
800 out:
801         free(base_file_path);
802         return ret;
803 }