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
26 static blksize_t alloc_size;
27 int allow_default_behavior = 1;
28 int default_behavior = 0;
29 int unwritten_extents = 0;
33 static void get_file_system(int fd)
37 if (!fstatfs(fd, &buf)) {
38 fprintf(stdout, "File system magic#: 0x%lx\n",
39 (unsigned long int)buf.f_type);
43 /* Compute the file allocation unit size for an XFS file. */
44 static int detect_xfs_alloc_unit(int fd)
47 struct xfs_fsop_geom fsgeom;
50 ret = ioctl(fd, XFS_IOC_FSGEOMETRY, &fsgeom);
54 ret = ioctl(fd, XFS_IOC_FSGETXATTR, &fsx);
58 alloc_size = fsgeom.blocksize;
59 if (fsx.fsx_xflags & XFS_XFLAG_REALTIME)
60 alloc_size *= fsgeom.rtextsize;
65 static int get_io_sizes(int fd)
67 off_t pos = 0, offset = 1;
70 int pagesz = sysconf(_SC_PAGE_SIZE);
72 ret = detect_xfs_alloc_unit(fd);
76 ret = fstat(fd, &buf);
78 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
83 /* st_blksize is typically also the allocation size */
84 alloc_size = buf.st_blksize;
86 /* try to discover the actual alloc size */
87 while (pos == 0 && offset < alloc_size) {
90 pwrite(fd, "a", 1, offset);
91 pos = lseek(fd, 0, SEEK_DATA);
98 while (shift && offset < alloc_size) {
100 pwrite(fd, "a", 1, offset);
101 pos = lseek(fd, 0, SEEK_DATA);
104 offset += pos ? -shift : shift;
108 offset += pos ? 0 : 1;
111 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
115 fprintf(stderr, "Kernel does not support llseek(2) extension "
116 "SEEK_DATA. Aborting.\n");
120 #define do_free(x) do { if(x) free(x); } while(0);
122 static void *do_malloc(size_t size)
128 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
134 static int do_truncate(int fd, off_t length)
138 ret = ftruncate(fd, length);
140 fprintf(stderr, " ERROR %d: Failed to extend file "
141 "to %ld bytes\n", errno, (long)length);
145 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
149 ret = fallocate(fd, mode, offset, length);
151 fprintf(stderr, " ERROR %d: Failed to %s of %ld bytes\n",
152 errno, (mode & FALLOC_FL_PUNCH_HOLE) ? "punch hole" :
153 "preallocate space", (long) length);
159 * Synchnorize all dirty pages in the file range starting from
160 * offset to nbytes length.
162 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
166 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
168 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
174 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
176 ssize_t ret, written = 0;
178 while (count > written) {
179 ret = pwrite(fd, buf + written, count - written, offset + written);
181 /* Don't warn about too large file. It's fs dependent. */
184 fprintf(stderr, " ERROR %d: Failed to write %ld "
185 "bytes\n", errno, (long)count);
194 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
196 static int do_create(const char *filename)
200 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
202 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
208 static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
209 off_t set, off_t exp)
214 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
217 * The file pointer can be set to different values depending
218 * on the implementation. For SEEK_HOLE, EOF could be a valid
219 * value. For SEEK_DATA, supplied offset could be the valid
223 if (origin == SEEK_HOLE && exp2 != -1)
225 if (origin == SEEK_DATA && default_behavior && set < filsz)
228 pos = lseek(fd, set, origin);
230 if (pos == -1 && exp == -1) {
231 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
233 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
235 ret = !(errno == ENXIO);
238 x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
240 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
241 (long long)exp, (long long)exp2, (long long)pos);
242 ret = !(pos == exp || pos == exp2);
245 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
250 static int huge_file_test(int fd, int testnum, off_t filsz)
253 int bufsz = alloc_size * 16; /* XFS seems to round allocated size */
254 off_t off = filsz - bufsz;
257 buf = do_malloc(bufsz);
260 memset(buf, 'a', bufsz);
262 /* |- DATA -|- HUGE HOLE -|- DATA -| */
263 ret = do_pwrite(fd, buf, bufsz, 0);
266 ret = do_pwrite(fd, buf, bufsz, off);
269 * Report success. Filesystem just cannot handle so large
270 * offsets and correctly reports it.
272 if (errno == EFBIG) {
273 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
279 /* offset at the beginning */
280 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
281 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
282 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
283 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
285 /* offset around eof */
286 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off, off + bufsz);
287 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off, off);
288 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, off + 1, off + 1);
289 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off - bufsz, off);
297 * Make sure hole size is properly reported when punched in the middle of a file
299 static int test21(int fd, int testnum)
306 fprintf(stdout, "Test skipped as fs doesn't support punch hole.\n");
310 if (default_behavior) {
311 fprintf(stdout, "Test skipped as fs doesn't support seeking a punched hole.\n");
315 bufsz = alloc_size * 3;
316 buf = do_malloc(bufsz);
321 memset(buf, 'a', bufsz);
323 ret = do_pwrite(fd, buf, bufsz, 0);
328 ret = do_fallocate(fd, alloc_size, alloc_size,
329 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
333 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
334 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, alloc_size);
335 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, alloc_size, alloc_size * 2);
343 * Make sure hole size is properly reported when starting in the middle of a
344 * hole in ext? doubly indirect tree
346 static int test20(int fd, int testnum)
353 buf = do_malloc(bufsz);
356 memset(buf, 'a', bufsz);
358 /* Magic size in the middle of ext[23] triple indirect tree */
359 filsz = (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + 2 * bufsz / 4 + 5) * bufsz;
360 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
363 * Report success. Filesystem just cannot handle so large
364 * offsets and correctly reports it.
366 if (errno == EFBIG) {
367 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
373 /* Offset inside ext[23] indirect block */
374 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 14 * bufsz, filsz - bufsz);
375 /* Offset inside ext[23] doubly indirect block */
376 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, (12 + 2 * bufsz / 4) * bufsz, filsz - bufsz);
377 /* Offsets inside ext[23] triply indirect block */
378 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
379 (12 + bufsz / 4 + bufsz / 4 * bufsz / 4 + 3 * bufsz / 4 + 5) * bufsz, filsz - bufsz);
380 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
381 (12 + bufsz / 4 + 7 * bufsz / 4 * bufsz / 4 + 5 * bufsz / 4) * bufsz, filsz - bufsz);
382 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
383 (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + bufsz / 4 + 11) * bufsz, filsz - bufsz);
390 * Make sure hole size is properly reported when starting in the middle of a
391 * hole in ext? indirect tree
393 static int test19(int fd, int testnum)
400 buf = do_malloc(bufsz);
403 memset(buf, 'a', bufsz);
405 /* Magic size just beyond ext[23] indirect tree size */
406 filsz = (12 + bufsz / 4 + 1) * bufsz;
407 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
411 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
412 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 12*bufsz, filsz - bufsz);
413 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, (12 + bufsz / 4 - 8)*bufsz, filsz - bufsz);
420 /* Make sure we get ENXIO if we pass in a negative offset. */
421 static int test18(int fd, int testnum)
425 /* file size doesn't matter in this test, set to 0 */
428 ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
429 ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
430 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, LLONG_MIN, -1);
431 ret += do_lseek(testnum, 4, fd, 0, SEEK_DATA, LLONG_MIN, -1);
436 static int test17(int fd, int testnum)
439 int pagesz = sysconf(_SC_PAGE_SIZE);
443 if (!unwritten_extents) {
444 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
448 if (pagesz < 4 * alloc_size) {
449 fprintf(stdout, "Test skipped as page size (%d) is less than "
450 "four times allocation size (%d).\n",
451 pagesz, (int)alloc_size);
457 buf = do_malloc(bufsz);
462 memset(buf, 'a', bufsz);
464 ret = do_fallocate(fd, 0, filsz, 0);
468 ret = do_pwrite(fd, buf, bufsz, 0);
472 ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
476 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
477 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, bufsz);
478 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
479 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, 1, bufsz);
480 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
481 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
482 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
483 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
484 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
485 ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
486 ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
487 ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
490 ret += do_fallocate(fd, 0, filsz, 0);
492 ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
493 ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
494 ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
495 ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
503 * test file with unwritten extents, having non-contiguous dirty pages in
504 * the unwritten extent.
506 static int test16(int fd, int testnum)
510 int bufsz = sysconf(_SC_PAGE_SIZE);
513 if (!unwritten_extents) {
514 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
518 /* HOLE - unwritten DATA in dirty page */
519 /* Each unit is bufsz */
520 buf = do_malloc(bufsz);
523 memset(buf, 'a', bufsz);
525 /* preallocate 4M space to file */
526 ret = do_fallocate(fd, 0, filsz, 0);
530 ret = do_pwrite(fd, buf, bufsz, 0);
534 ret = do_pwrite(fd, buf, bufsz, filsz/2);
538 /* offset at the beginning */
539 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
540 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
541 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
542 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
543 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
544 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, filsz/2,
553 * test file with unwritten extents, having page just after end of unwritten
556 static int test15(int fd, int testnum)
560 int bufsz = sysconf(_SC_PAGE_SIZE);
563 if (!unwritten_extents) {
564 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
568 /* HOLE - unwritten DATA in dirty page */
569 /* Each unit is bufsz */
570 buf = do_malloc(bufsz);
573 memset(buf, 'a', bufsz);
575 /* preallocate 4M space to file */
576 ret = do_fallocate(fd, 0, filsz, 0);
580 ret = do_pwrite(fd, buf, bufsz, 0);
584 /* One page written just after end of unwritten extent... */
585 ret = do_pwrite(fd, buf, bufsz, filsz);
589 /* update file size */
592 /* offset at the beginning */
593 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
594 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
595 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
596 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
597 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
605 * test file with unwritten extents, only have pagevec worth of dirty pages
606 * in page cache, a hole and then another page.
608 static int test14(int fd, int testnum)
612 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
615 if (!unwritten_extents) {
616 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
620 /* HOLE - unwritten DATA in dirty page */
621 /* Each unit is bufsz */
622 buf = do_malloc(bufsz);
625 memset(buf, 'a', bufsz);
627 /* preallocate 4M space to file */
628 ret = do_fallocate(fd, 0, filsz, 0);
632 ret = do_pwrite(fd, buf, bufsz, 0);
636 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
640 /* offset at the beginning */
641 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
642 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
643 ret += do_lseek(testnum, 3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
644 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 0, 0);
645 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, 1, 1);
646 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
654 * test file with unwritten extents, only have pagevec worth of dirty pages
657 static int test13(int fd, int testnum)
661 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
664 if (!unwritten_extents) {
665 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
669 /* HOLE - unwritten DATA in dirty page */
670 /* Each unit is bufsz */
671 buf = do_malloc(bufsz);
674 memset(buf, 'a', bufsz);
676 /* preallocate 4M space to file */
677 ret = do_fallocate(fd, 0, filsz, 0);
681 ret = do_pwrite(fd, buf, bufsz, 0);
685 /* offset at the beginning */
686 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
687 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
688 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
689 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
697 * Test huge file to check for overflows of block counts due to usage of
700 static int test12(int fd, int testnum)
702 return huge_file_test(fd, testnum,
703 ((long long)alloc_size << 32) + (1 << 20));
707 * Test huge file to check for overflows of block counts due to usage of
710 static int test11(int fd, int testnum)
712 return huge_file_test(fd, testnum,
713 ((long long)alloc_size << 31) + (1 << 20));
716 /* Test an 8G file to check for offset overflows at 1 << 32 */
717 static int test10(int fd, int testnum)
719 return huge_file_test(fd, testnum, 8ULL << 30);
723 * test file with unwritten extents, have both dirty and
724 * writeback pages in page cache.
726 static int test09(int fd, int testnum)
730 int bufsz = alloc_size;
731 int filsz = bufsz * 100 + bufsz;
733 if (!unwritten_extents) {
734 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
739 * HOLE - unwritten DATA in dirty page - HOLE -
740 * unwritten DATA in writeback page
743 /* Each unit is bufsz */
744 buf = do_malloc(bufsz);
747 memset(buf, 'a', bufsz);
749 /* preallocate 8M space to file */
750 ret = do_fallocate(fd, 0, filsz, 0);
754 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
756 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
762 * Sync out dirty pages from bufsz * 100, this will convert
763 * the dirty page to writeback.
765 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
769 /* offset at the beginning */
770 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
771 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
772 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
773 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
780 /* test file with unwritten extent, only have writeback page */
781 static int test08(int fd, int testnum)
785 int bufsz = alloc_size;
786 int filsz = bufsz * 10 + bufsz;
788 if (!unwritten_extents) {
789 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
793 /* HOLE - unwritten DATA in writeback page */
794 /* Each unit is bufsz */
795 buf = do_malloc(bufsz);
798 memset(buf, 'a', bufsz);
800 /* preallocate 4M space to file */
801 ret = do_fallocate(fd, 0, filsz, 0);
805 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
809 /* Sync out all file */
810 ret = do_sync_dirty_pages(fd, 0, 0);
814 /* offset at the beginning */
815 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
816 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
817 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
818 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
826 * test file with unwritten extents, only have dirty pages
829 static int test07(int fd, int testnum)
833 int bufsz = alloc_size;
834 int filsz = bufsz * 10 + bufsz;
836 if (!unwritten_extents) {
837 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
841 /* HOLE - unwritten DATA in dirty page */
842 /* Each unit is bufsz */
843 buf = do_malloc(bufsz);
846 memset(buf, 'a', bufsz);
848 /* preallocate 4M space to file */
849 ret = do_fallocate(fd, 0, filsz, 0);
853 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
857 /* offset at the beginning */
858 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
859 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
860 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
861 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
868 /* test hole data hole data */
869 static int test06(int fd, int testnum)
873 int bufsz = alloc_size;
874 int filsz = bufsz * 4;
877 /* HOLE - DATA - HOLE - DATA */
878 /* Each unit is bufsz */
880 buf = do_malloc(bufsz);
884 memset(buf, 'a', bufsz);
886 ret = do_pwrite(fd, buf, bufsz, bufsz);
888 do_pwrite(fd, buf, bufsz, bufsz * 3);
892 /* offset at the beginning */
893 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
894 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
895 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
896 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
898 /* offset around first hole-data boundary */
900 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
901 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
902 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
903 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
904 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
905 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
907 /* offset around data-hole boundary */
909 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
910 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
911 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
912 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
913 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
914 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
916 /* offset around second hole-data boundary */
918 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
919 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
920 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
921 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
922 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
923 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
925 /* offset around the end of file */
927 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
928 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
929 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
930 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
931 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
932 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
939 /* test file with data at the beginning and a hole at the end */
940 static int test05(int fd, int testnum)
944 int bufsz = alloc_size;
945 int filsz = bufsz * 4;
947 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
949 buf = do_malloc(bufsz);
952 memset(buf, 'a', bufsz);
954 ret = do_truncate(fd, filsz);
956 ret = do_pwrite(fd, buf, bufsz, 0);
960 /* offset at the beginning */
962 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
963 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
965 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
966 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
968 /* offset around data-hole boundary */
969 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
970 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
972 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
973 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
974 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
975 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
977 /* offset around eof */
978 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
979 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
980 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
981 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
982 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
983 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
988 /* test hole begin and data end */
989 static int test04(int fd, int testnum)
992 char *buf = "ABCDEFGH";
993 int bufsz, holsz, filsz;
996 holsz = alloc_size * 2;
997 filsz = holsz + bufsz;
999 /* |- HOLE -|- HOLE -|- DATA -| */
1001 ret = do_pwrite(fd, buf, bufsz, holsz);
1005 /* offset at the beginning */
1006 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
1007 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
1008 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
1009 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
1010 /* offset around hole-data boundary */
1011 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
1012 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
1013 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
1014 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
1015 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
1016 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
1018 /* offset around eof */
1019 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
1020 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
1021 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
1022 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
1023 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
1024 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
1029 /* test a larger full file */
1030 static int test03(int fd, int testnum)
1033 int bufsz = alloc_size * 2 + 100;
1037 buf = do_malloc(bufsz);
1040 memset(buf, 'a', bufsz);
1042 ret = do_pwrite(fd, buf, bufsz, 0);
1046 /* offset at the beginning */
1047 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
1048 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
1049 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
1050 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
1052 /* offset around eof */
1053 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
1054 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1055 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
1056 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
1057 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1058 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1065 /* test tiny full file */
1066 static int test02(int fd, int testnum)
1069 char buf[] = "ABCDEFGH";
1072 bufsz = strlen(buf);
1077 ret = do_pwrite(fd, buf, bufsz, 0);
1081 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
1082 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
1083 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
1084 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
1085 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1086 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
1087 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
1088 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1089 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1095 /* test empty file */
1096 static int test01(int fd, int testnum)
1100 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
1101 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
1102 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
1109 int (*test_func)(int fd, int testnum);
1113 struct testrec seek_tests[] = {
1114 { 1, test01, "Test empty file" },
1115 { 2, test02, "Test a tiny full file" },
1116 { 3, test03, "Test a larger full file" },
1117 { 4, test04, "Test file hole at beg, data at end" },
1118 { 5, test05, "Test file data at beg, hole at end" },
1119 { 6, test06, "Test file hole data hole data" },
1120 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
1121 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
1122 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
1123 { 10, test10, "Test a huge file for offset overflow" },
1124 { 11, test11, "Test a huge file for block number signed" },
1125 { 12, test12, "Test a huge file for block number overflow" },
1126 { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
1127 { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
1128 { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
1129 { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
1130 { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
1131 { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
1132 { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
1133 { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
1134 { 21, test21, "Test file SEEK_HOLE that was created by PUNCH_HOLE" },
1137 static int run_test(struct testrec *tr)
1139 int ret = 0, fd = -1;
1142 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
1144 fd = do_create(filename);
1146 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
1147 ret = tr->test_func(fd, tr->test_num);
1155 static int test_basic_support(void)
1162 fd = do_create(base_file_path);
1166 get_file_system(fd);
1168 ret = get_io_sizes(fd);
1173 bufsz = alloc_size * 2;
1176 buf = do_malloc(bufsz);
1179 memset(buf, 'a', bufsz);
1181 /* File with 2 allocated blocks.... */
1182 ret = do_pwrite(fd, buf, bufsz, 0);
1186 /* followed by a hole... */
1187 ret = do_truncate(fd, filsz);
1191 /* Is SEEK_DATA supported in the kernel? */
1192 pos = lseek(fd, 0, SEEK_DATA);
1194 fprintf(stderr, "Kernel does not support llseek(2) extension "
1195 "SEEK_DATA. Aborting.\n");
1200 /* Is SEEK_HOLE supported in the kernel? */
1201 pos = lseek(fd, 0, SEEK_HOLE);
1203 fprintf(stderr, "Kernel does not support llseek(2) extension "
1204 "SEEK_HOLE. Aborting.\n");
1210 default_behavior = 1;
1211 fprintf(stderr, "File system supports the default behavior.\n");
1214 if (default_behavior && !allow_default_behavior) {
1215 fprintf(stderr, "Default behavior is not allowed. Aborting.\n");
1220 /* Is unwritten extent supported? */
1222 if (fallocate(fd, 0, 0, alloc_size * 2) == -1) {
1223 if (errno == EOPNOTSUPP) {
1224 fprintf(stderr, "File system does not support fallocate.\n");
1226 fprintf(stderr, "ERROR %d: Failed to preallocate "
1227 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1233 pos = lseek(fd, 0, SEEK_DATA);
1235 fprintf(stderr, "File system does not support unwritten extents.\n");
1237 unwritten_extents = 1;
1239 /* Is punch hole supported? */
1240 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1241 0, alloc_size) == -1)
1242 fprintf(stderr, "File system does not support punch hole.\n");
1254 void usage(char *cmd)
1256 fprintf(stdout, "Usage: %s [-tf] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1260 int main(int argc, char **argv)
1265 int check_support = 0;
1266 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1267 int teststart, testend;
1270 * First twelve tests are used by generic/285. To run these is the
1271 * default. Further tests are run e.g. by generic/436. They are not
1272 * run by default in order to not regress older kernels.
1277 while ((opt = getopt(argc, argv, "tfs:e:")) != -1) {
1283 allow_default_behavior = 0;
1286 teststart = strtol(optarg, NULL, 10);
1287 if (teststart <= 0 || teststart > numtests) {
1288 fprintf(stdout, "Invalid starting test: %s\n",
1294 testend = strtol(optarg, NULL, 10);
1295 if (testend <= 0 || testend > numtests) {
1296 fprintf(stdout, "Invalid final test: %s\n",
1306 /* should be exactly one arg left, the filename */
1307 if (optind != argc - 1)
1310 if (teststart > testend) {
1311 fprintf(stdout, "Starting test is larger than final test!\n");
1315 base_file_path = (char *)strdup(argv[optind]);
1317 ret = test_basic_support();
1318 if (ret || check_support)
1321 for (i = 0; i < numtests; ++i) {
1322 if (seek_tests[i].test_num >= teststart &&
1323 seek_tests[i].test_num <= testend) {
1324 ret = run_test(&seek_tests[i]);
1331 free(base_file_path);