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;
42 static void get_file_system(int fd)
46 if (!fstatfs(fd, &buf)) {
47 fprintf(stdout, "File system magic#: 0x%lx\n",
48 (unsigned long int)buf.f_type);
52 static int get_io_sizes(int fd)
57 ret = fstat(fd, &buf);
59 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
62 /* st_blksize is typically also the allocation size */
63 alloc_size = buf.st_blksize;
64 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
69 #define do_free(x) do { if(x) free(x); } while(0);
71 static void *do_malloc(size_t size)
77 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
83 static int do_truncate(int fd, off_t length)
87 ret = ftruncate(fd, length);
89 fprintf(stderr, " ERROR %d: Failed to extend file "
90 "to %ld bytes\n", errno, (long)length);
94 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
98 ret = fallocate(fd, mode, offset, length);
100 /* Don't warn about a filesystem w/o fallocate support */
101 if (errno == EOPNOTSUPP)
103 fprintf(stderr, " ERROR %d: Failed to preallocate "
104 "space to %ld bytes\n", errno, (long) length);
111 * Synchnorize all dirty pages in the file range starting from
112 * offset to nbytes length.
114 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
118 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
120 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
126 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
128 ssize_t ret, written = 0;
130 while (count > written) {
131 ret = pwrite(fd, buf + written, count - written, offset + written);
133 /* Don't warn about too large file. It's fs dependent. */
136 fprintf(stderr, " ERROR %d: Failed to write %ld "
137 "bytes\n", errno, (long)count);
146 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
148 static int do_create(const char *filename)
152 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
154 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
160 static int do_lseek(int testnum, int subtest, int fd, int filsz, int origin,
161 off_t set, off_t exp)
166 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
169 * The file pointer can be set to different values depending
170 * on the implementation. For SEEK_HOLE, EOF could be a valid
171 * value. For SEEK_DATA, supplied offset could be the valid
175 if (origin == SEEK_HOLE && exp2 != -1)
177 if (origin == SEEK_DATA && default_behavior && set < filsz)
180 pos = lseek(fd, set, origin);
182 if (pos == -1 && exp == -1) {
183 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
185 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
187 ret = !(errno == ENXIO);
190 x = fprintf(stdout, "%02d.%02d %s expected %ld or %ld, got %ld. ",
192 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
193 (long)exp, (long)exp2, (long)pos);
194 ret = !(pos == exp || pos == exp2);
197 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
202 static int huge_file_test(int fd, int testnum, off_t filsz)
205 int bufsz = alloc_size * 16; /* XFS seems to round allocated size */
206 off_t off = filsz - 2*bufsz;
209 buf = do_malloc(bufsz);
212 memset(buf, 'a', bufsz);
214 ret = do_pwrite(fd, buf, bufsz, 0);
217 ret = do_pwrite(fd, buf, bufsz, off);
220 * Report success. Filesystem just cannot handle so large
221 * offsets and correctly reports it.
223 if (errno == EFBIG) {
224 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
230 /* offset at the beginning */
231 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
232 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
233 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
234 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
236 /* offset around eof */
237 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off, off + bufsz);
238 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off, off);
239 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, off + 1, off + 1);
240 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off - bufsz, off);
248 * Test huge file to check for overflows of block counts due to usage of
251 static int test12(int fd, int testnum)
253 return huge_file_test(fd, testnum,
254 ((long long)alloc_size << 32) + (1 << 20));
258 * Test huge file to check for overflows of block counts due to usage of
261 static int test11(int fd, int testnum)
263 return huge_file_test(fd, testnum,
264 ((long long)alloc_size << 31) + (1 << 20));
267 /* Test an 8G file to check for offset overflows at 1 << 32 */
268 static int test10(int fd, int testnum)
270 return huge_file_test(fd, testnum, 8ULL << 30);
274 * test file with unwritten extents, have both dirty and
275 * writeback pages in page cache.
277 static int test09(int fd, int testnum)
281 int bufsz = alloc_size;
285 * HOLE - unwritten DATA in dirty page - HOLE -
286 * unwritten DATA in writeback page
289 /* Each unit is bufsz */
290 buf = do_malloc(bufsz);
293 memset(buf, 'a', bufsz);
295 /* preallocate 8M space to file */
296 ret = do_fallocate(fd, 0, filsz, 0);
298 /* Report success if fs doesn't support fallocate */
299 if (errno == EOPNOTSUPP) {
300 fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
306 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
308 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
314 * Sync out dirty pages from bufsz * 100, this will convert
315 * the dirty page to writeback.
317 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
321 /* offset at the beginning */
322 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
323 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
324 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
325 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
332 /* test file with unwritten extent, only have writeback page */
333 static int test08(int fd, int testnum)
337 int bufsz = alloc_size;
340 /* HOLE - unwritten DATA in writeback page */
341 /* Each unit is bufsz */
342 buf = do_malloc(bufsz);
345 memset(buf, 'a', bufsz);
347 /* preallocate 4M space to file */
348 ret = do_fallocate(fd, 0, filsz, 0);
350 /* Report success if fs doesn't support fallocate */
351 if (errno == EOPNOTSUPP) {
352 fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
358 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
362 /* Sync out all file */
363 ret = do_sync_dirty_pages(fd, 0, 0);
367 /* offset at the beginning */
368 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
369 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
370 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
371 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
379 * test file with unwritten extents, only have dirty pages
382 static int test07(int fd, int testnum)
386 int bufsz = alloc_size;
389 /* HOLE - unwritten DATA in dirty page */
390 /* Each unit is bufsz */
391 buf = do_malloc(bufsz);
394 memset(buf, 'a', bufsz);
396 /* preallocate 4M space to file */
397 ret = do_fallocate(fd, 0, filsz, 0);
399 /* Report success if fs doesn't support fallocate */
400 if (errno == EOPNOTSUPP) {
401 fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
407 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
411 /* offset at the beginning */
412 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
413 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
414 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
415 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
422 /* test hole data hole data */
423 static int test06(int fd, int testnum)
427 int bufsz = alloc_size;
428 int filsz = bufsz * 4;
431 /* HOLE - DATA - HOLE - DATA */
432 /* Each unit is bufsz */
434 buf = do_malloc(bufsz);
438 memset(buf, 'a', bufsz);
440 ret = do_pwrite(fd, buf, bufsz, bufsz);
442 do_pwrite(fd, buf, bufsz, bufsz * 3);
446 /* offset at the beginning */
447 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
448 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
449 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
450 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
452 /* offset around first hole-data boundary */
454 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
455 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
456 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
457 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
458 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
459 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
461 /* offset around data-hole boundary */
463 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
464 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
465 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
466 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
467 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
468 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
470 /* offset around second hole-data boundary */
472 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
473 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
474 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
475 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
476 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
477 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
479 /* offset around the end of file */
481 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
482 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
483 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
484 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
485 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
486 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
493 /* test file with data at the beginning and a hole at the end */
494 static int test05(int fd, int testnum)
498 int bufsz = alloc_size;
499 int filsz = bufsz * 4;
501 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
503 buf = do_malloc(bufsz);
506 memset(buf, 'a', bufsz);
508 ret = do_truncate(fd, filsz);
510 ret = do_pwrite(fd, buf, bufsz, 0);
514 /* offset at the beginning */
516 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
517 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
519 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
520 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
522 /* offset around data-hole boundary */
523 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
524 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
526 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
527 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
528 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
529 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
531 /* offset around eof */
532 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
533 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
534 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
535 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
536 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
537 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
542 /* test hole begin and data end */
543 static int test04(int fd, int testnum)
546 char *buf = "ABCDEFGH";
547 int bufsz, holsz, filsz;
550 holsz = alloc_size * 2;
551 filsz = holsz + bufsz;
553 /* |- HOLE -|- HOLE -|- DATA -| */
555 ret = do_pwrite(fd, buf, bufsz, holsz);
559 /* offset at the beginning */
560 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
561 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
562 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
563 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
564 /* offset around hole-data boundary */
565 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
566 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
567 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
568 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
569 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
570 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
572 /* offset around eof */
573 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
574 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
575 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
576 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
577 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
578 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
583 /* test a larger full file */
584 static int test03(int fd, int testnum)
587 int bufsz = alloc_size * 2 + 100;
591 buf = do_malloc(bufsz);
594 memset(buf, 'a', bufsz);
596 ret = do_pwrite(fd, buf, bufsz, 0);
600 /* offset at the beginning */
601 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
602 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
603 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
604 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
606 /* offset around eof */
607 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
608 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
609 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
610 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
611 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
612 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
619 /* test tiny full file */
620 static int test02(int fd, int testnum)
623 char buf[] = "ABCDEFGH";
631 ret = do_pwrite(fd, buf, bufsz, 0);
635 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
636 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
637 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
638 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
639 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
640 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
641 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
642 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
643 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
649 /* test empty file */
650 static int test01(int fd, int testnum)
654 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
655 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
656 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
663 int (*test_func)(int fd, int testnum);
667 struct testrec seek_tests[] = {
668 { 1, test01, "Test empty file" },
669 { 2, test02, "Test a tiny full file" },
670 { 3, test03, "Test a larger full file" },
671 { 4, test04, "Test file hole at beg, data at end" },
672 { 5, test05, "Test file data at beg, hole at end" },
673 { 6, test06, "Test file hole data hole data" },
674 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
675 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
676 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
677 { 10, test10, "Test a huge file for offset overflow" },
678 { 11, test11, "Test a huge file for block number signed" },
679 { 12, test12, "Test a huge file for block number overflow" },
682 static int run_test(struct testrec *tr)
684 int ret = 0, fd = -1;
687 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
689 fd = do_create(filename);
691 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
692 ret = tr->test_func(fd, tr->test_num);
700 static int test_basic_support(void)
707 fd = do_create(base_file_path);
713 ret = get_io_sizes(fd);
717 bufsz = alloc_size * 2;
720 buf = do_malloc(bufsz);
723 memset(buf, 'a', bufsz);
725 /* File with 2 allocated blocks.... */
726 ret = do_pwrite(fd, buf, bufsz, 0);
730 /* followed by a hole... */
731 ret = do_truncate(fd, filsz);
735 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
736 pos = lseek(fd, 0, SEEK_DATA);
738 pos = lseek(fd, 0, SEEK_HOLE);
740 fprintf(stderr, "Kernel does not support llseek(2) extensions "
741 "SEEK_HOLE and/or SEEK_DATA. Aborting.\n");
747 default_behavior = 1;
748 fprintf(stderr, "File system supports the default behavior.\n");
759 void usage(char *cmd)
761 fprintf(stdout, "Usage: %s [-t] base_file_path\n", cmd);
765 int main(int argc, char **argv)
770 int check_support = 0;
771 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
773 while ((opt = getopt(argc, argv, "t")) != -1) {
783 /* should be exactly one arg left, the filename */
784 if (optind != argc - 1)
787 base_file_path = (char *)strdup(argv[optind]);
789 ret = test_basic_support();
790 if (ret || check_support)
793 for (i = 0; i < numtests; ++i) {
794 ret = run_test(&seek_tests[i]);
800 free(base_file_path);