generic: hole punching followed by writes in the same range
[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 %lld or %lld, got %lld. ",
191                             testnum, subtest,
192                             (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
193                             (long long)exp, (long long)exp2, (long 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 file with unwritten extents, having non-contiguous dirty pages in
250  * the unwritten extent.
251  */
252 static int test16(int fd, int testnum)
253 {
254         int ret = 0;
255         char *buf = NULL;
256         int bufsz = sysconf(_SC_PAGE_SIZE);
257         int filsz = 4 << 20;
258
259         /* HOLE - unwritten DATA in dirty page */
260         /* Each unit is bufsz */
261         buf = do_malloc(bufsz);
262         if (!buf)
263                 goto out;
264         memset(buf, 'a', bufsz);
265
266         /* preallocate 4M space to file */
267         ret = do_fallocate(fd, 0, filsz, 0);
268         if (ret < 0) {
269                 /* Report success if fs doesn't support fallocate */
270                 if (errno == EOPNOTSUPP) {
271                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
272                         ret = 0;
273                 }
274                 goto out;
275         }
276
277         ret = do_pwrite(fd, buf, bufsz, 0);
278         if (ret)
279                 goto out;
280
281         ret = do_pwrite(fd, buf, bufsz, filsz/2);
282         if (ret)
283                 goto out;
284
285         /* offset at the beginning */
286         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
287         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
288         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
289         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
290         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
291         ret += do_lseek(testnum,  6, fd, filsz, SEEK_HOLE, filsz/2,
292                         filsz/2 + bufsz);
293
294 out:
295         do_free(buf);
296         return ret;
297 }
298
299 /*
300  * test file with unwritten extents, having page just after end of unwritten
301  * extent.
302  */
303 static int test15(int fd, int testnum)
304 {
305         int ret = 0;
306         char *buf = NULL;
307         int bufsz = sysconf(_SC_PAGE_SIZE);
308         int filsz = 4 << 20;
309
310         /* HOLE - unwritten DATA in dirty page */
311         /* Each unit is bufsz */
312         buf = do_malloc(bufsz);
313         if (!buf)
314                 goto out;
315         memset(buf, 'a', bufsz);
316
317         /* preallocate 4M space to file */
318         ret = do_fallocate(fd, 0, filsz, 0);
319         if (ret < 0) {
320                 /* Report success if fs doesn't support fallocate */
321                 if (errno == EOPNOTSUPP) {
322                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
323                         ret = 0;
324                 }
325                 goto out;
326         }
327
328         ret = do_pwrite(fd, buf, bufsz, 0);
329         if (ret)
330                 goto out;
331
332         /* One page written just after end of unwritten extent... */
333         ret = do_pwrite(fd, buf, bufsz, filsz);
334         if (ret)
335                 goto out;
336
337         /* update file size */
338         filsz += bufsz;
339
340         /* offset at the beginning */
341         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
342         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
343         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
344         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
345         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
346
347 out:
348         do_free(buf);
349         return ret;
350 }
351
352 /*
353  * test file with unwritten extents, only have pagevec worth of dirty pages
354  * in page cache, a hole and then another page.
355  */
356 static int test14(int fd, int testnum)
357 {
358         int ret = 0;
359         char *buf = NULL;
360         int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
361         int filsz = 4 << 20;
362
363         /* HOLE - unwritten DATA in dirty page */
364         /* Each unit is bufsz */
365         buf = do_malloc(bufsz);
366         if (!buf)
367                 goto out;
368         memset(buf, 'a', bufsz);
369
370         /* preallocate 4M space to file */
371         ret = do_fallocate(fd, 0, filsz, 0);
372         if (ret < 0) {
373                 /* Report success if fs doesn't support fallocate */
374                 if (errno == EOPNOTSUPP) {
375                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
376                         ret = 0;
377                 }
378                 goto out;
379         }
380
381         ret = do_pwrite(fd, buf, bufsz, 0);
382         if (ret)
383                 goto out;
384
385         ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
386         if (ret)
387                 goto out;
388
389         /* offset at the beginning */
390         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
391         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
392         ret += do_lseek(testnum,  3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
393         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 0, 0);
394         ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, 1, 1);
395         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
396
397 out:
398         do_free(buf);
399         return ret;
400 }
401
402 /*
403  * test file with unwritten extents, only have pagevec worth of dirty pages
404  * in page cache.
405  */
406 static int test13(int fd, int testnum)
407 {
408         int ret = 0;
409         char *buf = NULL;
410         int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
411         int filsz = 4 << 20;
412
413         /* HOLE - unwritten DATA in dirty page */
414         /* Each unit is bufsz */
415         buf = do_malloc(bufsz);
416         if (!buf)
417                 goto out;
418         memset(buf, 'a', bufsz);
419
420         /* preallocate 4M space to file */
421         ret = do_fallocate(fd, 0, filsz, 0);
422         if (ret < 0) {
423                 /* Report success if fs doesn't support fallocate */
424                 if (errno == EOPNOTSUPP) {
425                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
426                         ret = 0;
427                 }
428                 goto out;
429         }
430
431         ret = do_pwrite(fd, buf, bufsz, 0);
432         if (ret)
433                 goto out;
434
435         /* offset at the beginning */
436         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
437         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
438         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
439         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
440
441 out:
442         do_free(buf);
443         return ret;
444 }
445
446 /*
447  * Test huge file to check for overflows of block counts due to usage of
448  * 32-bit types.
449  */
450 static int test12(int fd, int testnum)
451 {
452         return huge_file_test(fd, testnum,
453                                 ((long long)alloc_size << 32) + (1 << 20));
454 }
455
456 /*
457  * Test huge file to check for overflows of block counts due to usage of
458  * signed types
459  */
460 static int test11(int fd, int testnum)
461 {
462         return huge_file_test(fd, testnum,
463                                 ((long long)alloc_size << 31) + (1 << 20));
464 }
465
466 /* Test an 8G file to check for offset overflows at 1 << 32 */
467 static int test10(int fd, int testnum)
468 {
469         return huge_file_test(fd, testnum, 8ULL << 30);
470 }
471
472 /*
473  * test file with unwritten extents, have both dirty and
474  * writeback pages in page cache.
475  */
476 static int test09(int fd, int testnum)
477 {
478         int ret = 0;
479         char *buf = NULL;
480         int bufsz = alloc_size;
481         int filsz = bufsz * 100 + bufsz;
482
483         /*
484          * HOLE - unwritten DATA in dirty page - HOLE -
485          * unwritten DATA in writeback page
486          */
487
488         /* Each unit is bufsz */
489         buf = do_malloc(bufsz);
490         if (!buf)
491                 goto out;
492         memset(buf, 'a', bufsz);
493
494         /* preallocate 8M space to file */
495         ret = do_fallocate(fd, 0, filsz, 0);
496         if (ret < 0) {
497                 /* Report success if fs doesn't support fallocate */
498                 if (errno == EOPNOTSUPP) {
499                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
500                         ret = 0;
501                 }
502                 goto out;
503         }
504
505         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
506         if (!ret) {
507                 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
508                 if (ret)
509                         goto out;
510         }
511
512         /*
513          * Sync out dirty pages from bufsz * 100, this will convert
514          * the dirty page to writeback.
515          */
516         ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
517         if (ret)
518                 goto out;
519
520         /* offset at the beginning */
521         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
522         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
523         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
524         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
525
526 out:
527         do_free(buf);
528         return ret;
529 }
530
531 /* test file with unwritten extent, only have writeback page */
532 static int test08(int fd, int testnum)
533 {
534         int ret = 0;
535         char *buf = NULL;
536         int bufsz = alloc_size;
537         int filsz = bufsz * 10 + bufsz;
538
539         /* HOLE - unwritten DATA in writeback page */
540         /* Each unit is bufsz */
541         buf = do_malloc(bufsz);
542         if (!buf)
543                 goto out;
544         memset(buf, 'a', bufsz);
545
546         /* preallocate 4M space to file */
547         ret = do_fallocate(fd, 0, filsz, 0);
548         if (ret < 0) {
549                 /* Report success if fs doesn't support fallocate */
550                 if (errno == EOPNOTSUPP) {
551                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
552                         ret = 0;
553                 }
554                 goto out;
555         }
556
557         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
558         if (ret)
559                 goto out;
560
561         /* Sync out all file */
562         ret = do_sync_dirty_pages(fd, 0, 0);
563         if (ret)
564                 goto out;
565
566         /* offset at the beginning */
567         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
568         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
569         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
570         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
571
572 out:
573         do_free(buf);
574         return ret;
575 }
576
577 /*
578  * test file with unwritten extents, only have dirty pages
579  * in page cache.
580  */
581 static int test07(int fd, int testnum)
582 {
583         int ret = 0;
584         char *buf = NULL;
585         int bufsz = alloc_size;
586         int filsz = bufsz * 10 + bufsz;
587
588         /* HOLE - unwritten DATA in dirty page */
589         /* Each unit is bufsz */
590         buf = do_malloc(bufsz);
591         if (!buf)
592                 goto out;
593         memset(buf, 'a', bufsz);
594
595         /* preallocate 4M space to file */
596         ret = do_fallocate(fd, 0, filsz, 0);
597         if (ret < 0) {
598                 /* Report success if fs doesn't support fallocate */
599                 if (errno == EOPNOTSUPP) {
600                         fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
601                         ret = 0;
602                 }
603                 goto out;
604         }
605
606         ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
607         if (ret)
608                 goto out;
609
610         /* offset at the beginning */
611         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
612         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
613         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
614         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
615
616 out:
617         do_free(buf);
618         return ret;
619 }
620
621 /* test hole data hole data */
622 static int test06(int fd, int testnum)
623 {
624         int ret = -1;
625         char *buf = NULL;
626         int bufsz = alloc_size;
627         int filsz = bufsz * 4;
628         int off;
629
630         /* HOLE - DATA - HOLE - DATA */
631         /* Each unit is bufsz */
632
633         buf = do_malloc(bufsz);
634         if (!buf)
635                 goto out;
636
637         memset(buf, 'a', bufsz);
638
639         ret = do_pwrite(fd, buf, bufsz, bufsz);
640         if (!ret)
641                 do_pwrite(fd, buf, bufsz, bufsz * 3);
642         if (ret)
643                 goto out;
644
645         /* offset at the beginning */
646         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
647         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
648         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, bufsz);
649         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, bufsz);
650
651         /* offset around first hole-data boundary */
652         off = bufsz;
653         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
654         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off - 1, off);
655         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, off,     bufsz * 2);
656         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off,     off);
657         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
658         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
659
660         /* offset around data-hole boundary */
661         off = bufsz * 2;
662         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
663         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
664         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off,     off);
665         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off,     bufsz * 3);
666         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
667         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
668
669         /* offset around second hole-data boundary */
670         off = bufsz * 3;
671         ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
672         ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
673         ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off,     filsz);
674         ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off,     off);
675         ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
676         ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
677
678         /* offset around the end of file */
679         off = filsz;
680         ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
681         ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
682         ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
683         ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
684         ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
685         ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
686
687 out:
688         do_free(buf);
689         return ret;
690 }
691
692 /* test file with data at the beginning and a hole at the end */
693 static int test05(int fd, int testnum)
694 {
695         int ret = -1;
696         char *buf = NULL;
697         int bufsz = alloc_size;
698         int filsz = bufsz * 4;
699
700         /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
701
702         buf = do_malloc(bufsz);
703         if (!buf)
704                 goto out;
705         memset(buf, 'a', bufsz);
706
707         ret = do_truncate(fd, filsz);
708         if (!ret)
709                 ret = do_pwrite(fd, buf, bufsz, 0);
710         if (ret)
711                 goto out;
712
713         /* offset at the beginning */
714
715         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
716         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
717
718         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
719         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
720
721         /* offset around data-hole boundary */
722         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
723         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
724
725         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     bufsz);
726         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
727         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
728         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
729
730         /* offset around eof */
731         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
732         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
733         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
734         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
735         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
736         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
737 out:
738         do_free(buf);
739         return ret;
740 }
741 /* test hole begin and data end */
742 static int test04(int fd, int testnum)
743 {
744         int ret;
745         char *buf = "ABCDEFGH";
746         int bufsz, holsz, filsz;
747
748         bufsz = strlen(buf);
749         holsz = alloc_size * 2;
750         filsz = holsz + bufsz;
751
752         /* |- HOLE -|- HOLE -|- DATA -| */
753
754         ret = do_pwrite(fd, buf, bufsz, holsz);
755         if (ret)
756                 goto out;
757
758         /* offset at the beginning */
759         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, 0);
760         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, 1);
761         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, holsz);
762         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, holsz);
763         /* offset around hole-data boundary */
764         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
765         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
766         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, holsz,     filsz);
767         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, holsz,     holsz);
768         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
769         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
770
771         /* offset around eof */
772         ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
773         ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
774         ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz,     -1);
775         ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz,     -1);
776         ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
777         ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
778 out:
779         return ret;
780 }
781
782 /* test a larger full file */
783 static int test03(int fd, int testnum)
784 {
785         char *buf = NULL;
786         int bufsz = alloc_size * 2 + 100;
787         int filsz = bufsz;
788         int ret = -1;
789
790         buf = do_malloc(bufsz);
791         if (!buf)
792                 goto out;
793         memset(buf, 'a', bufsz);
794
795         ret = do_pwrite(fd, buf, bufsz, 0);
796         if (ret)
797                 goto out;
798
799         /* offset at the beginning */
800         ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
801         ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
802         ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
803         ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
804
805         /* offset around eof */
806         ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
807         ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
808         ret += do_lseek(testnum,  7, fd, filsz, SEEK_HOLE, bufsz,     -1);
809         ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, bufsz,     -1);
810         ret += do_lseek(testnum,  9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
811         ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
812
813 out:
814         do_free(buf);
815         return ret;
816 }
817
818 /* test tiny full file */
819 static int test02(int fd, int testnum)
820 {
821         int ret;
822         char buf[] = "ABCDEFGH";
823         int bufsz, filsz;
824
825         bufsz = strlen(buf);
826         filsz = bufsz;
827
828         /* |- DATA -| */
829
830         ret = do_pwrite(fd, buf, bufsz, 0);
831         if (ret)
832                 goto out;
833
834         ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
835         ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
836         ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
837         ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
838         ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
839         ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz,     -1);
840         ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz,     -1);
841         ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
842         ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
843
844 out:
845         return ret;
846 }
847
848 /* test empty file */
849 static int test01(int fd, int testnum)
850 {
851         int ret = 0;
852
853         ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
854         ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
855         ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
856
857         return ret;
858 }
859
860 struct testrec {
861        int     test_num;
862        int     (*test_func)(int fd, int testnum);
863        char    *test_desc;
864 };
865
866 struct testrec seek_tests[] = {
867        {  1, test01, "Test empty file" },
868        {  2, test02, "Test a tiny full file" },
869        {  3, test03, "Test a larger full file" },
870        {  4, test04, "Test file hole at beg, data at end" },
871        {  5, test05, "Test file data at beg, hole at end" },
872        {  6, test06, "Test file hole data hole data" },
873        {  7, test07, "Test file with unwritten extents, only have dirty pages" },
874        {  8, test08, "Test file with unwritten extents, only have unwritten pages" },
875        {  9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
876        { 10, test10, "Test a huge file for offset overflow" },
877        { 11, test11, "Test a huge file for block number signed" },
878        { 12, test12, "Test a huge file for block number overflow" },
879        { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
880        { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
881        { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
882        { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
883 };
884
885 static int run_test(struct testrec *tr)
886 {
887         int ret = 0, fd = -1;
888         char filename[255];
889
890         snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
891
892         fd = do_create(filename);
893         if (fd > -1) {
894                 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
895                 ret = tr->test_func(fd, tr->test_num);
896                 printf("\n");
897         }
898
899         do_close(fd);
900         return ret;
901 }
902
903 static int test_basic_support(void)
904 {
905         int ret = -1, fd, shift;
906         off_t pos = 0, offset = 1;
907         char *buf = NULL;
908         int bufsz, filsz;
909
910         fd = do_create(base_file_path);
911         if (fd == -1)
912                 goto out;
913
914         get_file_system(fd);
915
916         ret = get_io_sizes(fd);
917         if (ret)
918                 goto out;
919
920         /* try to discover the actual alloc size */
921         while (pos == 0 && offset < alloc_size) {
922                 offset <<= 1;
923                 ftruncate(fd, 0);
924                 pwrite(fd, "a", 1, offset);
925                 pos = lseek(fd, 0, SEEK_DATA);
926         }
927
928         /* bisect */
929         shift = offset >> 2;
930         while (shift && offset < alloc_size) {
931                 ftruncate(fd, 0);
932                 pwrite(fd, "a", 1, offset);
933                 pos = lseek(fd, 0, SEEK_DATA);
934                 offset += pos ? -shift : shift;
935                 shift >>= 1;
936         }
937         if (!shift)
938                 offset += pos ? 0 : 1;
939         alloc_size = offset;
940
941         if (pos == -1) {
942                 fprintf(stderr, "Kernel does not support llseek(2) extension "
943                         "SEEK_DATA. Aborting.\n");
944                 ret = -1;
945                 goto out;
946         }
947
948         ftruncate(fd, 0);
949         bufsz = alloc_size * 2;
950         filsz = bufsz * 2;
951
952         buf = do_malloc(bufsz);
953         if (!buf)
954                 goto out;
955         memset(buf, 'a', bufsz);
956
957         /* File with 2 allocated blocks.... */
958         ret = do_pwrite(fd, buf, bufsz, 0);
959         if (ret)
960                 goto out;
961
962         /* followed by a hole... */
963         ret = do_truncate(fd, filsz);
964         if (ret)
965                 goto out;
966
967         /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
968         pos = lseek(fd, 0, SEEK_HOLE);
969         if (pos == -1) {
970                 fprintf(stderr, "Kernel does not support llseek(2) extension "
971                         "SEEK_HOLE. Aborting.\n");
972                 ret = -1;
973                 goto out;
974         }
975
976         if (pos == filsz) {
977                 default_behavior = 1;
978                 fprintf(stderr, "File system supports the default behavior.\n");
979         }
980
981         printf("\n");
982
983 out:
984         do_free(buf);
985         do_close(fd);
986         return ret;
987 }
988
989 void usage(char *cmd)
990 {
991         fprintf(stdout, "Usage: %s [-t] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
992         exit(1);
993 }
994
995 int main(int argc, char **argv)
996 {
997         int ret = -1;
998         int i = 0;
999         int opt;
1000         int check_support = 0;
1001         int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1002         int teststart, testend;
1003
1004         /*
1005          * First twelve tests are used by generic/285. To run these is the
1006          * default. Further tests are run e.g. by generic/436. They are not
1007          * run by default in order to not regress older kernels.
1008          */
1009         teststart = 1;
1010         testend = 12;
1011
1012         while ((opt = getopt(argc, argv, "ts:e:")) != -1) {
1013                 switch (opt) {
1014                 case 't':
1015                         check_support++;
1016                         break;
1017                 case 's':
1018                         teststart = strtol(optarg, NULL, 10);
1019                         if (teststart <= 0 || teststart > numtests) {
1020                                 fprintf(stdout, "Invalid starting test: %s\n",
1021                                         optarg);
1022                                 usage(argv[0]);
1023                         }
1024                         break;
1025                 case 'e':
1026                         testend = strtol(optarg, NULL, 10);
1027                         if (testend <= 0 || testend > numtests) {
1028                                 fprintf(stdout, "Invalid final test: %s\n",
1029                                         optarg);
1030                                 usage(argv[0]);
1031                         }
1032                         break;
1033                 default:
1034                         usage(argv[0]);
1035                 }
1036         }
1037
1038         /* should be exactly one arg left, the filename */
1039         if (optind != argc - 1)
1040                 usage(argv[0]);
1041
1042         if (teststart > testend) {
1043                 fprintf(stdout, "Starting test is larger than final test!\n");
1044                 usage(argv[0]);
1045         }
1046
1047         base_file_path = (char *)strdup(argv[optind]);
1048
1049         ret = test_basic_support();
1050         if (ret || check_support)
1051                 goto out;
1052
1053         for (i = 0; i < numtests; ++i) {
1054                 if (seek_tests[i].test_num >= teststart &&
1055                     seek_tests[i].test_num <= testend) {
1056                         ret = run_test(&seek_tests[i]);
1057                         if (ret)
1058                                 break;
1059                 }
1060         }
1061
1062 out:
1063         free(base_file_path);
1064         return ret;
1065 }