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, off_t 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 - bufsz;
209 buf = do_malloc(bufsz);
212 memset(buf, 'a', bufsz);
214 /* |- DATA -|- HUGE HOLE -|- DATA -| */
215 ret = do_pwrite(fd, buf, bufsz, 0);
218 ret = do_pwrite(fd, buf, bufsz, off);
221 * Report success. Filesystem just cannot handle so large
222 * offsets and correctly reports it.
224 if (errno == EFBIG) {
225 fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
231 /* offset at the beginning */
232 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
233 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
234 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
235 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
237 /* offset around eof */
238 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off, off + bufsz);
239 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off, off);
240 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, off + 1, off + 1);
241 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off - bufsz, off);
249 * Test huge file to check for overflows of block counts due to usage of
252 static int test12(int fd, int testnum)
254 return huge_file_test(fd, testnum,
255 ((long long)alloc_size << 32) + (1 << 20));
259 * Test huge file to check for overflows of block counts due to usage of
262 static int test11(int fd, int testnum)
264 return huge_file_test(fd, testnum,
265 ((long long)alloc_size << 31) + (1 << 20));
268 /* Test an 8G file to check for offset overflows at 1 << 32 */
269 static int test10(int fd, int testnum)
271 return huge_file_test(fd, testnum, 8ULL << 30);
275 * test file with unwritten extents, have both dirty and
276 * writeback pages in page cache.
278 static int test09(int fd, int testnum)
282 int bufsz = alloc_size;
286 * HOLE - unwritten DATA in dirty page - HOLE -
287 * unwritten DATA in writeback page
290 /* Each unit is bufsz */
291 buf = do_malloc(bufsz);
294 memset(buf, 'a', bufsz);
296 /* preallocate 8M space to file */
297 ret = do_fallocate(fd, 0, filsz, 0);
299 /* Report success if fs doesn't support fallocate */
300 if (errno == EOPNOTSUPP) {
301 fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
307 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
309 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
315 * Sync out dirty pages from bufsz * 100, this will convert
316 * the dirty page to writeback.
318 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
322 /* offset at the beginning */
323 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
324 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
325 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
326 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
333 /* test file with unwritten extent, only have writeback page */
334 static int test08(int fd, int testnum)
338 int bufsz = alloc_size;
341 /* HOLE - unwritten DATA in writeback page */
342 /* Each unit is bufsz */
343 buf = do_malloc(bufsz);
346 memset(buf, 'a', bufsz);
348 /* preallocate 4M space to file */
349 ret = do_fallocate(fd, 0, filsz, 0);
351 /* Report success if fs doesn't support fallocate */
352 if (errno == EOPNOTSUPP) {
353 fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
359 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
363 /* Sync out all file */
364 ret = do_sync_dirty_pages(fd, 0, 0);
368 /* offset at the beginning */
369 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
370 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
371 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
372 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
380 * test file with unwritten extents, only have dirty pages
383 static int test07(int fd, int testnum)
387 int bufsz = alloc_size;
390 /* HOLE - unwritten DATA in dirty page */
391 /* Each unit is bufsz */
392 buf = do_malloc(bufsz);
395 memset(buf, 'a', bufsz);
397 /* preallocate 4M space to file */
398 ret = do_fallocate(fd, 0, filsz, 0);
400 /* Report success if fs doesn't support fallocate */
401 if (errno == EOPNOTSUPP) {
402 fprintf(stdout, "Test skipped as fs doesn't support fallocate.\n");
408 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
412 /* offset at the beginning */
413 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
414 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
415 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
416 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
423 /* test hole data hole data */
424 static int test06(int fd, int testnum)
428 int bufsz = alloc_size;
429 int filsz = bufsz * 4;
432 /* HOLE - DATA - HOLE - DATA */
433 /* Each unit is bufsz */
435 buf = do_malloc(bufsz);
439 memset(buf, 'a', bufsz);
441 ret = do_pwrite(fd, buf, bufsz, bufsz);
443 do_pwrite(fd, buf, bufsz, bufsz * 3);
447 /* offset at the beginning */
448 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
449 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
450 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
451 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
453 /* offset around first hole-data boundary */
455 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
456 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
457 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
458 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
459 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
460 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
462 /* offset around data-hole boundary */
464 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
465 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
466 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
467 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
468 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
469 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
471 /* offset around second hole-data boundary */
473 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
474 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
475 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
476 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
477 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
478 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
480 /* offset around the end of file */
482 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
483 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
484 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
485 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
486 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
487 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
494 /* test file with data at the beginning and a hole at the end */
495 static int test05(int fd, int testnum)
499 int bufsz = alloc_size;
500 int filsz = bufsz * 4;
502 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
504 buf = do_malloc(bufsz);
507 memset(buf, 'a', bufsz);
509 ret = do_truncate(fd, filsz);
511 ret = do_pwrite(fd, buf, bufsz, 0);
515 /* offset at the beginning */
517 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
518 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
520 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
521 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
523 /* offset around data-hole boundary */
524 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
525 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
527 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
528 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
529 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
530 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
532 /* offset around eof */
533 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
534 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
535 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
536 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
537 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
538 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
543 /* test hole begin and data end */
544 static int test04(int fd, int testnum)
547 char *buf = "ABCDEFGH";
548 int bufsz, holsz, filsz;
551 holsz = alloc_size * 2;
552 filsz = holsz + bufsz;
554 /* |- HOLE -|- HOLE -|- DATA -| */
556 ret = do_pwrite(fd, buf, bufsz, holsz);
560 /* offset at the beginning */
561 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
562 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
563 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
564 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
565 /* offset around hole-data boundary */
566 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
567 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
568 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
569 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
570 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
571 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
573 /* offset around eof */
574 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
575 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
576 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
577 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
578 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
579 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
584 /* test a larger full file */
585 static int test03(int fd, int testnum)
588 int bufsz = alloc_size * 2 + 100;
592 buf = do_malloc(bufsz);
595 memset(buf, 'a', bufsz);
597 ret = do_pwrite(fd, buf, bufsz, 0);
601 /* offset at the beginning */
602 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
603 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
604 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
605 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
607 /* offset around eof */
608 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
609 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
610 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
611 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
612 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
613 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
620 /* test tiny full file */
621 static int test02(int fd, int testnum)
624 char buf[] = "ABCDEFGH";
632 ret = do_pwrite(fd, buf, bufsz, 0);
636 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
637 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
638 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
639 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
640 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
641 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
642 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
643 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
644 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
650 /* test empty file */
651 static int test01(int fd, int testnum)
655 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
656 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
657 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
664 int (*test_func)(int fd, int testnum);
668 struct testrec seek_tests[] = {
669 { 1, test01, "Test empty file" },
670 { 2, test02, "Test a tiny full file" },
671 { 3, test03, "Test a larger full file" },
672 { 4, test04, "Test file hole at beg, data at end" },
673 { 5, test05, "Test file data at beg, hole at end" },
674 { 6, test06, "Test file hole data hole data" },
675 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
676 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
677 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
678 { 10, test10, "Test a huge file for offset overflow" },
679 { 11, test11, "Test a huge file for block number signed" },
680 { 12, test12, "Test a huge file for block number overflow" },
683 static int run_test(struct testrec *tr)
685 int ret = 0, fd = -1;
688 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
690 fd = do_create(filename);
692 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
693 ret = tr->test_func(fd, tr->test_num);
701 static int test_basic_support(void)
708 fd = do_create(base_file_path);
714 ret = get_io_sizes(fd);
718 bufsz = alloc_size * 2;
721 buf = do_malloc(bufsz);
724 memset(buf, 'a', bufsz);
726 /* File with 2 allocated blocks.... */
727 ret = do_pwrite(fd, buf, bufsz, 0);
731 /* followed by a hole... */
732 ret = do_truncate(fd, filsz);
736 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
737 pos = lseek(fd, 0, SEEK_DATA);
739 pos = lseek(fd, 0, SEEK_HOLE);
741 fprintf(stderr, "Kernel does not support llseek(2) extensions "
742 "SEEK_HOLE and/or SEEK_DATA. Aborting.\n");
748 default_behavior = 1;
749 fprintf(stderr, "File system supports the default behavior.\n");
760 void usage(char *cmd)
762 fprintf(stdout, "Usage: %s [-t] base_file_path\n", cmd);
766 int main(int argc, char **argv)
771 int check_support = 0;
772 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
774 while ((opt = getopt(argc, argv, "t")) != -1) {
784 /* should be exactly one arg left, the filename */
785 if (optind != argc - 1)
788 base_file_path = (char *)strdup(argv[optind]);
790 ret = test_basic_support();
791 if (ret || check_support)
794 for (i = 0; i < numtests; ++i) {
795 ret = run_test(&seek_tests[i]);
801 free(base_file_path);