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);
277 /* Make sure we get ENXIO if we pass in a negative offset. */
278 static int test18(int fd, int testnum)
282 /* file size doesn't matter in this test, set to 0 */
283 ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
284 ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
289 static int test17(int fd, int testnum)
292 int pagesz = sysconf(_SC_PAGE_SIZE);
296 if (!unwritten_extents) {
297 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
301 if (pagesz < 4 * alloc_size) {
302 fprintf(stdout, "Test skipped as page size (%d) is less than "
303 "four times allocation size (%d).\n",
304 pagesz, (int)alloc_size);
310 buf = do_malloc(bufsz);
315 memset(buf, 'a', bufsz);
317 ret = do_fallocate(fd, 0, filsz, 0);
321 ret = do_pwrite(fd, buf, bufsz, 0);
325 ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
329 ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
330 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, bufsz);
331 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
332 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, 1, bufsz);
333 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
334 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
335 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
336 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
337 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
338 ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
339 ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
340 ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
343 ret += do_fallocate(fd, 0, filsz, 0);
345 ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
346 ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
347 ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
348 ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
356 * test file with unwritten extents, having non-contiguous dirty pages in
357 * the unwritten extent.
359 static int test16(int fd, int testnum)
363 int bufsz = sysconf(_SC_PAGE_SIZE);
366 if (!unwritten_extents) {
367 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
371 /* HOLE - unwritten DATA in dirty page */
372 /* Each unit is bufsz */
373 buf = do_malloc(bufsz);
376 memset(buf, 'a', bufsz);
378 /* preallocate 4M space to file */
379 ret = do_fallocate(fd, 0, filsz, 0);
383 ret = do_pwrite(fd, buf, bufsz, 0);
387 ret = do_pwrite(fd, buf, bufsz, filsz/2);
391 /* offset at the beginning */
392 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
393 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
394 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
395 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
396 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
397 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, filsz/2,
406 * test file with unwritten extents, having page just after end of unwritten
409 static int test15(int fd, int testnum)
413 int bufsz = sysconf(_SC_PAGE_SIZE);
416 if (!unwritten_extents) {
417 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
421 /* HOLE - unwritten DATA in dirty page */
422 /* Each unit is bufsz */
423 buf = do_malloc(bufsz);
426 memset(buf, 'a', bufsz);
428 /* preallocate 4M space to file */
429 ret = do_fallocate(fd, 0, filsz, 0);
433 ret = do_pwrite(fd, buf, bufsz, 0);
437 /* One page written just after end of unwritten extent... */
438 ret = do_pwrite(fd, buf, bufsz, filsz);
442 /* update file size */
445 /* offset at the beginning */
446 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
447 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
448 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
449 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
450 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
458 * test file with unwritten extents, only have pagevec worth of dirty pages
459 * in page cache, a hole and then another page.
461 static int test14(int fd, int testnum)
465 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
468 if (!unwritten_extents) {
469 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
473 /* HOLE - unwritten DATA in dirty page */
474 /* Each unit is bufsz */
475 buf = do_malloc(bufsz);
478 memset(buf, 'a', bufsz);
480 /* preallocate 4M space to file */
481 ret = do_fallocate(fd, 0, filsz, 0);
485 ret = do_pwrite(fd, buf, bufsz, 0);
489 ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
493 /* offset at the beginning */
494 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
495 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
496 ret += do_lseek(testnum, 3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
497 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 0, 0);
498 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, 1, 1);
499 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
507 * test file with unwritten extents, only have pagevec worth of dirty pages
510 static int test13(int fd, int testnum)
514 int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
517 if (!unwritten_extents) {
518 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
522 /* HOLE - unwritten DATA in dirty page */
523 /* Each unit is bufsz */
524 buf = do_malloc(bufsz);
527 memset(buf, 'a', bufsz);
529 /* preallocate 4M space to file */
530 ret = do_fallocate(fd, 0, filsz, 0);
534 ret = do_pwrite(fd, buf, bufsz, 0);
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);
550 * Test huge file to check for overflows of block counts due to usage of
553 static int test12(int fd, int testnum)
555 return huge_file_test(fd, testnum,
556 ((long long)alloc_size << 32) + (1 << 20));
560 * Test huge file to check for overflows of block counts due to usage of
563 static int test11(int fd, int testnum)
565 return huge_file_test(fd, testnum,
566 ((long long)alloc_size << 31) + (1 << 20));
569 /* Test an 8G file to check for offset overflows at 1 << 32 */
570 static int test10(int fd, int testnum)
572 return huge_file_test(fd, testnum, 8ULL << 30);
576 * test file with unwritten extents, have both dirty and
577 * writeback pages in page cache.
579 static int test09(int fd, int testnum)
583 int bufsz = alloc_size;
584 int filsz = bufsz * 100 + bufsz;
586 if (!unwritten_extents) {
587 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
592 * HOLE - unwritten DATA in dirty page - HOLE -
593 * unwritten DATA in writeback page
596 /* Each unit is bufsz */
597 buf = do_malloc(bufsz);
600 memset(buf, 'a', bufsz);
602 /* preallocate 8M space to file */
603 ret = do_fallocate(fd, 0, filsz, 0);
607 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
609 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
615 * Sync out dirty pages from bufsz * 100, this will convert
616 * the dirty page to writeback.
618 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
622 /* offset at the beginning */
623 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
624 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
625 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
626 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
633 /* test file with unwritten extent, only have writeback page */
634 static int test08(int fd, int testnum)
638 int bufsz = alloc_size;
639 int filsz = bufsz * 10 + bufsz;
641 if (!unwritten_extents) {
642 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
646 /* HOLE - unwritten DATA in writeback page */
647 /* Each unit is bufsz */
648 buf = do_malloc(bufsz);
651 memset(buf, 'a', bufsz);
653 /* preallocate 4M space to file */
654 ret = do_fallocate(fd, 0, filsz, 0);
658 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
662 /* Sync out all file */
663 ret = do_sync_dirty_pages(fd, 0, 0);
667 /* offset at the beginning */
668 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
669 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
670 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
671 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
679 * test file with unwritten extents, only have dirty pages
682 static int test07(int fd, int testnum)
686 int bufsz = alloc_size;
687 int filsz = bufsz * 10 + bufsz;
689 if (!unwritten_extents) {
690 fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
694 /* HOLE - unwritten DATA in dirty page */
695 /* Each unit is bufsz */
696 buf = do_malloc(bufsz);
699 memset(buf, 'a', bufsz);
701 /* preallocate 4M space to file */
702 ret = do_fallocate(fd, 0, filsz, 0);
706 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
710 /* offset at the beginning */
711 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
712 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
713 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
714 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
721 /* test hole data hole data */
722 static int test06(int fd, int testnum)
726 int bufsz = alloc_size;
727 int filsz = bufsz * 4;
730 /* HOLE - DATA - HOLE - DATA */
731 /* Each unit is bufsz */
733 buf = do_malloc(bufsz);
737 memset(buf, 'a', bufsz);
739 ret = do_pwrite(fd, buf, bufsz, bufsz);
741 do_pwrite(fd, buf, bufsz, bufsz * 3);
745 /* offset at the beginning */
746 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
747 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
748 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
749 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
751 /* offset around first hole-data boundary */
753 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
754 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
755 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
756 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
757 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
758 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
760 /* offset around data-hole boundary */
762 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
763 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
764 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
765 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
766 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
767 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
769 /* offset around second hole-data boundary */
771 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
772 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
773 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
774 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
775 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
776 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
778 /* offset around the end of file */
780 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
781 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
782 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
783 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
784 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
785 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
792 /* test file with data at the beginning and a hole at the end */
793 static int test05(int fd, int testnum)
797 int bufsz = alloc_size;
798 int filsz = bufsz * 4;
800 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
802 buf = do_malloc(bufsz);
805 memset(buf, 'a', bufsz);
807 ret = do_truncate(fd, filsz);
809 ret = do_pwrite(fd, buf, bufsz, 0);
813 /* offset at the beginning */
815 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
816 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
818 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
819 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
821 /* offset around data-hole boundary */
822 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
823 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
825 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
826 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
827 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
828 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
830 /* offset around eof */
831 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
832 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
833 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
834 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
835 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
836 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
841 /* test hole begin and data end */
842 static int test04(int fd, int testnum)
845 char *buf = "ABCDEFGH";
846 int bufsz, holsz, filsz;
849 holsz = alloc_size * 2;
850 filsz = holsz + bufsz;
852 /* |- HOLE -|- HOLE -|- DATA -| */
854 ret = do_pwrite(fd, buf, bufsz, holsz);
858 /* offset at the beginning */
859 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
860 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
861 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
862 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
863 /* offset around hole-data boundary */
864 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
865 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
866 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
867 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
868 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
869 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
871 /* offset around eof */
872 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
873 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
874 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
875 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
876 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
877 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
882 /* test a larger full file */
883 static int test03(int fd, int testnum)
886 int bufsz = alloc_size * 2 + 100;
890 buf = do_malloc(bufsz);
893 memset(buf, 'a', bufsz);
895 ret = do_pwrite(fd, buf, bufsz, 0);
899 /* offset at the beginning */
900 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
901 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
902 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
903 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
905 /* offset around eof */
906 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
907 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
908 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
909 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
910 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
911 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
918 /* test tiny full file */
919 static int test02(int fd, int testnum)
922 char buf[] = "ABCDEFGH";
930 ret = do_pwrite(fd, buf, bufsz, 0);
934 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
935 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
936 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
937 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
938 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
939 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
940 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
941 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
942 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
948 /* test empty file */
949 static int test01(int fd, int testnum)
953 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
954 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
955 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
962 int (*test_func)(int fd, int testnum);
966 struct testrec seek_tests[] = {
967 { 1, test01, "Test empty file" },
968 { 2, test02, "Test a tiny full file" },
969 { 3, test03, "Test a larger full file" },
970 { 4, test04, "Test file hole at beg, data at end" },
971 { 5, test05, "Test file data at beg, hole at end" },
972 { 6, test06, "Test file hole data hole data" },
973 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
974 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
975 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
976 { 10, test10, "Test a huge file for offset overflow" },
977 { 11, test11, "Test a huge file for block number signed" },
978 { 12, test12, "Test a huge file for block number overflow" },
979 { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
980 { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
981 { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
982 { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
983 { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
984 { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
987 static int run_test(struct testrec *tr)
989 int ret = 0, fd = -1;
992 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
994 fd = do_create(filename);
996 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
997 ret = tr->test_func(fd, tr->test_num);
1005 static int test_basic_support(void)
1012 fd = do_create(base_file_path);
1016 get_file_system(fd);
1018 ret = get_io_sizes(fd);
1023 bufsz = alloc_size * 2;
1026 buf = do_malloc(bufsz);
1029 memset(buf, 'a', bufsz);
1031 /* File with 2 allocated blocks.... */
1032 ret = do_pwrite(fd, buf, bufsz, 0);
1036 /* followed by a hole... */
1037 ret = do_truncate(fd, filsz);
1041 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
1042 pos = lseek(fd, 0, SEEK_HOLE);
1044 fprintf(stderr, "Kernel does not support llseek(2) extension "
1045 "SEEK_HOLE. Aborting.\n");
1051 default_behavior = 1;
1052 fprintf(stderr, "File system supports the default behavior.\n");
1056 if (fallocate(fd, 0, 0, alloc_size) == -1) {
1057 if (errno == EOPNOTSUPP)
1058 fprintf(stderr, "File system does not support fallocate.");
1060 fprintf(stderr, "ERROR %d: Failed to preallocate "
1061 "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
1067 pos = lseek(fd, 0, SEEK_DATA);
1069 fprintf(stderr, "File system does not support unwritten extents.\n");
1072 unwritten_extents = 1;
1082 void usage(char *cmd)
1084 fprintf(stdout, "Usage: %s [-t] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
1088 int main(int argc, char **argv)
1093 int check_support = 0;
1094 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
1095 int teststart, testend;
1098 * First twelve tests are used by generic/285. To run these is the
1099 * default. Further tests are run e.g. by generic/436. They are not
1100 * run by default in order to not regress older kernels.
1105 while ((opt = getopt(argc, argv, "ts:e:")) != -1) {
1111 teststart = strtol(optarg, NULL, 10);
1112 if (teststart <= 0 || teststart > numtests) {
1113 fprintf(stdout, "Invalid starting test: %s\n",
1119 testend = strtol(optarg, NULL, 10);
1120 if (testend <= 0 || testend > numtests) {
1121 fprintf(stdout, "Invalid final test: %s\n",
1131 /* should be exactly one arg left, the filename */
1132 if (optind != argc - 1)
1135 if (teststart > testend) {
1136 fprintf(stdout, "Starting test is larger than final test!\n");
1140 base_file_path = (char *)strdup(argv[optind]);
1142 ret = test_basic_support();
1143 if (ret || check_support)
1146 for (i = 0; i < numtests; ++i) {
1147 if (seek_tests[i].test_num >= teststart &&
1148 seek_tests[i].test_num <= testend) {
1149 ret = run_test(&seek_tests[i]);
1156 free(base_file_path);