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 static int get_io_sizes(int fd)
45 off_t pos = 0, offset = 1;
49 ret = fstat(fd, &buf);
51 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
56 /* st_blksize is typically also the allocation size */
57 alloc_size = buf.st_blksize;
59 /* try to discover the actual alloc size */
60 while (pos == 0 && offset < alloc_size) {
63 pwrite(fd, "a", 1, offset);
64 pos = lseek(fd, 0, SEEK_DATA);
71 while (shift && offset < alloc_size) {
73 pwrite(fd, "a", 1, offset);
74 pos = lseek(fd, 0, SEEK_DATA);
77 offset += pos ? -shift : shift;
81 offset += pos ? 0 : 1;
83 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
87 fprintf(stderr, "Kernel does not support llseek(2) extension "
88 "SEEK_DATA. Aborting.\n");
92 #define do_free(x) do { if(x) free(x); } while(0);
94 static void *do_malloc(size_t size)
100 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
106 static int do_truncate(int fd, off_t length)
110 ret = ftruncate(fd, length);
112 fprintf(stderr, " ERROR %d: Failed to extend file "
113 "to %ld bytes\n", errno, (long)length);
117 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
121 ret = fallocate(fd, mode, offset, length);
123 fprintf(stderr, " ERROR %d: Failed to %s of %ld bytes\n",
124 errno, (mode & FALLOC_FL_PUNCH_HOLE) ? "punch hole" :
125 "preallocate space", (long) length);
131 * Synchnorize all dirty pages in the file range starting from
132 * offset to nbytes length.
134 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
138 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
140 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
146 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
148 ssize_t ret, written = 0;
150 while (count > written) {
151 ret = pwrite(fd, buf + written, count - written, offset + written);
153 /* Don't warn about too large file. It's fs dependent. */
156 fprintf(stderr, " ERROR %d: Failed to write %ld "
157 "bytes\n", errno, (long)count);
166 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
168 static int do_create(const char *filename)
172 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
174 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
180 static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
181 off_t set, off_t exp)
186 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
189 * The file pointer can be set to different values depending
190 * on the implementation. For SEEK_HOLE, EOF could be a valid
191 * value. For SEEK_DATA, supplied offset could be the valid
195 if (origin == SEEK_HOLE && exp2 != -1)
197 if (origin == SEEK_DATA && default_behavior && set < filsz)
200 pos = lseek(fd, set, origin);
202 if (pos == -1 && exp == -1) {
203 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
205 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
207 ret = !(errno == ENXIO);
210 x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
212 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
213 (long long)exp, (long long)exp2, (long long)pos);
214 ret = !(pos == exp || pos == exp2);
217 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
222 static int huge_file_test(int fd, int testnum, off_t filsz)
225 int bufsz = alloc_size * 16; /* XFS seems to round allocated size */
226 off_t off = filsz - bufsz;
229 buf = do_malloc(bufsz);
232 memset(buf, 'a', bufsz);
234 /* |- DATA -|- HUGE HOLE -|- DATA -| */
235 ret = do_pwrite(fd, buf, bufsz, 0);
238 ret = do_pwrite(fd, buf, bufsz, off);
241 * Report success. Filesystem just cannot handle so large
242 * offsets and correctly reports it.
244 if (errno == EFBIG) {
245 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
251 /* offset at the beginning */
252 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
253 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
254 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
255 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
257 /* offset around eof */
258 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off, off + bufsz);
259 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off, off);
260 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, off + 1, off + 1);
261 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off - bufsz, off);
269 * Make sure hole size is properly reported when punched in the middle of a file
271 static int test21(int fd, int testnum)
278 fprintf(stdout, "Test skipped as fs doesn't support punch hole.\n");
282 if (default_behavior) {
283 fprintf(stdout, "Test skipped as fs doesn't support seeking a punched hole.\n");
287 bufsz = alloc_size * 3;
288 buf = do_malloc(bufsz);
293 memset(buf, 'a', bufsz);
295 ret = do_pwrite(fd, buf, bufsz, 0);
300 ret = do_fallocate(fd, alloc_size, alloc_size,
301 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
305 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
306 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, alloc_size);
307 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, alloc_size, alloc_size * 2);
315 * Make sure hole size is properly reported when starting in the middle of a
316 * hole in ext? doubly indirect tree
318 static int test20(int fd, int testnum)
325 buf = do_malloc(bufsz);
328 memset(buf, 'a', bufsz);
330 /* Magic size in the middle of ext[23] triple indirect tree */
331 filsz = (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + 2 * bufsz / 4 + 5) * bufsz;
332 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
335 * Report success. Filesystem just cannot handle so large
336 * offsets and correctly reports it.
338 if (errno == EFBIG) {
339 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
345 /* Offset inside ext[23] indirect block */
346 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 14 * bufsz, filsz - bufsz);
347 /* Offset inside ext[23] doubly indirect block */
348 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, (12 + 2 * bufsz / 4) * bufsz, filsz - bufsz);
349 /* Offsets inside ext[23] triply indirect block */
350 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
351 (12 + bufsz / 4 + bufsz / 4 * bufsz / 4 + 3 * bufsz / 4 + 5) * bufsz, filsz - bufsz);
352 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
353 (12 + bufsz / 4 + 7 * bufsz / 4 * bufsz / 4 + 5 * bufsz / 4) * bufsz, filsz - bufsz);
354 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
355 (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + bufsz / 4 + 11) * bufsz, filsz - bufsz);
362 * Make sure hole size is properly reported when starting in the middle of a
363 * hole in ext? indirect tree
365 static int test19(int fd, int testnum)
372 buf = do_malloc(bufsz);
375 memset(buf, 'a', bufsz);
377 /* Magic size just beyond ext[23] indirect tree size */
378 filsz = (12 + bufsz / 4 + 1) * bufsz;
379 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
383 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
384 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 12*bufsz, filsz - bufsz);
385 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, (12 + bufsz / 4 - 8)*bufsz, filsz - bufsz);
392 /* Make sure we get ENXIO if we pass in a negative offset. */
393 static int test18(int fd, int testnum)
397 /* file size doesn't matter in this test, set to 0 */
398 ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
399 ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
404 static int test17(int fd, int testnum)
407 int pagesz = sysconf(_SC_PAGE_SIZE);
411 if (!unwritten_extents) {
412 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
416 if (pagesz < 4 * alloc_size) {
417 fprintf(stdout, "Test skipped as page size (%d) is less than "
418 "four times allocation size (%d).\n",
419 pagesz, (int)alloc_size);
425 buf = do_malloc(bufsz);
430 memset(buf, 'a', bufsz);
432 ret = do_fallocate(fd, 0, filsz, 0);
436 ret = do_pwrite(fd, buf, bufsz, 0);
440 ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
444 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
445 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, bufsz);
446 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
447 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, 1, bufsz);
448 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
449 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
450 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
451 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
452 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
453 ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
454 ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
455 ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
458 ret += do_fallocate(fd, 0, filsz, 0);
460 ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
461 ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
462 ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
463 ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
471 * test file with unwritten extents, having non-contiguous dirty pages in
472 * the unwritten extent.
474 static int test16(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 ret = do_pwrite(fd, buf, bufsz, filsz/2);
506 /* offset at the beginning */
507 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
508 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
509 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
510 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
511 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
512 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, filsz/2,
521 * test file with unwritten extents, having page just after end of unwritten
524 static int test15(int fd, int testnum)
528 int bufsz = sysconf(_SC_PAGE_SIZE);
531 if (!unwritten_extents) {
532 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
536 /* HOLE - unwritten DATA in dirty page */
537 /* Each unit is bufsz */
538 buf = do_malloc(bufsz);
541 memset(buf, 'a', bufsz);
543 /* preallocate 4M space to file */
544 ret = do_fallocate(fd, 0, filsz, 0);
548 ret = do_pwrite(fd, buf, bufsz, 0);
552 /* One page written just after end of unwritten extent... */
553 ret = do_pwrite(fd, buf, bufsz, filsz);
557 /* update file size */
560 /* offset at the beginning */
561 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
562 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
563 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
564 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
565 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
573 * test file with unwritten extents, only have pagevec worth of dirty pages
574 * in page cache, a hole and then another page.
576 static int test14(int fd, int testnum)
580 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
583 if (!unwritten_extents) {
584 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
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);
600 ret = do_pwrite(fd, buf, bufsz, 0);
604 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
608 /* offset at the beginning */
609 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
610 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
611 ret += do_lseek(testnum, 3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
612 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 0, 0);
613 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, 1, 1);
614 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
622 * test file with unwritten extents, only have pagevec worth of dirty pages
625 static int test13(int fd, int testnum)
629 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
632 if (!unwritten_extents) {
633 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
637 /* HOLE - unwritten DATA in dirty page */
638 /* Each unit is bufsz */
639 buf = do_malloc(bufsz);
642 memset(buf, 'a', bufsz);
644 /* preallocate 4M space to file */
645 ret = do_fallocate(fd, 0, filsz, 0);
649 ret = do_pwrite(fd, buf, bufsz, 0);
653 /* offset at the beginning */
654 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
655 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
656 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
657 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
665 * Test huge file to check for overflows of block counts due to usage of
668 static int test12(int fd, int testnum)
670 return huge_file_test(fd, testnum,
671 ((long long)alloc_size << 32) + (1 << 20));
675 * Test huge file to check for overflows of block counts due to usage of
678 static int test11(int fd, int testnum)
680 return huge_file_test(fd, testnum,
681 ((long long)alloc_size << 31) + (1 << 20));
684 /* Test an 8G file to check for offset overflows at 1 << 32 */
685 static int test10(int fd, int testnum)
687 return huge_file_test(fd, testnum, 8ULL << 30);
691 * test file with unwritten extents, have both dirty and
692 * writeback pages in page cache.
694 static int test09(int fd, int testnum)
698 int bufsz = alloc_size;
699 int filsz = bufsz * 100 + bufsz;
701 if (!unwritten_extents) {
702 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
707 * HOLE - unwritten DATA in dirty page - HOLE -
708 * unwritten DATA in writeback page
711 /* Each unit is bufsz */
712 buf = do_malloc(bufsz);
715 memset(buf, 'a', bufsz);
717 /* preallocate 8M space to file */
718 ret = do_fallocate(fd, 0, filsz, 0);
722 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
724 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
730 * Sync out dirty pages from bufsz * 100, this will convert
731 * the dirty page to writeback.
733 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
737 /* offset at the beginning */
738 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
739 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
740 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
741 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
748 /* test file with unwritten extent, only have writeback page */
749 static int test08(int fd, int testnum)
753 int bufsz = alloc_size;
754 int filsz = bufsz * 10 + bufsz;
756 if (!unwritten_extents) {
757 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
761 /* HOLE - unwritten DATA in writeback page */
762 /* Each unit is bufsz */
763 buf = do_malloc(bufsz);
766 memset(buf, 'a', bufsz);
768 /* preallocate 4M space to file */
769 ret = do_fallocate(fd, 0, filsz, 0);
773 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
777 /* Sync out all file */
778 ret = do_sync_dirty_pages(fd, 0, 0);
782 /* offset at the beginning */
783 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
784 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
785 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
786 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
794 * test file with unwritten extents, only have dirty pages
797 static int test07(int fd, int testnum)
801 int bufsz = alloc_size;
802 int filsz = bufsz * 10 + bufsz;
804 if (!unwritten_extents) {
805 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
809 /* HOLE - unwritten DATA in dirty page */
810 /* Each unit is bufsz */
811 buf = do_malloc(bufsz);
814 memset(buf, 'a', bufsz);
816 /* preallocate 4M space to file */
817 ret = do_fallocate(fd, 0, filsz, 0);
821 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
825 /* offset at the beginning */
826 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
827 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
828 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
829 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
836 /* test hole data hole data */
837 static int test06(int fd, int testnum)
841 int bufsz = alloc_size;
842 int filsz = bufsz * 4;
845 /* HOLE - DATA - HOLE - DATA */
846 /* Each unit is bufsz */
848 buf = do_malloc(bufsz);
852 memset(buf, 'a', bufsz);
854 ret = do_pwrite(fd, buf, bufsz, bufsz);
856 do_pwrite(fd, buf, bufsz, bufsz * 3);
860 /* offset at the beginning */
861 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
862 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
863 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
864 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
866 /* offset around first hole-data boundary */
868 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
869 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
870 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
871 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
872 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
873 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
875 /* offset around data-hole boundary */
877 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
878 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
879 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
880 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
881 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
882 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
884 /* offset around second hole-data boundary */
886 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
887 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
888 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
889 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
890 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
891 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
893 /* offset around the end of file */
895 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
896 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
897 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
898 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
899 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
900 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
907 /* test file with data at the beginning and a hole at the end */
908 static int test05(int fd, int testnum)
912 int bufsz = alloc_size;
913 int filsz = bufsz * 4;
915 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
917 buf = do_malloc(bufsz);
920 memset(buf, 'a', bufsz);
922 ret = do_truncate(fd, filsz);
924 ret = do_pwrite(fd, buf, bufsz, 0);
928 /* offset at the beginning */
930 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
931 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
933 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
934 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
936 /* offset around data-hole boundary */
937 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
938 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
940 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
941 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
942 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
943 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
945 /* offset around eof */
946 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
947 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
948 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
949 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
950 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
951 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
956 /* test hole begin and data end */
957 static int test04(int fd, int testnum)
960 char *buf = "ABCDEFGH";
961 int bufsz, holsz, filsz;
964 holsz = alloc_size * 2;
965 filsz = holsz + bufsz;
967 /* |- HOLE -|- HOLE -|- DATA -| */
969 ret = do_pwrite(fd, buf, bufsz, holsz);
973 /* offset at the beginning */
974 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
975 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
976 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
977 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
978 /* offset around hole-data boundary */
979 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
980 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
981 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
982 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
983 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
984 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
986 /* offset around eof */
987 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
988 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
989 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
990 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
991 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
992 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
997 /* test a larger full file */
998 static int test03(int fd, int testnum)
1001 int bufsz = alloc_size * 2 + 100;
1005 buf = do_malloc(bufsz);
1008 memset(buf, 'a', bufsz);
1010 ret = do_pwrite(fd, buf, bufsz, 0);
1014 /* offset at the beginning */
1015 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
1016 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
1017 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
1018 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
1020 /* offset around eof */
1021 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
1022 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1023 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
1024 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
1025 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1026 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1033 /* test tiny full file */
1034 static int test02(int fd, int testnum)
1037 char buf[] = "ABCDEFGH";
1040 bufsz = strlen(buf);
1045 ret = do_pwrite(fd, buf, bufsz, 0);
1049 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
1050 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
1051 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
1052 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
1053 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1054 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
1055 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
1056 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1057 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1063 /* test empty file */
1064 static int test01(int fd, int testnum)
1068 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
1069 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
1070 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
1077 int (*test_func)(int fd, int testnum);
1081 struct testrec seek_tests[] = {
1082 { 1, test01, "Test empty file" },
1083 { 2, test02, "Test a tiny full file" },
1084 { 3, test03, "Test a larger full file" },
1085 { 4, test04, "Test file hole at beg, data at end" },
1086 { 5, test05, "Test file data at beg, hole at end" },
1087 { 6, test06, "Test file hole data hole data" },
1088 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
1089 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
1090 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
1091 { 10, test10, "Test a huge file for offset overflow" },
1092 { 11, test11, "Test a huge file for block number signed" },
1093 { 12, test12, "Test a huge file for block number overflow" },
1094 { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
1095 { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
1096 { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
1097 { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
1098 { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
1099 { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
1100 { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
1101 { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
1102 { 21, test21, "Test file SEEK_HOLE that was created by PUNCH_HOLE" },
1105 static int run_test(struct testrec *tr)
1107 int ret = 0, fd = -1;
1110 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
1112 fd = do_create(filename);
1114 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
1115 ret = tr->test_func(fd, tr->test_num);
1123 static int test_basic_support(void)
1130 fd = do_create(base_file_path);
1134 get_file_system(fd);
1136 ret = get_io_sizes(fd);
1141 bufsz = alloc_size * 2;
1144 buf = do_malloc(bufsz);
1147 memset(buf, 'a', bufsz);
1149 /* File with 2 allocated blocks.... */
1150 ret = do_pwrite(fd, buf, bufsz, 0);
1154 /* followed by a hole... */
1155 ret = do_truncate(fd, filsz);
1159 /* Is SEEK_DATA supported in the kernel? */
1160 pos = lseek(fd, 0, SEEK_DATA);
1162 fprintf(stderr, "Kernel does not support llseek(2) extension "
1163 "SEEK_DATA. Aborting.\n");
1168 /* Is SEEK_HOLE supported in the kernel? */
1169 pos = lseek(fd, 0, SEEK_HOLE);
1171 fprintf(stderr, "Kernel does not support llseek(2) extension "
1172 "SEEK_HOLE. Aborting.\n");
1178 default_behavior = 1;
1179 fprintf(stderr, "File system supports the default behavior.\n");
1182 if (default_behavior && !allow_default_behavior) {
1183 fprintf(stderr, "Default behavior is not allowed. Aborting.\n");
1188 /* Is unwritten extent supported? */
1190 if (fallocate(fd, 0, 0, alloc_size * 2) == -1) {
1191 if (errno == EOPNOTSUPP) {
1192 fprintf(stderr, "File system does not support fallocate.\n");
1194 fprintf(stderr, "ERROR %d: Failed to preallocate "
1195 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1201 pos = lseek(fd, 0, SEEK_DATA);
1203 fprintf(stderr, "File system does not support unwritten extents.\n");
1205 unwritten_extents = 1;
1207 /* Is punch hole supported? */
1208 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1209 0, alloc_size) == -1)
1210 fprintf(stderr, "File system does not support punch hole.\n");
1222 void usage(char *cmd)
1224 fprintf(stdout, "Usage: %s [-tf] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1228 int main(int argc, char **argv)
1233 int check_support = 0;
1234 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1235 int teststart, testend;
1238 * First twelve tests are used by generic/285. To run these is the
1239 * default. Further tests are run e.g. by generic/436. They are not
1240 * run by default in order to not regress older kernels.
1245 while ((opt = getopt(argc, argv, "tfs:e:")) != -1) {
1251 allow_default_behavior = 0;
1254 teststart = strtol(optarg, NULL, 10);
1255 if (teststart <= 0 || teststart > numtests) {
1256 fprintf(stdout, "Invalid starting test: %s\n",
1262 testend = strtol(optarg, NULL, 10);
1263 if (testend <= 0 || testend > numtests) {
1264 fprintf(stdout, "Invalid final test: %s\n",
1274 /* should be exactly one arg left, the filename */
1275 if (optind != argc - 1)
1278 if (teststart > testend) {
1279 fprintf(stdout, "Starting test is larger than final test!\n");
1283 base_file_path = (char *)strdup(argv[optind]);
1285 ret = test_basic_support();
1286 if (ret || check_support)
1289 for (i = 0; i < numtests; ++i) {
1290 if (seek_tests[i].test_num >= teststart &&
1291 seek_tests[i].test_num <= testend) {
1292 ret = run_test(&seek_tests[i]);
1299 free(base_file_path);