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 #include <sys/types.h>
37 static blksize_t alloc_size;
38 int default_behavior = 0;
41 static void get_file_system(int fd)
45 if (!fstatfs(fd, &buf))
46 fprintf(stdout, "File system magic#: 0x%lx\n", buf.f_type);
49 static int get_io_sizes(int fd)
54 ret = fstat(fd, &buf);
56 fprintf(stderr, " ERROR %d: Failed to find io blocksize\n",
59 /* st_blksize is typically also the allocation size */
60 alloc_size = buf.st_blksize;
61 fprintf(stdout, "Allocation size: %ld\n", alloc_size);
66 #define do_free(x) do { if(x) free(x); } while(0);
68 static void *do_malloc(size_t size)
74 fprintf(stderr, " ERROR: Unable to allocate %ld bytes\n",
80 static int do_truncate(int fd, off_t length)
84 ret = ftruncate(fd, length);
86 fprintf(stderr, " ERROR %d: Failed to extend file "
87 "to %ld bytes\n", errno, (long)length);
91 static int do_fallocate(int fd, off_t offset, off_t length, int mode)
95 ret = fallocate(fd, mode, offset, length);
97 fprintf(stderr, " ERROR %d: Failed to preallocate "
98 "space to %ld bytes\n", errno, (long) length);
104 * Synchnorize all dirty pages in the file range starting from
105 * offset to nbytes length.
107 static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
111 ret = sync_file_range(fd, offset, nbytes, SYNC_FILE_RANGE_WRITE);
113 fprintf(stderr, " ERROR %d: Failed to sync out dirty "
119 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
121 ssize_t ret, written = 0;
123 while (count > written) {
124 ret = pwrite(fd, buf + written, count - written, offset + written);
126 fprintf(stderr, " ERROR %d: Failed to write %ld "
127 "bytes\n", errno, (long)count);
136 #define do_close(x) do { if ((x) > -1) close(x); } while(0);
138 static int do_create(const char *filename)
142 fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0644);
144 fprintf(stderr, " ERROR %d: Failed to create file '%s'\n",
150 static int do_lseek(int testnum, int subtest, int fd, int filsz, int origin,
151 off_t set, off_t exp)
156 assert(!(origin != SEEK_HOLE && origin != SEEK_DATA));
159 * The file pointer can be set to different values depending
160 * on the implementation. For SEEK_HOLE, EOF could be a valid
161 * value. For SEEK_DATA, supplied offset could be the valid
165 if (origin == SEEK_HOLE && exp2 != -1)
167 if (origin == SEEK_DATA && default_behavior && set < filsz)
170 pos = lseek(fd, set, origin);
172 if (pos == -1 && exp == -1) {
173 x = fprintf(stdout, "%02d.%02d %s expected -1 with errno %d, got %d. ",
175 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
177 ret = !(errno == ENXIO);
180 x = fprintf(stdout, "%02d.%02d %s expected %ld or %ld, got %ld. ",
182 (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
183 (long)exp, (long)exp2, (long)pos);
184 ret = !(pos == exp || pos == exp2);
187 fprintf(stdout, "%*s\n", (70 - x), ret ? "FAIL" : "succ");
193 * test file with unwritten extents, have both dirty and
194 * writeback pages in page cache.
196 static int test09(int fd, int testnum)
200 int bufsz = alloc_size;
204 * HOLE - unwritten DATA in dirty page - HOLE -
205 * unwritten DATA in writeback page
208 /* Each unit is bufsz */
209 buf = do_malloc(bufsz);
212 memset(buf, 'a', bufsz);
214 /* preallocate 8M space to file */
215 ret = do_fallocate(fd, 0, filsz, 0);
219 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
221 ret = do_pwrite(fd, buf, bufsz, bufsz * 100);
227 * Sync out dirty pages from bufsz * 100, this will convert
228 * the dirty page to writeback.
230 ret = do_sync_dirty_pages(fd, bufsz * 100, 0);
234 /* offset at the beginning */
235 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
236 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
237 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
238 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
245 /* test file with unwritten extent, only have writeback page */
246 static int test08(int fd, int testnum)
250 int bufsz = alloc_size;
253 /* HOLE - unwritten DATA in writeback page */
254 /* Each unit is bufsz */
255 buf = do_malloc(bufsz);
258 memset(buf, 'a', bufsz);
260 /* preallocate 4M space to file */
261 ret = do_fallocate(fd, 0, filsz, 0);
265 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
269 /* Sync out all file */
270 ret = do_sync_dirty_pages(fd, 0, 0);
274 /* offset at the beginning */
275 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
276 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
277 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
278 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
286 * test file with unwritten extents, only have dirty pages
289 static int test07(int fd, int testnum)
293 int bufsz = alloc_size;
296 /* HOLE - unwritten DATA in dirty page */
297 /* Each unit is bufsz */
298 buf = do_malloc(bufsz);
301 memset(buf, 'a', bufsz);
303 /* preallocate 4M space to file */
304 ret = do_fallocate(fd, 0, filsz, 0);
308 ret = do_pwrite(fd, buf, bufsz, bufsz * 10);
312 /* offset at the beginning */
313 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
314 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
315 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz * 10);
316 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz * 10);
323 /* test hole data hole data */
324 static int test06(int fd, int testnum)
328 int bufsz = alloc_size;
329 int filsz = bufsz * 4;
332 /* HOLE - DATA - HOLE - DATA */
333 /* Each unit is bufsz */
335 buf = do_malloc(bufsz);
339 memset(buf, 'a', bufsz);
341 ret = do_pwrite(fd, buf, bufsz, bufsz);
343 do_pwrite(fd, buf, bufsz, bufsz * 3);
347 /* offset at the beginning */
348 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
349 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
350 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, bufsz);
351 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, bufsz);
353 /* offset around first hole-data boundary */
355 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, off - 1, off - 1);
356 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, off - 1, off);
357 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, off, bufsz * 2);
358 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, off, off);
359 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, off + 1, bufsz * 2);
360 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, off + 1, off + 1);
362 /* offset around data-hole boundary */
364 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, off - 1, off);
365 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, off - 1, off - 1);
366 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, off, off);
367 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, off, bufsz * 3);
368 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, off + 1, off + 1);
369 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, off + 1, bufsz * 3);
371 /* offset around second hole-data boundary */
373 ret += do_lseek(testnum, 17, fd, filsz, SEEK_HOLE, off - 1, off - 1);
374 ret += do_lseek(testnum, 18, fd, filsz, SEEK_DATA, off - 1, off);
375 ret += do_lseek(testnum, 19, fd, filsz, SEEK_HOLE, off, filsz);
376 ret += do_lseek(testnum, 20, fd, filsz, SEEK_DATA, off, off);
377 ret += do_lseek(testnum, 21, fd, filsz, SEEK_HOLE, off + 1, filsz);
378 ret += do_lseek(testnum, 22, fd, filsz, SEEK_DATA, off + 1, off + 1);
380 /* offset around the end of file */
382 ret += do_lseek(testnum, 23, fd, filsz, SEEK_HOLE, off - 1, filsz);
383 ret += do_lseek(testnum, 24, fd, filsz, SEEK_DATA, off - 1, filsz - 1);
384 ret += do_lseek(testnum, 25, fd, filsz, SEEK_HOLE, off, -1);
385 ret += do_lseek(testnum, 26, fd, filsz, SEEK_DATA, off, -1);
386 ret += do_lseek(testnum, 27, fd, filsz, SEEK_HOLE, off + 1, -1);
387 ret += do_lseek(testnum, 28, fd, filsz, SEEK_DATA, off + 1, -1);
394 /* test file with data at the beginning and a hole at the end */
395 static int test05(int fd, int testnum)
399 int bufsz = alloc_size;
400 int filsz = bufsz * 4;
402 /* |- DATA -|- HOLE -|- HOLE -|- HOLE -| */
404 buf = do_malloc(bufsz);
407 memset(buf, 'a', bufsz);
409 ret = do_truncate(fd, filsz);
411 ret = do_pwrite(fd, buf, bufsz, 0);
415 /* offset at the beginning */
417 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
418 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
420 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
421 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
423 /* offset around data-hole boundary */
424 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
425 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
427 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, bufsz);
428 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
429 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
430 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
432 /* offset around eof */
433 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz - 1);
434 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, -1);
435 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
436 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
437 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
438 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
443 /* test hole begin and data end */
444 static int test04(int fd, int testnum)
447 char *buf = "ABCDEFGH";
448 int bufsz, holsz, filsz;
451 holsz = alloc_size * 2;
452 filsz = holsz + bufsz;
454 /* |- HOLE -|- HOLE -|- DATA -| */
456 ret = do_pwrite(fd, buf, bufsz, holsz);
460 /* offset at the beginning */
461 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, 0);
462 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, 1);
463 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, holsz);
464 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, holsz);
465 /* offset around hole-data boundary */
466 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, holsz - 1, holsz - 1);
467 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, holsz - 1, holsz);
468 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, holsz, filsz);
469 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, holsz, holsz);
470 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, holsz + 1, filsz);
471 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, holsz + 1, holsz + 1);
473 /* offset around eof */
474 ret += do_lseek(testnum, 11, fd, filsz, SEEK_HOLE, filsz - 1, filsz);
475 ret += do_lseek(testnum, 12, fd, filsz, SEEK_DATA, filsz - 1, filsz - 1);
476 ret += do_lseek(testnum, 13, fd, filsz, SEEK_HOLE, filsz, -1);
477 ret += do_lseek(testnum, 14, fd, filsz, SEEK_DATA, filsz, -1);
478 ret += do_lseek(testnum, 15, fd, filsz, SEEK_HOLE, filsz + 1, -1);
479 ret += do_lseek(testnum, 16, fd, filsz, SEEK_DATA, filsz + 1, -1);
484 /* test a larger full file */
485 static int test03(int fd, int testnum)
488 int bufsz = alloc_size * 2 + 100;
492 buf = do_malloc(bufsz);
495 memset(buf, 'a', bufsz);
497 ret = do_pwrite(fd, buf, bufsz, 0);
501 /* offset at the beginning */
502 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, bufsz);
503 ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 1, bufsz);
504 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 0, 0);
505 ret += do_lseek(testnum, 4, fd, filsz, SEEK_DATA, 1, 1);
507 /* offset around eof */
508 ret += do_lseek(testnum, 5, fd, filsz, SEEK_HOLE, bufsz - 1, bufsz);
509 ret += do_lseek(testnum, 6, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
510 ret += do_lseek(testnum, 7, fd, filsz, SEEK_HOLE, bufsz, -1);
511 ret += do_lseek(testnum, 8, fd, filsz, SEEK_DATA, bufsz, -1);
512 ret += do_lseek(testnum, 9, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
513 ret += do_lseek(testnum, 10, fd, filsz, SEEK_DATA, bufsz + 1, -1);
520 /* test tiny full file */
521 static int test02(int fd, int testnum)
524 char buf[] = "ABCDEFGH";
532 ret = do_pwrite(fd, buf, bufsz, 0);
536 ret += do_lseek(testnum, 1, fd, filsz, SEEK_HOLE, 0, filsz);
537 ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 0, 0);
538 ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, 1, 1);
539 ret += do_lseek(testnum, 4, fd, filsz, SEEK_HOLE, bufsz - 1, filsz);
540 ret += do_lseek(testnum, 5, fd, filsz, SEEK_DATA, bufsz - 1, bufsz - 1);
541 ret += do_lseek(testnum, 6, fd, filsz, SEEK_HOLE, bufsz, -1);
542 ret += do_lseek(testnum, 7, fd, filsz, SEEK_DATA, bufsz, -1);
543 ret += do_lseek(testnum, 8, fd, filsz, SEEK_HOLE, bufsz + 1, -1);
544 ret += do_lseek(testnum, 9, fd, filsz, SEEK_DATA, bufsz + 1, -1);
550 /* test empty file */
551 static int test01(int fd, int testnum)
555 ret += do_lseek(testnum, 1, fd, 0, SEEK_DATA, 0, -1);
556 ret += do_lseek(testnum, 2, fd, 0, SEEK_HOLE, 0, -1);
557 ret += do_lseek(testnum, 3, fd, 0, SEEK_HOLE, 1, -1);
564 int (*test_func)(int fd, int testnum);
568 struct testrec seek_tests[] = {
569 { 1, test01, "Test empty file" },
570 { 2, test02, "Test a tiny full file" },
571 { 3, test03, "Test a larger full file" },
572 { 4, test04, "Test file hole at beg, data at end" },
573 { 5, test05, "Test file data at beg, hole at end" },
574 { 6, test06, "Test file hole data hole data" },
575 { 7, test07, "Test file with unwritten extents, only have dirty pages" },
576 { 8, test08, "Test file with unwritten extents, only have unwritten pages" },
577 { 9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
580 static int run_test(struct testrec *tr)
582 int ret = 0, fd = -1;
585 snprintf(filename, sizeof(filename), "%s%02d", base_file_path, tr->test_num);
587 fd = do_create(filename);
589 printf("%02d. %-50s\n", tr->test_num, tr->test_desc);
590 ret = tr->test_func(fd, tr->test_num);
598 static int test_basic_support(void)
605 fd = do_create(base_file_path);
611 ret = get_io_sizes(fd);
615 bufsz = alloc_size * 2;
618 buf = do_malloc(bufsz);
621 memset(buf, 'a', bufsz);
623 /* File with 2 allocated blocks.... */
624 ret = do_pwrite(fd, buf, bufsz, 0);
628 /* followed by a hole... */
629 ret = do_truncate(fd, filsz);
633 /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
634 pos = lseek(fd, 0, SEEK_DATA);
636 pos = lseek(fd, 0, SEEK_HOLE);
638 fprintf(stderr, "Kernel does not support llseek(2) extensions "
639 "SEEK_HOLE and/or SEEK_DATA. Aborting.\n");
645 default_behavior = 1;
646 fprintf(stderr, "File system supports the default behavior.\n");
657 int main(int argc, char **argv)
661 int numtests = sizeof(seek_tests) / sizeof(struct testrec);
664 fprintf(stdout, "Usage: %s base_file_path\n", argv[0]);
668 base_file_path = (char *)strdup(argv[1]);
670 ret = test_basic_support();
674 for (i = 0; i < numtests; ++i) {
677 run_test(&seek_tests[i]);
681 free(base_file_path);