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 */
400 ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
401 ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
402 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, LLONG_MIN, -1);
403 ret += do_lseek(testnum, 4, fd, 0, SEEK_DATA, LLONG_MIN, -1);
408 static int test17(int fd, int testnum)
411 int pagesz = sysconf(_SC_PAGE_SIZE);
415 if (!unwritten_extents) {
416 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
420 if (pagesz < 4 * alloc_size) {
421 fprintf(stdout, "Test skipped as page size (%d) is less than "
422 "four times allocation size (%d).\n",
423 pagesz, (int)alloc_size);
429 buf = do_malloc(bufsz);
434 memset(buf, 'a', bufsz);
436 ret = do_fallocate(fd, 0, filsz, 0);
440 ret = do_pwrite(fd, buf, bufsz, 0);
444 ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
448 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
449 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, bufsz);
450 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
451 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, 1, bufsz);
452 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
453 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
454 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
455 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
456 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
457 ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
458 ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
459 ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
462 ret += do_fallocate(fd, 0, filsz, 0);
464 ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
465 ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
466 ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
467 ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
475 * test file with unwritten extents, having non-contiguous dirty pages in
476 * the unwritten extent.
478 static int test16(int fd, int testnum)
482 int bufsz = sysconf(_SC_PAGE_SIZE);
485 if (!unwritten_extents) {
486 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
490 /* HOLE - unwritten DATA in dirty page */
491 /* Each unit is bufsz */
492 buf = do_malloc(bufsz);
495 memset(buf, 'a', bufsz);
497 /* preallocate 4M space to file */
498 ret = do_fallocate(fd, 0, filsz, 0);
502 ret = do_pwrite(fd, buf, bufsz, 0);
506 ret = do_pwrite(fd, buf, bufsz, filsz/2);
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/2);
516 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, filsz/2,
525 * test file with unwritten extents, having page just after end of unwritten
528 static int test15(int fd, int testnum)
532 int bufsz = sysconf(_SC_PAGE_SIZE);
535 if (!unwritten_extents) {
536 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
540 /* HOLE - unwritten DATA in dirty page */
541 /* Each unit is bufsz */
542 buf = do_malloc(bufsz);
545 memset(buf, 'a', bufsz);
547 /* preallocate 4M space to file */
548 ret = do_fallocate(fd, 0, filsz, 0);
552 ret = do_pwrite(fd, buf, bufsz, 0);
556 /* One page written just after end of unwritten extent... */
557 ret = do_pwrite(fd, buf, bufsz, filsz);
561 /* update file size */
564 /* offset at the beginning */
565 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
566 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
567 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
568 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
569 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
577 * test file with unwritten extents, only have pagevec worth of dirty pages
578 * in page cache, a hole and then another page.
580 static int test14(int fd, int testnum)
584 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
587 if (!unwritten_extents) {
588 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
592 /* HOLE - unwritten DATA in dirty page */
593 /* Each unit is bufsz */
594 buf = do_malloc(bufsz);
597 memset(buf, 'a', bufsz);
599 /* preallocate 4M space to file */
600 ret = do_fallocate(fd, 0, filsz, 0);
604 ret = do_pwrite(fd, buf, bufsz, 0);
608 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
612 /* offset at the beginning */
613 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
614 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
615 ret += do_lseek(testnum, 3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
616 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 0, 0);
617 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, 1, 1);
618 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
626 * test file with unwritten extents, only have pagevec worth of dirty pages
629 static int test13(int fd, int testnum)
633 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
636 if (!unwritten_extents) {
637 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
641 /* HOLE - unwritten DATA in dirty page */
642 /* Each unit is bufsz */
643 buf = do_malloc(bufsz);
646 memset(buf, 'a', bufsz);
648 /* preallocate 4M space to file */
649 ret = do_fallocate(fd, 0, filsz, 0);
653 ret = do_pwrite(fd, buf, bufsz, 0);
657 /* offset at the beginning */
658 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
659 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
660 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
661 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
669 * Test huge file to check for overflows of block counts due to usage of
672 static int test12(int fd, int testnum)
674 return huge_file_test(fd, testnum,
675 ((long long)alloc_size << 32) + (1 << 20));
679 * Test huge file to check for overflows of block counts due to usage of
682 static int test11(int fd, int testnum)
684 return huge_file_test(fd, testnum,
685 ((long long)alloc_size << 31) + (1 << 20));
688 /* Test an 8G file to check for offset overflows at 1 << 32 */
689 static int test10(int fd, int testnum)
691 return huge_file_test(fd, testnum, 8ULL << 30);
695 * test file with unwritten extents, have both dirty and
696 * writeback pages in page cache.
698 static int test09(int fd, int testnum)
702 int bufsz = alloc_size;
703 int filsz = bufsz * 100 + bufsz;
705 if (!unwritten_extents) {
706 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
711 * HOLE - unwritten DATA in dirty page - HOLE -
712 * unwritten DATA in writeback page
715 /* Each unit is bufsz */
716 buf = do_malloc(bufsz);
719 memset(buf, 'a', bufsz);
721 /* preallocate 8M space to file */
722 ret = do_fallocate(fd, 0, filsz, 0);
726 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
728 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
734 * Sync out dirty pages from bufsz * 100, this will convert
735 * the dirty page to writeback.
737 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
741 /* offset at the beginning */
742 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
743 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
744 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
745 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
752 /* test file with unwritten extent, only have writeback page */
753 static int test08(int fd, int testnum)
757 int bufsz = alloc_size;
758 int filsz = bufsz * 10 + bufsz;
760 if (!unwritten_extents) {
761 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
765 /* HOLE - unwritten DATA in writeback page */
766 /* Each unit is bufsz */
767 buf = do_malloc(bufsz);
770 memset(buf, 'a', bufsz);
772 /* preallocate 4M space to file */
773 ret = do_fallocate(fd, 0, filsz, 0);
777 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
781 /* Sync out all file */
782 ret = do_sync_dirty_pages(fd, 0, 0);
786 /* offset at the beginning */
787 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
788 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
789 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
790 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
798 * test file with unwritten extents, only have dirty pages
801 static int test07(int fd, int testnum)
805 int bufsz = alloc_size;
806 int filsz = bufsz * 10 + bufsz;
808 if (!unwritten_extents) {
809 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
813 /* HOLE - unwritten DATA in dirty page */
814 /* Each unit is bufsz */
815 buf = do_malloc(bufsz);
818 memset(buf, 'a', bufsz);
820 /* preallocate 4M space to file */
821 ret = do_fallocate(fd, 0, filsz, 0);
825 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
829 /* offset at the beginning */
830 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
831 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
832 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
833 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
840 /* test hole data hole data */
841 static int test06(int fd, int testnum)
845 int bufsz = alloc_size;
846 int filsz = bufsz * 4;
849 /* HOLE - DATA - HOLE - DATA */
850 /* Each unit is bufsz */
852 buf = do_malloc(bufsz);
856 memset(buf, 'a', bufsz);
858 ret = do_pwrite(fd, buf, bufsz, bufsz);
860 do_pwrite(fd, buf, bufsz, bufsz * 3);
864 /* offset at the beginning */
865 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
866 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
867 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
868 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
870 /* offset around first hole-data boundary */
872 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
873 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
874 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
875 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
876 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
877 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
879 /* offset around data-hole boundary */
881 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
882 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
883 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
884 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
885 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
886 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
888 /* offset around second hole-data boundary */
890 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
891 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
892 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
893 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
894 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
895 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
897 /* offset around the end of file */
899 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
900 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
901 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
902 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
903 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
904 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
911 /* test file with data at the beginning and a hole at the end */
912 static int test05(int fd, int testnum)
916 int bufsz = alloc_size;
917 int filsz = bufsz * 4;
919 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
921 buf = do_malloc(bufsz);
924 memset(buf, 'a', bufsz);
926 ret = do_truncate(fd, filsz);
928 ret = do_pwrite(fd, buf, bufsz, 0);
932 /* offset at the beginning */
934 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
935 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
937 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
938 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
940 /* offset around data-hole boundary */
941 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
942 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
944 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
945 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
946 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
947 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
949 /* offset around eof */
950 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
951 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
952 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
953 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
954 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
955 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
960 /* test hole begin and data end */
961 static int test04(int fd, int testnum)
964 char *buf = "ABCDEFGH";
965 int bufsz, holsz, filsz;
968 holsz = alloc_size * 2;
969 filsz = holsz + bufsz;
971 /* |- HOLE -|- HOLE -|- DATA -| */
973 ret = do_pwrite(fd, buf, bufsz, holsz);
977 /* offset at the beginning */
978 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
979 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
980 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
981 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
982 /* offset around hole-data boundary */
983 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
984 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
985 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
986 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
987 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
988 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
990 /* offset around eof */
991 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
992 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
993 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
994 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
995 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
996 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
1001 /* test a larger full file */
1002 static int test03(int fd, int testnum)
1005 int bufsz = alloc_size * 2 + 100;
1009 buf = do_malloc(bufsz);
1012 memset(buf, 'a', bufsz);
1014 ret = do_pwrite(fd, buf, bufsz, 0);
1018 /* offset at the beginning */
1019 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
1020 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
1021 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
1022 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
1024 /* offset around eof */
1025 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
1026 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1027 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
1028 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
1029 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1030 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1037 /* test tiny full file */
1038 static int test02(int fd, int testnum)
1041 char buf[] = "ABCDEFGH";
1044 bufsz = strlen(buf);
1049 ret = do_pwrite(fd, buf, bufsz, 0);
1053 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
1054 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
1055 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
1056 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
1057 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1058 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
1059 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
1060 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1061 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1067 /* test empty file */
1068 static int test01(int fd, int testnum)
1072 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
1073 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
1074 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
1081 int (*test_func)(int fd, int testnum);
1085 struct testrec seek_tests[] = {
1086 { 1, test01, "Test empty file" },
1087 { 2, test02, "Test a tiny full file" },
1088 { 3, test03, "Test a larger full file" },
1089 { 4, test04, "Test file hole at beg, data at end" },
1090 { 5, test05, "Test file data at beg, hole at end" },
1091 { 6, test06, "Test file hole data hole data" },
1092 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
1093 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
1094 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
1095 { 10, test10, "Test a huge file for offset overflow" },
1096 { 11, test11, "Test a huge file for block number signed" },
1097 { 12, test12, "Test a huge file for block number overflow" },
1098 { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
1099 { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
1100 { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
1101 { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
1102 { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
1103 { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
1104 { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
1105 { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
1106 { 21, test21, "Test file SEEK_HOLE that was created by PUNCH_HOLE" },
1109 static int run_test(struct testrec *tr)
1111 int ret = 0, fd = -1;
1114 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
1116 fd = do_create(filename);
1118 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
1119 ret = tr->test_func(fd, tr->test_num);
1127 static int test_basic_support(void)
1134 fd = do_create(base_file_path);
1138 get_file_system(fd);
1140 ret = get_io_sizes(fd);
1145 bufsz = alloc_size * 2;
1148 buf = do_malloc(bufsz);
1151 memset(buf, 'a', bufsz);
1153 /* File with 2 allocated blocks.... */
1154 ret = do_pwrite(fd, buf, bufsz, 0);
1158 /* followed by a hole... */
1159 ret = do_truncate(fd, filsz);
1163 /* Is SEEK_DATA supported in the kernel? */
1164 pos = lseek(fd, 0, SEEK_DATA);
1166 fprintf(stderr, "Kernel does not support llseek(2) extension "
1167 "SEEK_DATA. Aborting.\n");
1172 /* Is SEEK_HOLE supported in the kernel? */
1173 pos = lseek(fd, 0, SEEK_HOLE);
1175 fprintf(stderr, "Kernel does not support llseek(2) extension "
1176 "SEEK_HOLE. Aborting.\n");
1182 default_behavior = 1;
1183 fprintf(stderr, "File system supports the default behavior.\n");
1186 if (default_behavior && !allow_default_behavior) {
1187 fprintf(stderr, "Default behavior is not allowed. Aborting.\n");
1192 /* Is unwritten extent supported? */
1194 if (fallocate(fd, 0, 0, alloc_size * 2) == -1) {
1195 if (errno == EOPNOTSUPP) {
1196 fprintf(stderr, "File system does not support fallocate.\n");
1198 fprintf(stderr, "ERROR %d: Failed to preallocate "
1199 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1205 pos = lseek(fd, 0, SEEK_DATA);
1207 fprintf(stderr, "File system does not support unwritten extents.\n");
1209 unwritten_extents = 1;
1211 /* Is punch hole supported? */
1212 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
1213 0, alloc_size) == -1)
1214 fprintf(stderr, "File system does not support punch hole.\n");
1226 void usage(char *cmd)
1228 fprintf(stdout, "Usage: %s [-tf] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1232 int main(int argc, char **argv)
1237 int check_support = 0;
1238 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1239 int teststart, testend;
1242 * First twelve tests are used by generic/285. To run these is the
1243 * default. Further tests are run e.g. by generic/436. They are not
1244 * run by default in order to not regress older kernels.
1249 while ((opt = getopt(argc, argv, "tfs:e:")) != -1) {
1255 allow_default_behavior = 0;
1258 teststart = strtol(optarg, NULL, 10);
1259 if (teststart <= 0 || teststart > numtests) {
1260 fprintf(stdout, "Invalid starting test: %s\n",
1266 testend = strtol(optarg, NULL, 10);
1267 if (testend <= 0 || testend > numtests) {
1268 fprintf(stdout, "Invalid final test: %s\n",
1278 /* should be exactly one arg left, the filename */
1279 if (optind != argc - 1)
1282 if (teststart > testend) {
1283 fprintf(stdout, "Starting test is larger than final test!\n");
1287 base_file_path = (char *)strdup(argv[optind]);
1289 ret = test_basic_support();
1290 if (ret || check_support)
1293 for (i = 0; i < numtests; ++i) {
1294 if (seek_tests[i].test_num >= teststart &&
1295 seek_tests[i].test_num <= testend) {
1296 ret = run_test(&seek_tests[i]);
1303 free(base_file_path);