2 * Copyright (C) 2011 Oracle. All rights reserved.
3 * Copyright (C) 2011 Red Hat. All rights reserved.
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.
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.
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.
20 #define _XOPEN_SOURCE 500
21 #define _FILE_OFFSET_BITS 64
22 #include <sys/types.h>
38 static blksize_t alloc_size;
39 int default_behavior = 0;
42 static void get_file_system(int fd)
46 if (!fstatfs(fd, &buf)) {
47 fprintf(stdout, "File system magic#: 0x%lx\n",
48 (unsigned long int)buf.f_type);
52 static int get_io_sizes(int fd)
57 ret = fstat(fd, &buf);
59 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
62 /* st_blksize is typically also the allocation size */
63 alloc_size = buf.st_blksize;
64 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
69 #define do_free(x) do { if(x) free(x); } while(0);
71 static void *do_malloc(size_t size)
77 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
83 static int do_truncate(int fd, off_t length)
87 ret = ftruncate(fd, length);
89 fprintf(stderr, " ERROR %d: Failed to extend file "
90 "to %ld bytes\n", errno, (long)length);
94 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
98 ret = fallocate(fd, mode, offset, length);
100 /* Don't warn about a filesystem w/o fallocate support */
101 if (errno == EOPNOTSUPP)
103 fprintf(stderr, " ERROR %d: Failed to preallocate "
104 "space to %ld bytes\n", errno, (long) length);
111 * Synchnorize all dirty pages in the file range starting from
112 * offset to nbytes length.
114 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
118 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
120 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
126 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
128 ssize_t ret, written = 0;
130 while (count > written) {
131 ret = pwrite(fd, buf + written, count - written, offset + written);
133 /* Don't warn about too large file. It's fs dependent. */
136 fprintf(stderr, " ERROR %d: Failed to write %ld "
137 "bytes\n", errno, (long)count);
146 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
148 static int do_create(const char *filename)
152 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
154 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
160 static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
161 off_t set, off_t exp)
166 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
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
175 if (origin == SEEK_HOLE && exp2 != -1)
177 if (origin == SEEK_DATA && default_behavior && set < filsz)
180 pos = lseek(fd, set, origin);
182 if (pos == -1 && exp == -1) {
183 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
185 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
187 ret = !(errno == ENXIO);
190 x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
192 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
193 (long long)exp, (long long)exp2, (long long)pos);
194 ret = !(pos == exp || pos == exp2);
197 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
202 static int huge_file_test(int fd, int testnum, off_t filsz)
205 int bufsz = alloc_size * 16; /* XFS seems to round allocated size */
206 off_t off = filsz - bufsz;
209 buf = do_malloc(bufsz);
212 memset(buf, 'a', bufsz);
214 /* |- DATA -|- HUGE HOLE -|- DATA -| */
215 ret = do_pwrite(fd, buf, bufsz, 0);
218 ret = do_pwrite(fd, buf, bufsz, off);
221 * Report success. Filesystem just cannot handle so large
222 * offsets and correctly reports it.
224 if (errno == EFBIG) {
225 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
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);
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);
249 * test file with unwritten extents, having non-contiguous dirty pages in
250 * the unwritten extent.
252 static int test16(int fd, int testnum)
256 int bufsz = sysconf(_SC_PAGE_SIZE);
259 /* HOLE - unwritten DATA in dirty page */
260 /* Each unit is bufsz */
261 buf = do_malloc(bufsz);
264 memset(buf, 'a', bufsz);
266 /* preallocate 4M space to file */
267 ret = do_fallocate(fd, 0, filsz, 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");
277 ret = do_pwrite(fd, buf, bufsz, 0);
281 ret = do_pwrite(fd, buf, bufsz, filsz/2);
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,
300 * test file with unwritten extents, having page just after end of unwritten
303 static int test15(int fd, int testnum)
307 int bufsz = sysconf(_SC_PAGE_SIZE);
310 /* HOLE - unwritten DATA in dirty page */
311 /* Each unit is bufsz */
312 buf = do_malloc(bufsz);
315 memset(buf, 'a', bufsz);
317 /* preallocate 4M space to file */
318 ret = do_fallocate(fd, 0, filsz, 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");
328 ret = do_pwrite(fd, buf, bufsz, 0);
332 /* One page written just after end of unwritten extent... */
333 ret = do_pwrite(fd, buf, bufsz, filsz);
337 /* update file size */
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);
353 * test file with unwritten extents, only have pagevec worth of dirty pages
354 * in page cache, a hole and then another page.
356 static int test14(int fd, int testnum)
360 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
363 /* HOLE - unwritten DATA in dirty page */
364 /* Each unit is bufsz */
365 buf = do_malloc(bufsz);
368 memset(buf, 'a', bufsz);
370 /* preallocate 4M space to file */
371 ret = do_fallocate(fd, 0, filsz, 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");
381 ret = do_pwrite(fd, buf, bufsz, 0);
385 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
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);
403 * test file with unwritten extents, only have pagevec worth of dirty pages
406 static int test13(int fd, int testnum)
410 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
413 /* HOLE - unwritten DATA in dirty page */
414 /* Each unit is bufsz */
415 buf = do_malloc(bufsz);
418 memset(buf, 'a', bufsz);
420 /* preallocate 4M space to file */
421 ret = do_fallocate(fd, 0, filsz, 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");
431 ret = do_pwrite(fd, buf, bufsz, 0);
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);
447 * Test huge file to check for overflows of block counts due to usage of
450 static int test12(int fd, int testnum)
452 return huge_file_test(fd, testnum,
453 ((long long)alloc_size << 32) + (1 << 20));
457 * Test huge file to check for overflows of block counts due to usage of
460 static int test11(int fd, int testnum)
462 return huge_file_test(fd, testnum,
463 ((long long)alloc_size << 31) + (1 << 20));
466 /* Test an 8G file to check for offset overflows at 1 << 32 */
467 static int test10(int fd, int testnum)
469 return huge_file_test(fd, testnum, 8ULL << 30);
473 * test file with unwritten extents, have both dirty and
474 * writeback pages in page cache.
476 static int test09(int fd, int testnum)
480 int bufsz = alloc_size;
481 int filsz = bufsz * 100 + bufsz;
484 * HOLE - unwritten DATA in dirty page - HOLE -
485 * unwritten DATA in writeback page
488 /* Each unit is bufsz */
489 buf = do_malloc(bufsz);
492 memset(buf, 'a', bufsz);
494 /* preallocate 8M space to file */
495 ret = do_fallocate(fd, 0, filsz, 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");
505 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
507 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
513 * Sync out dirty pages from bufsz * 100, this will convert
514 * the dirty page to writeback.
516 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
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);
531 /* test file with unwritten extent, only have writeback page */
532 static int test08(int fd, int testnum)
536 int bufsz = alloc_size;
537 int filsz = bufsz * 10 + bufsz;
539 /* HOLE - unwritten DATA in writeback page */
540 /* Each unit is bufsz */
541 buf = do_malloc(bufsz);
544 memset(buf, 'a', bufsz);
546 /* preallocate 4M space to file */
547 ret = do_fallocate(fd, 0, filsz, 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");
557 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
561 /* Sync out all file */
562 ret = do_sync_dirty_pages(fd, 0, 0);
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);
578 * test file with unwritten extents, only have dirty pages
581 static int test07(int fd, int testnum)
585 int bufsz = alloc_size;
586 int filsz = bufsz * 10 + bufsz;
588 /* HOLE - unwritten DATA in dirty page */
589 /* Each unit is bufsz */
590 buf = do_malloc(bufsz);
593 memset(buf, 'a', bufsz);
595 /* preallocate 4M space to file */
596 ret = do_fallocate(fd, 0, filsz, 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");
606 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
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);
621 /* test hole data hole data */
622 static int test06(int fd, int testnum)
626 int bufsz = alloc_size;
627 int filsz = bufsz * 4;
630 /* HOLE - DATA - HOLE - DATA */
631 /* Each unit is bufsz */
633 buf = do_malloc(bufsz);
637 memset(buf, 'a', bufsz);
639 ret = do_pwrite(fd, buf, bufsz, bufsz);
641 do_pwrite(fd, buf, bufsz, bufsz * 3);
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);
651 /* offset around first hole-data boundary */
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);
660 /* offset around data-hole boundary */
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);
669 /* offset around second hole-data boundary */
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);
678 /* offset around the end of file */
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);
692 /* test file with data at the beginning and a hole at the end */
693 static int test05(int fd, int testnum)
697 int bufsz = alloc_size;
698 int filsz = bufsz * 4;
700 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
702 buf = do_malloc(bufsz);
705 memset(buf, 'a', bufsz);
707 ret = do_truncate(fd, filsz);
709 ret = do_pwrite(fd, buf, bufsz, 0);
713 /* offset at the beginning */
715 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
716 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
718 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
719 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
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);
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);
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);
741 /* test hole begin and data end */
742 static int test04(int fd, int testnum)
745 char *buf = "ABCDEFGH";
746 int bufsz, holsz, filsz;
749 holsz = alloc_size * 2;
750 filsz = holsz + bufsz;
752 /* |- HOLE -|- HOLE -|- DATA -| */
754 ret = do_pwrite(fd, buf, bufsz, holsz);
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);
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);
782 /* test a larger full file */
783 static int test03(int fd, int testnum)
786 int bufsz = alloc_size * 2 + 100;
790 buf = do_malloc(bufsz);
793 memset(buf, 'a', bufsz);
795 ret = do_pwrite(fd, buf, bufsz, 0);
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);
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);
818 /* test tiny full file */
819 static int test02(int fd, int testnum)
822 char buf[] = "ABCDEFGH";
830 ret = do_pwrite(fd, buf, bufsz, 0);
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);
848 /* test empty file */
849 static int test01(int fd, int testnum)
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);
862 int (*test_func)(int fd, int testnum);
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" },
885 static int run_test(struct testrec *tr)
887 int ret = 0, fd = -1;
890 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
892 fd = do_create(filename);
894 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
895 ret = tr->test_func(fd, tr->test_num);
903 static int test_basic_support(void)
905 int ret = -1, fd, shift;
906 off_t pos = 0, offset = 1;
910 fd = do_create(base_file_path);
916 ret = get_io_sizes(fd);
920 /* try to discover the actual alloc size */
921 while (pos == 0 && offset < alloc_size) {
924 pwrite(fd, "a", 1, offset);
925 pos = lseek(fd, 0, SEEK_DATA);
930 while (shift && offset < alloc_size) {
932 pwrite(fd, "a", 1, offset);
933 pos = lseek(fd, 0, SEEK_DATA);
934 offset += pos ? -shift : shift;
938 offset += pos ? 0 : 1;
942 fprintf(stderr, "Kernel does not support llseek(2) extension "
943 "SEEK_DATA. Aborting.\n");
949 bufsz = alloc_size * 2;
952 buf = do_malloc(bufsz);
955 memset(buf, 'a', bufsz);
957 /* File with 2 allocated blocks.... */
958 ret = do_pwrite(fd, buf, bufsz, 0);
962 /* followed by a hole... */
963 ret = do_truncate(fd, filsz);
967 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
968 pos = lseek(fd, 0, SEEK_HOLE);
970 fprintf(stderr, "Kernel does not support llseek(2) extension "
971 "SEEK_HOLE. Aborting.\n");
977 default_behavior = 1;
978 fprintf(stderr, "File system supports the default behavior.\n");
989 void usage(char *cmd)
991 fprintf(stdout, "Usage: %s [-t] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
995 int main(int argc, char **argv)
1000 int check_support = 0;
1001 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1002 int teststart, testend;
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.
1012 while ((opt = getopt(argc, argv, "ts:e:")) != -1) {
1018 teststart = strtol(optarg, NULL, 10);
1019 if (teststart <= 0 || teststart > numtests) {
1020 fprintf(stdout, "Invalid starting test: %s\n",
1026 testend = strtol(optarg, NULL, 10);
1027 if (testend <= 0 || testend > numtests) {
1028 fprintf(stdout, "Invalid final test: %s\n",
1038 /* should be exactly one arg left, the filename */
1039 if (optind != argc - 1)
1042 if (teststart > testend) {
1043 fprintf(stdout, "Starting test is larger than final test!\n");
1047 base_file_path = (char *)strdup(argv[optind]);
1049 ret = test_basic_support();
1050 if (ret || check_support)
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]);
1063 free(base_file_path);