1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Oracle. All rights reserved.
4 * Copyright (C) 2011 Red Hat. All rights reserved.
7 #define _XOPEN_SOURCE 500
8 #define _FILE_OFFSET_BITS 64
25 static blksize_t alloc_size;
26 int default_behavior = 0;
27 int unwritten_extents = 0;
30 static void get_file_system(int fd)
34 if (!fstatfs(fd, &buf)) {
35 fprintf(stdout, "File system magic#: 0x%lx\n",
36 (unsigned long int)buf.f_type);
40 static int get_io_sizes(int fd)
42 off_t pos = 0, offset = 1;
46 ret = fstat(fd, &buf);
48 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
53 /* st_blksize is typically also the allocation size */
54 alloc_size = buf.st_blksize;
56 /* try to discover the actual alloc size */
57 while (pos == 0 && offset < alloc_size) {
60 pwrite(fd, "a", 1, offset);
61 pos = lseek(fd, 0, SEEK_DATA);
68 while (shift && offset < alloc_size) {
70 pwrite(fd, "a", 1, offset);
71 pos = lseek(fd, 0, SEEK_DATA);
74 offset += pos ? -shift : shift;
78 offset += pos ? 0 : 1;
80 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
84 fprintf(stderr, "Kernel does not support llseek(2) extension "
85 "SEEK_DATA. Aborting.\n");
89 #define do_free(x) do { if(x) free(x); } while(0);
91 static void *do_malloc(size_t size)
97 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
103 static int do_truncate(int fd, off_t length)
107 ret = ftruncate(fd, length);
109 fprintf(stderr, " ERROR %d: Failed to extend file "
110 "to %ld bytes\n", errno, (long)length);
114 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
118 ret = fallocate(fd, mode, offset, length);
120 fprintf(stderr, " ERROR %d: Failed to preallocate "
121 "space to %ld bytes\n", errno, (long) length);
127 * Synchnorize all dirty pages in the file range starting from
128 * offset to nbytes length.
130 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
134 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
136 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
142 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
144 ssize_t ret, written = 0;
146 while (count > written) {
147 ret = pwrite(fd, buf + written, count - written, offset + written);
149 /* Don't warn about too large file. It's fs dependent. */
152 fprintf(stderr, " ERROR %d: Failed to write %ld "
153 "bytes\n", errno, (long)count);
162 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
164 static int do_create(const char *filename)
168 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
170 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
176 static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
177 off_t set, off_t exp)
182 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
185 * The file pointer can be set to different values depending
186 * on the implementation. For SEEK_HOLE, EOF could be a valid
187 * value. For SEEK_DATA, supplied offset could be the valid
191 if (origin == SEEK_HOLE && exp2 != -1)
193 if (origin == SEEK_DATA && default_behavior && set < filsz)
196 pos = lseek(fd, set, origin);
198 if (pos == -1 && exp == -1) {
199 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
201 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
203 ret = !(errno == ENXIO);
206 x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
208 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
209 (long long)exp, (long long)exp2, (long long)pos);
210 ret = !(pos == exp || pos == exp2);
213 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
218 static int huge_file_test(int fd, int testnum, off_t filsz)
221 int bufsz = alloc_size * 16; /* XFS seems to round allocated size */
222 off_t off = filsz - bufsz;
225 buf = do_malloc(bufsz);
228 memset(buf, 'a', bufsz);
230 /* |- DATA -|- HUGE HOLE -|- DATA -| */
231 ret = do_pwrite(fd, buf, bufsz, 0);
234 ret = do_pwrite(fd, buf, bufsz, off);
237 * Report success. Filesystem just cannot handle so large
238 * offsets and correctly reports it.
240 if (errno == EFBIG) {
241 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
247 /* offset at the beginning */
248 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
249 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
250 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
251 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
253 /* offset around eof */
254 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off, off + bufsz);
255 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off, off);
256 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, off + 1, off + 1);
257 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off - bufsz, off);
265 * Make sure hole size is properly reported when starting in the middle of a
266 * hole in ext? doubly indirect tree
268 static int test20(int fd, int testnum)
275 buf = do_malloc(bufsz);
278 memset(buf, 'a', bufsz);
280 /* Magic size in the middle of ext[23] triple indirect tree */
281 filsz = (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + 2 * bufsz / 4 + 5) * bufsz;
282 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
285 * Report success. Filesystem just cannot handle so large
286 * offsets and correctly reports it.
288 if (errno == EFBIG) {
289 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
295 /* Offset inside ext[23] indirect block */
296 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 14 * bufsz, filsz - bufsz);
297 /* Offset inside ext[23] doubly indirect block */
298 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, (12 + 2 * bufsz / 4) * bufsz, filsz - bufsz);
299 /* Offsets inside ext[23] triply indirect block */
300 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
301 (12 + bufsz / 4 + bufsz / 4 * bufsz / 4 + 3 * bufsz / 4 + 5) * bufsz, filsz - bufsz);
302 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
303 (12 + bufsz / 4 + 7 * bufsz / 4 * bufsz / 4 + 5 * bufsz / 4) * bufsz, filsz - bufsz);
304 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
305 (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + bufsz / 4 + 11) * bufsz, filsz - bufsz);
312 * Make sure hole size is properly reported when starting in the middle of a
313 * hole in ext? indirect tree
315 static int test19(int fd, int testnum)
322 buf = do_malloc(bufsz);
325 memset(buf, 'a', bufsz);
327 /* Magic size just beyond ext[23] indirect tree size */
328 filsz = (12 + bufsz / 4 + 1) * bufsz;
329 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
333 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
334 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 12*bufsz, filsz - bufsz);
335 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, (12 + bufsz / 4 - 8)*bufsz, filsz - bufsz);
342 /* Make sure we get ENXIO if we pass in a negative offset. */
343 static int test18(int fd, int testnum)
347 /* file size doesn't matter in this test, set to 0 */
348 ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
349 ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
354 static int test17(int fd, int testnum)
357 int pagesz = sysconf(_SC_PAGE_SIZE);
361 if (!unwritten_extents) {
362 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
366 if (pagesz < 4 * alloc_size) {
367 fprintf(stdout, "Test skipped as page size (%d) is less than "
368 "four times allocation size (%d).\n",
369 pagesz, (int)alloc_size);
375 buf = do_malloc(bufsz);
380 memset(buf, 'a', bufsz);
382 ret = do_fallocate(fd, 0, filsz, 0);
386 ret = do_pwrite(fd, buf, bufsz, 0);
390 ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
394 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
395 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, bufsz);
396 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
397 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, 1, bufsz);
398 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
399 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
400 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
401 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
402 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
403 ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
404 ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
405 ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
408 ret += do_fallocate(fd, 0, filsz, 0);
410 ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
411 ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
412 ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
413 ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
421 * test file with unwritten extents, having non-contiguous dirty pages in
422 * the unwritten extent.
424 static int test16(int fd, int testnum)
428 int bufsz = sysconf(_SC_PAGE_SIZE);
431 if (!unwritten_extents) {
432 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
436 /* HOLE - unwritten DATA in dirty page */
437 /* Each unit is bufsz */
438 buf = do_malloc(bufsz);
441 memset(buf, 'a', bufsz);
443 /* preallocate 4M space to file */
444 ret = do_fallocate(fd, 0, filsz, 0);
448 ret = do_pwrite(fd, buf, bufsz, 0);
452 ret = do_pwrite(fd, buf, bufsz, filsz/2);
456 /* offset at the beginning */
457 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
458 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
459 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
460 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
461 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
462 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, filsz/2,
471 * test file with unwritten extents, having page just after end of unwritten
474 static int test15(int fd, int testnum)
478 int bufsz = sysconf(_SC_PAGE_SIZE);
481 if (!unwritten_extents) {
482 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
486 /* HOLE - unwritten DATA in dirty page */
487 /* Each unit is bufsz */
488 buf = do_malloc(bufsz);
491 memset(buf, 'a', bufsz);
493 /* preallocate 4M space to file */
494 ret = do_fallocate(fd, 0, filsz, 0);
498 ret = do_pwrite(fd, buf, bufsz, 0);
502 /* One page written just after end of unwritten extent... */
503 ret = do_pwrite(fd, buf, bufsz, filsz);
507 /* update file size */
510 /* offset at the beginning */
511 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
512 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
513 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
514 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
515 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
523 * test file with unwritten extents, only have pagevec worth of dirty pages
524 * in page cache, a hole and then another page.
526 static int test14(int fd, int testnum)
530 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
533 if (!unwritten_extents) {
534 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
538 /* HOLE - unwritten DATA in dirty page */
539 /* Each unit is bufsz */
540 buf = do_malloc(bufsz);
543 memset(buf, 'a', bufsz);
545 /* preallocate 4M space to file */
546 ret = do_fallocate(fd, 0, filsz, 0);
550 ret = do_pwrite(fd, buf, bufsz, 0);
554 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
558 /* offset at the beginning */
559 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
560 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
561 ret += do_lseek(testnum, 3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
562 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 0, 0);
563 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, 1, 1);
564 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
572 * test file with unwritten extents, only have pagevec worth of dirty pages
575 static int test13(int fd, int testnum)
579 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
582 if (!unwritten_extents) {
583 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
587 /* HOLE - unwritten DATA in dirty page */
588 /* Each unit is bufsz */
589 buf = do_malloc(bufsz);
592 memset(buf, 'a', bufsz);
594 /* preallocate 4M space to file */
595 ret = do_fallocate(fd, 0, filsz, 0);
599 ret = do_pwrite(fd, buf, bufsz, 0);
603 /* offset at the beginning */
604 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
605 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
606 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
607 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
615 * Test huge file to check for overflows of block counts due to usage of
618 static int test12(int fd, int testnum)
620 return huge_file_test(fd, testnum,
621 ((long long)alloc_size << 32) + (1 << 20));
625 * Test huge file to check for overflows of block counts due to usage of
628 static int test11(int fd, int testnum)
630 return huge_file_test(fd, testnum,
631 ((long long)alloc_size << 31) + (1 << 20));
634 /* Test an 8G file to check for offset overflows at 1 << 32 */
635 static int test10(int fd, int testnum)
637 return huge_file_test(fd, testnum, 8ULL << 30);
641 * test file with unwritten extents, have both dirty and
642 * writeback pages in page cache.
644 static int test09(int fd, int testnum)
648 int bufsz = alloc_size;
649 int filsz = bufsz * 100 + bufsz;
651 if (!unwritten_extents) {
652 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
657 * HOLE - unwritten DATA in dirty page - HOLE -
658 * unwritten DATA in writeback page
661 /* Each unit is bufsz */
662 buf = do_malloc(bufsz);
665 memset(buf, 'a', bufsz);
667 /* preallocate 8M space to file */
668 ret = do_fallocate(fd, 0, filsz, 0);
672 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
674 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
680 * Sync out dirty pages from bufsz * 100, this will convert
681 * the dirty page to writeback.
683 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
687 /* offset at the beginning */
688 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
689 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
690 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
691 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
698 /* test file with unwritten extent, only have writeback page */
699 static int test08(int fd, int testnum)
703 int bufsz = alloc_size;
704 int filsz = bufsz * 10 + bufsz;
706 if (!unwritten_extents) {
707 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
711 /* HOLE - unwritten DATA in writeback page */
712 /* Each unit is bufsz */
713 buf = do_malloc(bufsz);
716 memset(buf, 'a', bufsz);
718 /* preallocate 4M space to file */
719 ret = do_fallocate(fd, 0, filsz, 0);
723 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
727 /* Sync out all file */
728 ret = do_sync_dirty_pages(fd, 0, 0);
732 /* offset at the beginning */
733 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
734 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
735 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
736 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
744 * test file with unwritten extents, only have dirty pages
747 static int test07(int fd, int testnum)
751 int bufsz = alloc_size;
752 int filsz = bufsz * 10 + bufsz;
754 if (!unwritten_extents) {
755 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
759 /* HOLE - unwritten DATA in dirty page */
760 /* Each unit is bufsz */
761 buf = do_malloc(bufsz);
764 memset(buf, 'a', bufsz);
766 /* preallocate 4M space to file */
767 ret = do_fallocate(fd, 0, filsz, 0);
771 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
775 /* offset at the beginning */
776 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
777 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
778 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
779 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
786 /* test hole data hole data */
787 static int test06(int fd, int testnum)
791 int bufsz = alloc_size;
792 int filsz = bufsz * 4;
795 /* HOLE - DATA - HOLE - DATA */
796 /* Each unit is bufsz */
798 buf = do_malloc(bufsz);
802 memset(buf, 'a', bufsz);
804 ret = do_pwrite(fd, buf, bufsz, bufsz);
806 do_pwrite(fd, buf, bufsz, bufsz * 3);
810 /* offset at the beginning */
811 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
812 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
813 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
814 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
816 /* offset around first hole-data boundary */
818 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
819 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
820 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
821 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
822 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
823 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
825 /* offset around data-hole boundary */
827 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
828 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
829 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
830 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
831 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
832 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
834 /* offset around second hole-data boundary */
836 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
837 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
838 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
839 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
840 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
841 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
843 /* offset around the end of file */
845 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
846 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
847 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
848 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
849 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
850 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
857 /* test file with data at the beginning and a hole at the end */
858 static int test05(int fd, int testnum)
862 int bufsz = alloc_size;
863 int filsz = bufsz * 4;
865 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
867 buf = do_malloc(bufsz);
870 memset(buf, 'a', bufsz);
872 ret = do_truncate(fd, filsz);
874 ret = do_pwrite(fd, buf, bufsz, 0);
878 /* offset at the beginning */
880 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
881 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
883 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
884 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
886 /* offset around data-hole boundary */
887 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
888 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
890 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
891 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
892 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
893 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
895 /* offset around eof */
896 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
897 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
898 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
899 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
900 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
901 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
906 /* test hole begin and data end */
907 static int test04(int fd, int testnum)
910 char *buf = "ABCDEFGH";
911 int bufsz, holsz, filsz;
914 holsz = alloc_size * 2;
915 filsz = holsz + bufsz;
917 /* |- HOLE -|- HOLE -|- DATA -| */
919 ret = do_pwrite(fd, buf, bufsz, holsz);
923 /* offset at the beginning */
924 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
925 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
926 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
927 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
928 /* offset around hole-data boundary */
929 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
930 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
931 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
932 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
933 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
934 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
936 /* offset around eof */
937 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
938 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
939 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
940 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
941 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
942 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
947 /* test a larger full file */
948 static int test03(int fd, int testnum)
951 int bufsz = alloc_size * 2 + 100;
955 buf = do_malloc(bufsz);
958 memset(buf, 'a', bufsz);
960 ret = do_pwrite(fd, buf, bufsz, 0);
964 /* offset at the beginning */
965 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
966 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
967 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
968 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
970 /* offset around eof */
971 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
972 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
973 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
974 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
975 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
976 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
983 /* test tiny full file */
984 static int test02(int fd, int testnum)
987 char buf[] = "ABCDEFGH";
995 ret = do_pwrite(fd, buf, bufsz, 0);
999 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
1000 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
1001 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
1002 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
1003 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1004 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
1005 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
1006 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1007 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1013 /* test empty file */
1014 static int test01(int fd, int testnum)
1018 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
1019 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
1020 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
1027 int (*test_func)(int fd, int testnum);
1031 struct testrec seek_tests[] = {
1032 { 1, test01, "Test empty file" },
1033 { 2, test02, "Test a tiny full file" },
1034 { 3, test03, "Test a larger full file" },
1035 { 4, test04, "Test file hole at beg, data at end" },
1036 { 5, test05, "Test file data at beg, hole at end" },
1037 { 6, test06, "Test file hole data hole data" },
1038 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
1039 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
1040 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
1041 { 10, test10, "Test a huge file for offset overflow" },
1042 { 11, test11, "Test a huge file for block number signed" },
1043 { 12, test12, "Test a huge file for block number overflow" },
1044 { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
1045 { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
1046 { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
1047 { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
1048 { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
1049 { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
1050 { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
1051 { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
1054 static int run_test(struct testrec *tr)
1056 int ret = 0, fd = -1;
1059 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
1061 fd = do_create(filename);
1063 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
1064 ret = tr->test_func(fd, tr->test_num);
1072 static int test_basic_support(void)
1079 fd = do_create(base_file_path);
1083 get_file_system(fd);
1085 ret = get_io_sizes(fd);
1090 bufsz = alloc_size * 2;
1093 buf = do_malloc(bufsz);
1096 memset(buf, 'a', bufsz);
1098 /* File with 2 allocated blocks.... */
1099 ret = do_pwrite(fd, buf, bufsz, 0);
1103 /* followed by a hole... */
1104 ret = do_truncate(fd, filsz);
1108 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
1109 pos = lseek(fd, 0, SEEK_HOLE);
1111 fprintf(stderr, "Kernel does not support llseek(2) extension "
1112 "SEEK_HOLE. Aborting.\n");
1118 default_behavior = 1;
1119 fprintf(stderr, "File system supports the default behavior.\n");
1123 if (fallocate(fd, 0, 0, alloc_size) == -1) {
1124 if (errno == EOPNOTSUPP)
1125 fprintf(stderr, "File system does not support fallocate.");
1127 fprintf(stderr, "ERROR %d: Failed to preallocate "
1128 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1134 pos = lseek(fd, 0, SEEK_DATA);
1136 fprintf(stderr, "File system does not support unwritten extents.\n");
1139 unwritten_extents = 1;
1149 void usage(char *cmd)
1151 fprintf(stdout, "Usage: %s [-t] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1155 int main(int argc, char **argv)
1160 int check_support = 0;
1161 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1162 int teststart, testend;
1165 * First twelve tests are used by generic/285. To run these is the
1166 * default. Further tests are run e.g. by generic/436. They are not
1167 * run by default in order to not regress older kernels.
1172 while ((opt = getopt(argc, argv, "ts:e:")) != -1) {
1178 teststart = strtol(optarg, NULL, 10);
1179 if (teststart <= 0 || teststart > numtests) {
1180 fprintf(stdout, "Invalid starting test: %s\n",
1186 testend = strtol(optarg, NULL, 10);
1187 if (testend <= 0 || testend > numtests) {
1188 fprintf(stdout, "Invalid final test: %s\n",
1198 /* should be exactly one arg left, the filename */
1199 if (optind != argc - 1)
1202 if (teststart > testend) {
1203 fprintf(stdout, "Starting test is larger than final test!\n");
1207 base_file_path = (char *)strdup(argv[optind]);
1209 ret = test_basic_support();
1210 if (ret || check_support)
1213 for (i = 0; i < numtests; ++i) {
1214 if (seek_tests[i].test_num >= teststart &&
1215 seek_tests[i].test_num <= testend) {
1216 ret = run_test(&seek_tests[i]);
1223 free(base_file_path);