2 * Copyright (C) 2011 Oracle. All rights reserved.
3 * Copyright (C) 2011 Red Hat. All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
20 #define _XOPEN_SOURCE 500
21 #define _FILE_OFFSET_BITS 64
22 #include <sys/types.h>
38 static blksize_t alloc_size;
39 int default_behavior = 0;
40 int unwritten_extents = 0;
43 static void get_file_system(int fd)
47 if (!fstatfs(fd, &buf)) {
48 fprintf(stdout, "File system magic#: 0x%lx\n",
49 (unsigned long int)buf.f_type);
53 static int get_io_sizes(int fd)
55 off_t pos = 0, offset = 1;
59 ret = fstat(fd, &buf);
61 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
66 /* st_blksize is typically also the allocation size */
67 alloc_size = buf.st_blksize;
69 /* try to discover the actual alloc size */
70 while (pos == 0 && offset < alloc_size) {
73 pwrite(fd, "a", 1, offset);
74 pos = lseek(fd, 0, SEEK_DATA);
81 while (shift && offset < alloc_size) {
83 pwrite(fd, "a", 1, offset);
84 pos = lseek(fd, 0, SEEK_DATA);
87 offset += pos ? -shift : shift;
91 offset += pos ? 0 : 1;
93 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
97 fprintf(stderr, "Kernel does not support llseek(2) extension "
98 "SEEK_DATA. Aborting.\n");
102 #define do_free(x) do { if(x) free(x); } while(0);
104 static void *do_malloc(size_t size)
110 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
116 static int do_truncate(int fd, off_t length)
120 ret = ftruncate(fd, length);
122 fprintf(stderr, " ERROR %d: Failed to extend file "
123 "to %ld bytes\n", errno, (long)length);
127 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
131 ret = fallocate(fd, mode, offset, length);
133 fprintf(stderr, " ERROR %d: Failed to preallocate "
134 "space to %ld bytes\n", errno, (long) length);
140 * Synchnorize all dirty pages in the file range starting from
141 * offset to nbytes length.
143 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
147 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
149 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
155 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
157 ssize_t ret, written = 0;
159 while (count > written) {
160 ret = pwrite(fd, buf + written, count - written, offset + written);
162 /* Don't warn about too large file. It's fs dependent. */
165 fprintf(stderr, " ERROR %d: Failed to write %ld "
166 "bytes\n", errno, (long)count);
175 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
177 static int do_create(const char *filename)
181 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
183 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
189 static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
190 off_t set, off_t exp)
195 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
198 * The file pointer can be set to different values depending
199 * on the implementation. For SEEK_HOLE, EOF could be a valid
200 * value. For SEEK_DATA, supplied offset could be the valid
204 if (origin == SEEK_HOLE && exp2 != -1)
206 if (origin == SEEK_DATA && default_behavior && set < filsz)
209 pos = lseek(fd, set, origin);
211 if (pos == -1 && exp == -1) {
212 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
214 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
216 ret = !(errno == ENXIO);
219 x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
221 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
222 (long long)exp, (long long)exp2, (long long)pos);
223 ret = !(pos == exp || pos == exp2);
226 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
231 static int huge_file_test(int fd, int testnum, off_t filsz)
234 int bufsz = alloc_size * 16; /* XFS seems to round allocated size */
235 off_t off = filsz - bufsz;
238 buf = do_malloc(bufsz);
241 memset(buf, 'a', bufsz);
243 /* |- DATA -|- HUGE HOLE -|- DATA -| */
244 ret = do_pwrite(fd, buf, bufsz, 0);
247 ret = do_pwrite(fd, buf, bufsz, off);
250 * Report success. Filesystem just cannot handle so large
251 * offsets and correctly reports it.
253 if (errno == EFBIG) {
254 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
260 /* offset at the beginning */
261 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
262 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
263 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
264 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
266 /* offset around eof */
267 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off, off + bufsz);
268 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off, off);
269 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, off + 1, off + 1);
270 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off - bufsz, off);
278 * Make sure hole size is properly reported when starting in the middle of a
279 * hole in ext? doubly indirect tree
281 static int test20(int fd, int testnum)
288 buf = do_malloc(bufsz);
291 memset(buf, 'a', bufsz);
293 /* Magic size in the middle of ext[23] triple indirect tree */
294 filsz = (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + 2 * bufsz / 4 + 5) * bufsz;
295 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
299 /* Offset inside ext[23] indirect block */
300 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 14 * bufsz, filsz - bufsz);
301 /* Offset inside ext[23] doubly indirect block */
302 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, (12 + 2 * bufsz / 4) * bufsz, filsz - bufsz);
303 /* Offsets inside ext[23] triply indirect block */
304 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
305 (12 + bufsz / 4 + bufsz / 4 * bufsz / 4 + 3 * bufsz / 4 + 5) * bufsz, filsz - bufsz);
306 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
307 (12 + bufsz / 4 + 7 * bufsz / 4 * bufsz / 4 + 5 * bufsz / 4) * bufsz, filsz - bufsz);
308 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
309 (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + bufsz / 4 + 11) * bufsz, filsz - bufsz);
316 * Make sure hole size is properly reported when starting in the middle of a
317 * hole in ext? indirect tree
319 static int test19(int fd, int testnum)
326 buf = do_malloc(bufsz);
329 memset(buf, 'a', bufsz);
331 /* Magic size just beyond ext[23] indirect tree size */
332 filsz = (12 + bufsz / 4 + 1) * bufsz;
333 ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
337 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
338 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 12*bufsz, filsz - bufsz);
339 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, (12 + bufsz / 4 - 8)*bufsz, filsz - bufsz);
346 /* Make sure we get ENXIO if we pass in a negative offset. */
347 static int test18(int fd, int testnum)
351 /* file size doesn't matter in this test, set to 0 */
352 ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
353 ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
358 static int test17(int fd, int testnum)
361 int pagesz = sysconf(_SC_PAGE_SIZE);
365 if (!unwritten_extents) {
366 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
370 if (pagesz < 4 * alloc_size) {
371 fprintf(stdout, "Test skipped as page size (%d) is less than "
372 "four times allocation size (%d).\n",
373 pagesz, (int)alloc_size);
379 buf = do_malloc(bufsz);
384 memset(buf, 'a', bufsz);
386 ret = do_fallocate(fd, 0, filsz, 0);
390 ret = do_pwrite(fd, buf, bufsz, 0);
394 ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
398 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
399 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, bufsz);
400 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
401 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, 1, bufsz);
402 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
403 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
404 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
405 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
406 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
407 ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
408 ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
409 ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
412 ret += do_fallocate(fd, 0, filsz, 0);
414 ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
415 ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
416 ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
417 ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
425 * test file with unwritten extents, having non-contiguous dirty pages in
426 * the unwritten extent.
428 static int test16(int fd, int testnum)
432 int bufsz = sysconf(_SC_PAGE_SIZE);
435 if (!unwritten_extents) {
436 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
440 /* HOLE - unwritten DATA in dirty page */
441 /* Each unit is bufsz */
442 buf = do_malloc(bufsz);
445 memset(buf, 'a', bufsz);
447 /* preallocate 4M space to file */
448 ret = do_fallocate(fd, 0, filsz, 0);
452 ret = do_pwrite(fd, buf, bufsz, 0);
456 ret = do_pwrite(fd, buf, bufsz, filsz/2);
460 /* offset at the beginning */
461 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
462 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
463 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
464 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
465 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
466 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, filsz/2,
475 * test file with unwritten extents, having page just after end of unwritten
478 static int test15(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 /* One page written just after end of unwritten extent... */
507 ret = do_pwrite(fd, buf, bufsz, filsz);
511 /* update file size */
514 /* offset at the beginning */
515 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
516 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
517 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
518 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
519 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
527 * test file with unwritten extents, only have pagevec worth of dirty pages
528 * in page cache, a hole and then another page.
530 static int test14(int fd, int testnum)
534 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
537 if (!unwritten_extents) {
538 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
542 /* HOLE - unwritten DATA in dirty page */
543 /* Each unit is bufsz */
544 buf = do_malloc(bufsz);
547 memset(buf, 'a', bufsz);
549 /* preallocate 4M space to file */
550 ret = do_fallocate(fd, 0, filsz, 0);
554 ret = do_pwrite(fd, buf, bufsz, 0);
558 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
562 /* offset at the beginning */
563 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
564 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
565 ret += do_lseek(testnum, 3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
566 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 0, 0);
567 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, 1, 1);
568 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
576 * test file with unwritten extents, only have pagevec worth of dirty pages
579 static int test13(int fd, int testnum)
583 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
586 if (!unwritten_extents) {
587 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
591 /* HOLE - unwritten DATA in dirty page */
592 /* Each unit is bufsz */
593 buf = do_malloc(bufsz);
596 memset(buf, 'a', bufsz);
598 /* preallocate 4M space to file */
599 ret = do_fallocate(fd, 0, filsz, 0);
603 ret = do_pwrite(fd, buf, bufsz, 0);
607 /* offset at the beginning */
608 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
609 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
610 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
611 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
619 * Test huge file to check for overflows of block counts due to usage of
622 static int test12(int fd, int testnum)
624 return huge_file_test(fd, testnum,
625 ((long long)alloc_size << 32) + (1 << 20));
629 * Test huge file to check for overflows of block counts due to usage of
632 static int test11(int fd, int testnum)
634 return huge_file_test(fd, testnum,
635 ((long long)alloc_size << 31) + (1 << 20));
638 /* Test an 8G file to check for offset overflows at 1 << 32 */
639 static int test10(int fd, int testnum)
641 return huge_file_test(fd, testnum, 8ULL << 30);
645 * test file with unwritten extents, have both dirty and
646 * writeback pages in page cache.
648 static int test09(int fd, int testnum)
652 int bufsz = alloc_size;
653 int filsz = bufsz * 100 + bufsz;
655 if (!unwritten_extents) {
656 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
661 * HOLE - unwritten DATA in dirty page - HOLE -
662 * unwritten DATA in writeback page
665 /* Each unit is bufsz */
666 buf = do_malloc(bufsz);
669 memset(buf, 'a', bufsz);
671 /* preallocate 8M space to file */
672 ret = do_fallocate(fd, 0, filsz, 0);
676 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
678 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
684 * Sync out dirty pages from bufsz * 100, this will convert
685 * the dirty page to writeback.
687 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
691 /* offset at the beginning */
692 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
693 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
694 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
695 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
702 /* test file with unwritten extent, only have writeback page */
703 static int test08(int fd, int testnum)
707 int bufsz = alloc_size;
708 int filsz = bufsz * 10 + bufsz;
710 if (!unwritten_extents) {
711 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
715 /* HOLE - unwritten DATA in writeback page */
716 /* Each unit is bufsz */
717 buf = do_malloc(bufsz);
720 memset(buf, 'a', bufsz);
722 /* preallocate 4M space to file */
723 ret = do_fallocate(fd, 0, filsz, 0);
727 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
731 /* Sync out all file */
732 ret = do_sync_dirty_pages(fd, 0, 0);
736 /* offset at the beginning */
737 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
738 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
739 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
740 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
748 * test file with unwritten extents, only have dirty pages
751 static int test07(int fd, int testnum)
755 int bufsz = alloc_size;
756 int filsz = bufsz * 10 + bufsz;
758 if (!unwritten_extents) {
759 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
763 /* HOLE - unwritten DATA in dirty page */
764 /* Each unit is bufsz */
765 buf = do_malloc(bufsz);
768 memset(buf, 'a', bufsz);
770 /* preallocate 4M space to file */
771 ret = do_fallocate(fd, 0, filsz, 0);
775 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
779 /* offset at the beginning */
780 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
781 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
782 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
783 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
790 /* test hole data hole data */
791 static int test06(int fd, int testnum)
795 int bufsz = alloc_size;
796 int filsz = bufsz * 4;
799 /* HOLE - DATA - HOLE - DATA */
800 /* Each unit is bufsz */
802 buf = do_malloc(bufsz);
806 memset(buf, 'a', bufsz);
808 ret = do_pwrite(fd, buf, bufsz, bufsz);
810 do_pwrite(fd, buf, bufsz, bufsz * 3);
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);
818 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
820 /* offset around first hole-data boundary */
822 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
823 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
824 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
825 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
826 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
827 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
829 /* offset around data-hole boundary */
831 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
832 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
833 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
834 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
835 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
836 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
838 /* offset around second hole-data boundary */
840 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
841 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
842 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
843 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
844 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
845 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
847 /* offset around the end of file */
849 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
850 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
851 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
852 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
853 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
854 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
861 /* test file with data at the beginning and a hole at the end */
862 static int test05(int fd, int testnum)
866 int bufsz = alloc_size;
867 int filsz = bufsz * 4;
869 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
871 buf = do_malloc(bufsz);
874 memset(buf, 'a', bufsz);
876 ret = do_truncate(fd, filsz);
878 ret = do_pwrite(fd, buf, bufsz, 0);
882 /* offset at the beginning */
884 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
885 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
887 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
888 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
890 /* offset around data-hole boundary */
891 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
892 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
894 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
895 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
896 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
897 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
899 /* offset around eof */
900 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
901 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
902 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
903 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
904 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
905 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
910 /* test hole begin and data end */
911 static int test04(int fd, int testnum)
914 char *buf = "ABCDEFGH";
915 int bufsz, holsz, filsz;
918 holsz = alloc_size * 2;
919 filsz = holsz + bufsz;
921 /* |- HOLE -|- HOLE -|- DATA -| */
923 ret = do_pwrite(fd, buf, bufsz, holsz);
927 /* offset at the beginning */
928 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
929 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
930 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
931 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
932 /* offset around hole-data boundary */
933 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
934 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
935 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
936 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
937 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
938 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
940 /* offset around eof */
941 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
942 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
943 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
944 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
945 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
946 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
951 /* test a larger full file */
952 static int test03(int fd, int testnum)
955 int bufsz = alloc_size * 2 + 100;
959 buf = do_malloc(bufsz);
962 memset(buf, 'a', bufsz);
964 ret = do_pwrite(fd, buf, bufsz, 0);
968 /* offset at the beginning */
969 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
970 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
971 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
972 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
974 /* offset around eof */
975 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
976 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
977 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
978 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
979 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
980 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
987 /* test tiny full file */
988 static int test02(int fd, int testnum)
991 char buf[] = "ABCDEFGH";
999 ret = do_pwrite(fd, buf, bufsz, 0);
1003 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
1004 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
1005 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
1006 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
1007 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
1008 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
1009 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
1010 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
1011 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
1017 /* test empty file */
1018 static int test01(int fd, int testnum)
1022 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
1023 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
1024 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
1031 int (*test_func)(int fd, int testnum);
1035 struct testrec seek_tests[] = {
1036 { 1, test01, "Test empty file" },
1037 { 2, test02, "Test a tiny full file" },
1038 { 3, test03, "Test a larger full file" },
1039 { 4, test04, "Test file hole at beg, data at end" },
1040 { 5, test05, "Test file data at beg, hole at end" },
1041 { 6, test06, "Test file hole data hole data" },
1042 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
1043 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
1044 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
1045 { 10, test10, "Test a huge file for offset overflow" },
1046 { 11, test11, "Test a huge file for block number signed" },
1047 { 12, test12, "Test a huge file for block number overflow" },
1048 { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
1049 { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
1050 { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
1051 { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
1052 { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
1053 { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
1054 { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
1055 { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
1058 static int run_test(struct testrec *tr)
1060 int ret = 0, fd = -1;
1063 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
1065 fd = do_create(filename);
1067 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
1068 ret = tr->test_func(fd, tr->test_num);
1076 static int test_basic_support(void)
1083 fd = do_create(base_file_path);
1087 get_file_system(fd);
1089 ret = get_io_sizes(fd);
1094 bufsz = alloc_size * 2;
1097 buf = do_malloc(bufsz);
1100 memset(buf, 'a', bufsz);
1102 /* File with 2 allocated blocks.... */
1103 ret = do_pwrite(fd, buf, bufsz, 0);
1107 /* followed by a hole... */
1108 ret = do_truncate(fd, filsz);
1112 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
1113 pos = lseek(fd, 0, SEEK_HOLE);
1115 fprintf(stderr, "Kernel does not support llseek(2) extension "
1116 "SEEK_HOLE. Aborting.\n");
1122 default_behavior = 1;
1123 fprintf(stderr, "File system supports the default behavior.\n");
1127 if (fallocate(fd, 0, 0, alloc_size) == -1) {
1128 if (errno == EOPNOTSUPP)
1129 fprintf(stderr, "File system does not support fallocate.");
1131 fprintf(stderr, "ERROR %d: Failed to preallocate "
1132 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1138 pos = lseek(fd, 0, SEEK_DATA);
1140 fprintf(stderr, "File system does not support unwritten extents.\n");
1143 unwritten_extents = 1;
1153 void usage(char *cmd)
1155 fprintf(stdout, "Usage: %s [-t] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1159 int main(int argc, char **argv)
1164 int check_support = 0;
1165 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1166 int teststart, testend;
1169 * First twelve tests are used by generic/285. To run these is the
1170 * default. Further tests are run e.g. by generic/436. They are not
1171 * run by default in order to not regress older kernels.
1176 while ((opt = getopt(argc, argv, "ts:e:")) != -1) {
1182 teststart = strtol(optarg, NULL, 10);
1183 if (teststart <= 0 || teststart > numtests) {
1184 fprintf(stdout, "Invalid starting test: %s\n",
1190 testend = strtol(optarg, NULL, 10);
1191 if (testend <= 0 || testend > numtests) {
1192 fprintf(stdout, "Invalid final test: %s\n",
1202 /* should be exactly one arg left, the filename */
1203 if (optind != argc - 1)
1206 if (teststart > testend) {
1207 fprintf(stdout, "Starting test is larger than final test!\n");
1211 base_file_path = (char *)strdup(argv[optind]);
1213 ret = test_basic_support();
1214 if (ret || check_support)
1217 for (i = 0; i < numtests; ++i) {
1218 if (seek_tests[i].test_num >= teststart &&
1219 seek_tests[i].test_num <= testend) {
1220 ret = run_test(&seek_tests[i]);
1227 free(base_file_path);