generic: Test that SEEK_HOLE can find a punched hole
[xfstests-dev.git] / src / seek_sanity_test.c
index 4275a846737b1b27064a2dcaa940730d01c969c0..30e996e2e6a7af63884450757915c8ed24674d4b 100644 (file)
@@ -1,23 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (C) 2011 Oracle.  All rights reserved.
  * Copyright (C) 2011 Red Hat.  All rights reserved.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public
- * License v2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 021110-1307, USA.
  */
 
 #define _XOPEN_SOURCE 500
+#define _FILE_OFFSET_BITS 64
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/vfs.h>
@@ -28,6 +16,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <assert.h>
+#include "global.h"
 
 #ifndef SEEK_DATA
 #define SEEK_DATA      3
 #endif
 
 static blksize_t alloc_size;
+int allow_default_behavior = 1;
 int default_behavior = 0;
+int unwritten_extents = 0;
+int punch_hole = 0;
 char *base_file_path;
 
 static void get_file_system(int fd)
@@ -50,19 +42,51 @@ static void get_file_system(int fd)
 
 static int get_io_sizes(int fd)
 {
-       struct stat buf;
-       int ret;
-
-       ret = fstat(fd, &buf);
-       if (ret)
-               fprintf(stderr, "  ERROR %d: Failed to find io blocksize\n",
-                       errno);
+       off_t pos = 0, offset = 1;
+       struct stat buf;
+       int shift, ret;
+
+       ret = fstat(fd, &buf);
+       if (ret) {
+               fprintf(stderr, "  ERROR %d: Failed to find io blocksize\n",
+                       errno);
+               return ret;
+       }
 
-       /* st_blksize is typically also the allocation size */
-       alloc_size = buf.st_blksize;
-       fprintf(stdout, "Allocation size: %ld\n", alloc_size);
+       /* st_blksize is typically also the allocation size */
+       alloc_size = buf.st_blksize;
+
+       /* try to discover the actual alloc size */
+       while (pos == 0 && offset < alloc_size) {
+               offset <<= 1;
+               ftruncate(fd, 0);
+               pwrite(fd, "a", 1, offset);
+               pos = lseek(fd, 0, SEEK_DATA);
+               if (pos == -1)
+                       goto fail;
+       }
 
-       return ret;
+       /* bisect */
+       shift = offset >> 2;
+       while (shift && offset < alloc_size) {
+               ftruncate(fd, 0);
+               pwrite(fd, "a", 1, offset);
+               pos = lseek(fd, 0, SEEK_DATA);
+               if (pos == -1)
+                       goto fail;
+               offset += pos ? -shift : shift;
+               shift >>= 1;
+       }
+       if (!shift)
+               offset += pos ? 0 : 1;
+       alloc_size = offset;
+       fprintf(stdout, "Allocation size: %ld\n", alloc_size);
+       return 0;
+
+fail:
+       fprintf(stderr, "Kernel does not support llseek(2) extension "
+               "SEEK_DATA. Aborting.\n");
+       return -1;
 }
 
 #define do_free(x)     do { if(x) free(x); } while(0);
@@ -96,8 +120,9 @@ static int do_fallocate(int fd, off_t offset, off_t length, int mode)
 
        ret = fallocate(fd, mode, offset, length);
        if (ret)
-               fprintf(stderr, "  ERROR %d: Failed to preallocate "
-                       "space to %ld bytes\n", errno, (long) length);
+               fprintf(stderr, "  ERROR %d: Failed to %s of %ld bytes\n",
+                       errno, (mode & FALLOC_FL_PUNCH_HOLE) ? "punch hole" :
+                       "preallocate space", (long) length);
 
        return ret;
 }
@@ -120,19 +145,22 @@ static int do_sync_dirty_pages(int fd, off64_t offset, off64_t nbytes)
 
 static ssize_t do_pwrite(int fd, const void *buf, size_t count, off_t offset)
 {
-       ssize_t ret, written = 0;
-
-       while (count > written) {
-               ret = pwrite(fd, buf + written, count - written, offset + written);
-               if (ret < 0) {
-                       fprintf(stderr, "  ERROR %d: Failed to write %ld "
-                               "bytes\n", errno, (long)count);
-                       return ret;
-               }
-               written += ret;
-       }
+       ssize_t ret, written = 0;
+
+       while (count > written) {
+               ret = pwrite(fd, buf + written, count - written, offset + written);
+               if (ret < 0) {
+                       /* Don't warn about too large file. It's fs dependent. */
+                       if (errno == EFBIG)
+                               return ret;
+                       fprintf(stderr, "  ERROR %d: Failed to write %ld "
+                               "bytes\n", errno, (long)count);
+                       return ret;
+               }
+               written += ret;
+       }
 
-       return 0;
+       return 0;
 }
 
 #define do_close(x)    do { if ((x) > -1) close(x); } while(0);
@@ -149,7 +177,7 @@ static int do_create(const char *filename)
        return fd;
 }
 
-static int do_lseek(int testnum, int subtest, int fd, int filsz, int origin,
+static int do_lseek(int testnum, int subtest, int fd, off_t filsz, int origin,
                    off_t set, off_t exp)
 {
        off_t pos, exp2;
@@ -179,10 +207,10 @@ static int do_lseek(int testnum, int subtest, int fd, int filsz, int origin,
                ret = !(errno == ENXIO);
        } else {
 
-               x = fprintf(stdout, "%02d.%02d %s expected %ld or %ld, got %ld. ",
+               x = fprintf(stdout, "%02d.%02d %s expected %lld or %lld, got %lld. ",
                            testnum, subtest,
                            (origin == SEEK_HOLE) ? "SEEK_HOLE" : "SEEK_DATA",
-                           (long)exp, (long)exp2, (long)pos);
+                           (long long)exp, (long long)exp2, (long long)pos);
                ret = !(pos == exp || pos == exp2);
        }
 
@@ -191,6 +219,474 @@ static int do_lseek(int testnum, int subtest, int fd, int filsz, int origin,
        return ret;
 }
 
+static int huge_file_test(int fd, int testnum, off_t filsz)
+{
+       char *buf = NULL;
+       int bufsz = alloc_size * 16;    /* XFS seems to round allocated size */
+       off_t off = filsz - bufsz;
+       int ret = -1;
+
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* |- DATA -|- HUGE HOLE -|- DATA -| */
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+       ret = do_pwrite(fd, buf, bufsz, off);
+       if (ret) {
+               /*
+                * Report success. Filesystem just cannot handle so large
+                * offsets and correctly reports it.
+                */
+               if (errno == EFBIG) {
+                       fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
+                       ret = 0;
+               }
+               goto out;
+       }
+
+       /* offset at the beginning */
+       ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
+       ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
+       ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
+
+       /* offset around eof */
+       ret += do_lseek(testnum,  5, fd, filsz, SEEK_HOLE, off, off + bufsz);
+       ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, off, off);
+       ret += do_lseek(testnum,  7, fd, filsz, SEEK_DATA, off + 1, off + 1);
+       ret += do_lseek(testnum,  8, fd, filsz, SEEK_DATA, off - bufsz, off);
+
+out:
+       do_free(buf);
+       return ret;
+}
+
+/*
+ * Make sure hole size is properly reported when punched in the middle of a file
+ */
+static int test21(int fd, int testnum)
+{
+       char *buf = NULL;
+       int bufsz, filsz;
+       int ret = 0;
+
+       if (!punch_hole) {
+               fprintf(stdout, "Test skipped as fs doesn't support punch hole.\n");
+               goto out;
+       }
+
+       if (default_behavior) {
+               fprintf(stdout, "Test skipped as fs doesn't support seeking a punched hole.\n");
+               goto out;
+       }
+
+       bufsz = alloc_size * 3;
+       buf = do_malloc(bufsz);
+       if (!buf) {
+               ret = -1;
+               goto out;
+       }
+       memset(buf, 'a', bufsz);
+
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+
+       filsz = bufsz;
+       ret = do_fallocate(fd, alloc_size, alloc_size,
+                          FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE);
+       if (ret < 0)
+               goto out;
+
+       ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum, 2, fd, filsz, SEEK_HOLE, 0, alloc_size);
+       ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, alloc_size, alloc_size * 2);
+out:
+       if (buf)
+               free(buf);
+       return ret;
+}
+
+/*
+ * Make sure hole size is properly reported when starting in the middle of a
+ * hole in ext? doubly indirect tree
+ */
+static int test20(int fd, int testnum)
+{
+       int ret = -1;
+       char *buf = NULL;
+       loff_t bufsz, filsz;
+
+       bufsz = alloc_size;
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* Magic size in the middle of ext[23] triple indirect tree */
+       filsz = (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + 2 * bufsz / 4 + 5) * bufsz;
+       ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
+       if (ret) {
+               /*
+                * Report success. Filesystem just cannot handle so large
+                * offsets and correctly reports it.
+                */
+               if (errno == EFBIG) {
+                       fprintf(stdout, "Test skipped as fs doesn't support so large files.\n");
+                       ret = 0;
+               }
+               goto out;
+       }
+
+       /* Offset inside ext[23] indirect block */
+       ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, 14 * bufsz, filsz - bufsz);
+       /* Offset inside ext[23] doubly indirect block */
+       ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, (12 + 2 * bufsz / 4) * bufsz, filsz - bufsz);
+       /* Offsets inside ext[23] triply indirect block */
+       ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
+               (12 + bufsz / 4 + bufsz / 4 * bufsz / 4 + 3 * bufsz / 4 + 5) * bufsz, filsz - bufsz);
+       ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
+               (12 + bufsz / 4 + 7 * bufsz / 4 * bufsz / 4 + 5 * bufsz / 4) * bufsz, filsz - bufsz);
+       ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA,
+               (12 + bufsz / 4 + 8 * bufsz / 4 * bufsz / 4 + bufsz / 4 + 11) * bufsz, filsz - bufsz);
+out:
+       if (buf)
+               free(buf);
+       return ret;
+}
+/*
+ * Make sure hole size is properly reported when starting in the middle of a
+ * hole in ext? indirect tree
+ */
+static int test19(int fd, int testnum)
+{
+       int ret = -1;
+       char *buf = NULL;
+       int bufsz, filsz;
+
+       bufsz = alloc_size;
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* Magic size just beyond ext[23] indirect tree size */
+       filsz = (12 + bufsz / 4 + 1) * bufsz;
+       ret = do_pwrite(fd, buf, bufsz, filsz - bufsz);
+       if (ret)
+               goto out;
+
+       ret += do_lseek(testnum, 1, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
+       ret += do_lseek(testnum, 2, fd, filsz, SEEK_DATA, 12*bufsz, filsz - bufsz);
+       ret += do_lseek(testnum, 3, fd, filsz, SEEK_DATA, (12 + bufsz / 4 - 8)*bufsz, filsz - bufsz);
+out:
+       if (buf)
+               free(buf);
+       return ret;
+}
+
+/* Make sure we get ENXIO if we pass in a negative offset. */
+static int test18(int fd, int testnum)
+{
+       int ret = 0;
+
+       /* file size doesn't matter in this test, set to 0 */
+       ret += do_lseek(testnum, 1, fd, 0, SEEK_HOLE, -1, -1);
+       ret += do_lseek(testnum, 2, fd, 0, SEEK_DATA, -1, -1);
+
+       return ret;
+}
+
+static int test17(int fd, int testnum)
+{
+       char *buf = NULL;
+       int pagesz = sysconf(_SC_PAGE_SIZE);
+       int bufsz, filsz;
+       int ret = 0;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
+
+       if (pagesz < 4 * alloc_size) {
+               fprintf(stdout, "Test skipped as page size (%d) is less than "
+                       "four times allocation size (%d).\n",
+                       pagesz, (int)alloc_size);
+               goto out;
+       }
+       bufsz = alloc_size;
+       filsz = 3 * bufsz;
+
+       buf = do_malloc(bufsz);
+       if (!buf) {
+               ret = -1;
+               goto out;
+       }
+       memset(buf, 'a', bufsz);
+
+       ret = do_fallocate(fd, 0, filsz, 0);
+       if (ret < 0)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 2 * bufsz);
+       if (ret)
+               goto out;
+
+       ret += do_lseek(testnum,  1, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 0, bufsz);
+       ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 1, 1);
+       ret += do_lseek(testnum,  4, fd, filsz, SEEK_HOLE, 1, bufsz);
+       ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, 2 * bufsz);
+       ret += do_lseek(testnum,  6, fd, filsz, SEEK_HOLE, bufsz, bufsz);
+       ret += do_lseek(testnum,  7, fd, filsz, SEEK_DATA, bufsz + 1, 2 * bufsz);
+       ret += do_lseek(testnum,  8, fd, filsz, SEEK_HOLE, bufsz + 1, bufsz + 1);
+       ret += do_lseek(testnum,  9, fd, filsz, SEEK_DATA, 2 * bufsz, 2 * bufsz);
+       ret += do_lseek(testnum, 10, fd, filsz, SEEK_HOLE, 2 * bufsz, 3 * bufsz);
+       ret += do_lseek(testnum, 11, fd, filsz, SEEK_DATA, 2 * bufsz + 1, 2 * bufsz + 1);
+       ret += do_lseek(testnum, 12, fd, filsz, SEEK_HOLE, 2 * bufsz + 1, 3 * bufsz);
+
+       filsz += bufsz;
+       ret += do_fallocate(fd, 0, filsz, 0);
+
+       ret += do_lseek(testnum, 13, fd, filsz, SEEK_DATA, 3 * bufsz, -1);
+       ret += do_lseek(testnum, 14, fd, filsz, SEEK_HOLE, 3 * bufsz, 3 * bufsz);
+       ret += do_lseek(testnum, 15, fd, filsz, SEEK_DATA, 3 * bufsz + 1, -1);
+       ret += do_lseek(testnum, 16, fd, filsz, SEEK_HOLE, 3 * bufsz + 1, 3 * bufsz + 1);
+
+out:
+       do_free(buf);
+       return ret;
+}
+
+/*
+ * test file with unwritten extents, having non-contiguous dirty pages in
+ * the unwritten extent.
+ */
+static int test16(int fd, int testnum)
+{
+       int ret = 0;
+       char *buf = NULL;
+       int bufsz = sysconf(_SC_PAGE_SIZE);
+       int filsz = 4 << 20;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
+
+       /* HOLE - unwritten DATA in dirty page */
+       /* Each unit is bufsz */
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* preallocate 4M space to file */
+       ret = do_fallocate(fd, 0, filsz, 0);
+       if (ret < 0)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, filsz/2);
+       if (ret)
+               goto out;
+
+       /* offset at the beginning */
+       ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
+       ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
+       ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
+       ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, filsz/2);
+       ret += do_lseek(testnum,  6, fd, filsz, SEEK_HOLE, filsz/2,
+                       filsz/2 + bufsz);
+
+out:
+       do_free(buf);
+       return ret;
+}
+
+/*
+ * test file with unwritten extents, having page just after end of unwritten
+ * extent.
+ */
+static int test15(int fd, int testnum)
+{
+       int ret = 0;
+       char *buf = NULL;
+       int bufsz = sysconf(_SC_PAGE_SIZE);
+       int filsz = 4 << 20;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
+
+       /* HOLE - unwritten DATA in dirty page */
+       /* Each unit is bufsz */
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* preallocate 4M space to file */
+       ret = do_fallocate(fd, 0, filsz, 0);
+       if (ret < 0)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+
+       /* One page written just after end of unwritten extent... */
+       ret = do_pwrite(fd, buf, bufsz, filsz);
+       if (ret)
+               goto out;
+
+       /* update file size */
+       filsz += bufsz;
+
+       /* offset at the beginning */
+       ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
+       ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
+       ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
+       ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, bufsz, filsz - bufsz);
+
+out:
+       do_free(buf);
+       return ret;
+}
+
+/*
+ * test file with unwritten extents, only have pagevec worth of dirty pages
+ * in page cache, a hole and then another page.
+ */
+static int test14(int fd, int testnum)
+{
+       int ret = 0;
+       char *buf = NULL;
+       int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
+       int filsz = 4 << 20;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
+
+       /* HOLE - unwritten DATA in dirty page */
+       /* Each unit is bufsz */
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* preallocate 4M space to file */
+       ret = do_fallocate(fd, 0, filsz, 0);
+       if (ret < 0)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 3 * bufsz);
+       if (ret)
+               goto out;
+
+       /* offset at the beginning */
+       ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
+       ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
+       ret += do_lseek(testnum,  3, fd, filsz, SEEK_HOLE, 3 * bufsz, 4 * bufsz);
+       ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum,  5, fd, filsz, SEEK_DATA, 1, 1);
+       ret += do_lseek(testnum,  6, fd, filsz, SEEK_DATA, bufsz, 3 * bufsz);
+
+out:
+       do_free(buf);
+       return ret;
+}
+
+/*
+ * test file with unwritten extents, only have pagevec worth of dirty pages
+ * in page cache.
+ */
+static int test13(int fd, int testnum)
+{
+       int ret = 0;
+       char *buf = NULL;
+       int bufsz = sysconf(_SC_PAGE_SIZE) * 14;
+       int filsz = 4 << 20;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
+
+       /* HOLE - unwritten DATA in dirty page */
+       /* Each unit is bufsz */
+       buf = do_malloc(bufsz);
+       if (!buf)
+               goto out;
+       memset(buf, 'a', bufsz);
+
+       /* preallocate 4M space to file */
+       ret = do_fallocate(fd, 0, filsz, 0);
+       if (ret < 0)
+               goto out;
+
+       ret = do_pwrite(fd, buf, bufsz, 0);
+       if (ret)
+               goto out;
+
+       /* offset at the beginning */
+       ret += do_lseek(testnum,  1, fd, filsz, SEEK_HOLE, 0, bufsz);
+       ret += do_lseek(testnum,  2, fd, filsz, SEEK_HOLE, 1, bufsz);
+       ret += do_lseek(testnum,  3, fd, filsz, SEEK_DATA, 0, 0);
+       ret += do_lseek(testnum,  4, fd, filsz, SEEK_DATA, 1, 1);
+
+out:
+       do_free(buf);
+       return ret;
+}
+
+/*
+ * Test huge file to check for overflows of block counts due to usage of
+ * 32-bit types.
+ */
+static int test12(int fd, int testnum)
+{
+       return huge_file_test(fd, testnum,
+                               ((long long)alloc_size << 32) + (1 << 20));
+}
+
+/*
+ * Test huge file to check for overflows of block counts due to usage of
+ * signed types
+ */
+static int test11(int fd, int testnum)
+{
+       return huge_file_test(fd, testnum,
+                               ((long long)alloc_size << 31) + (1 << 20));
+}
+
+/* Test an 8G file to check for offset overflows at 1 << 32 */
+static int test10(int fd, int testnum)
+{
+       return huge_file_test(fd, testnum, 8ULL << 30);
+}
+
 /*
  * test file with unwritten extents, have both dirty and
  * writeback pages in page cache.
@@ -200,7 +696,12 @@ static int test09(int fd, int testnum)
        int ret = 0;
        char *buf = NULL;
        int bufsz = alloc_size;
-       int filsz = 8 << 20;
+       int filsz = bufsz * 100 + bufsz;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
 
        /*
         * HOLE - unwritten DATA in dirty page - HOLE -
@@ -250,7 +751,12 @@ static int test08(int fd, int testnum)
        int ret = 0;
        char *buf = NULL;
        int bufsz = alloc_size;
-       int filsz = 4 << 20;
+       int filsz = bufsz * 10 + bufsz;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
 
        /* HOLE - unwritten DATA in writeback page */
        /* Each unit is bufsz */
@@ -293,7 +799,12 @@ static int test07(int fd, int testnum)
        int ret = 0;
        char *buf = NULL;
        int bufsz = alloc_size;
-       int filsz = 4 << 20;
+       int filsz = bufsz * 10 + bufsz;
+
+       if (!unwritten_extents) {
+               fprintf(stdout, "Test skipped as fs doesn't support unwritten extents.\n");
+               goto out;
+       }
 
        /* HOLE - unwritten DATA in dirty page */
        /* Each unit is bufsz */
@@ -577,6 +1088,18 @@ struct testrec seek_tests[] = {
        {  7, test07, "Test file with unwritten extents, only have dirty pages" },
        {  8, test08, "Test file with unwritten extents, only have unwritten pages" },
        {  9, test09, "Test file with unwritten extents, have both dirty && unwritten pages" },
+       { 10, test10, "Test a huge file for offset overflow" },
+       { 11, test11, "Test a huge file for block number signed" },
+       { 12, test12, "Test a huge file for block number overflow" },
+       { 13, test13, "Test file with unwritten extents, only have pagevec dirty pages" },
+       { 14, test14, "Test file with unwritten extents, small hole after pagevec dirty pages" },
+       { 15, test15, "Test file with unwritten extents, page after unwritten extent" },
+       { 16, test16, "Test file with unwritten extents, non-contiguous dirty pages" },
+       { 17, test17, "Test file with unwritten extents, data-hole-data inside page" },
+       { 18, test18, "Test file with negative SEEK_{HOLE,DATA} offsets" },
+       { 19, test19, "Test file SEEK_DATA from middle of a large hole" },
+       { 20, test20, "Test file SEEK_DATA from middle of a huge hole" },
+       { 21, test21, "Test file SEEK_HOLE that was created by PUNCH_HOLE" },
 };
 
 static int run_test(struct testrec *tr)
@@ -614,6 +1137,7 @@ static int test_basic_support(void)
        if (ret)
                goto out;
 
+       ftruncate(fd, 0);
        bufsz = alloc_size * 2;
        filsz = bufsz * 2;
 
@@ -633,12 +1157,10 @@ static int test_basic_support(void)
                goto out;
 
        /* Is SEEK_DATA and SEEK_HOLE supported in the kernel? */
-       pos = lseek(fd, 0, SEEK_DATA);
-       if (pos != -1)
-               pos = lseek(fd, 0, SEEK_HOLE);
+       pos = lseek(fd, 0, SEEK_HOLE);
        if (pos == -1) {
-               fprintf(stderr, "Kernel does not support llseek(2) extensions "
-                       "SEEK_HOLE and/or SEEK_DATA. Aborting.\n");
+               fprintf(stderr, "Kernel does not support llseek(2) extension "
+                       "SEEK_HOLE. Aborting.\n");
                ret = -1;
                goto out;
        }
@@ -648,6 +1170,36 @@ static int test_basic_support(void)
                fprintf(stderr, "File system supports the default behavior.\n");
        }
 
+       if (default_behavior && !allow_default_behavior) {
+               fprintf(stderr, "Default behavior is not allowed. Aborting.\n");
+               ret = -1;
+               goto out;
+       }
+
+       ftruncate(fd, 0);
+       if (fallocate(fd, 0, 0, alloc_size * 2) == -1) {
+               if (errno == EOPNOTSUPP)
+                       fprintf(stderr, "File system does not support fallocate.\n");
+               else {
+                       fprintf(stderr, "ERROR %d: Failed to preallocate "
+                               "space to %ld bytes. Aborting.\n", errno, (long) alloc_size);
+                       ret = -1;
+               }
+               goto out;
+       } else if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+                            0, alloc_size) == -1) {
+               fprintf(stderr, "File system does not support punch hole.\n");
+       } else {
+               punch_hole = 1;
+       }
+
+       pos = lseek(fd, 0, SEEK_DATA);
+       if (pos == 0) {
+               fprintf(stderr, "File system does not support unwritten extents.\n");
+               goto out;
+       }
+       unwritten_extents = 1;
+
        printf("\n");
 
 out:
@@ -656,6 +1208,12 @@ out:
        return ret;
 }
 
+void usage(char *cmd)
+{
+       fprintf(stdout, "Usage: %s [-tf] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
+       exit(1);
+}
+
 int main(int argc, char **argv)
 {
        int ret = -1;
@@ -663,24 +1221,54 @@ int main(int argc, char **argv)
        int opt;
        int check_support = 0;
        int numtests = sizeof(seek_tests) / sizeof(struct testrec);
+       int teststart, testend;
 
-       if (argc != 2) {
-               fprintf(stdout, "Usage: %s base_file_path\n", argv[0]);
-               return ret;
-       }
+       /*
+        * First twelve tests are used by generic/285. To run these is the
+        * default. Further tests are run e.g. by generic/436. They are not
+        * run by default in order to not regress older kernels.
+        */
+       teststart = 1;
+       testend = 12;
 
-       while ((opt = getopt(argc, argv, "t")) != -1) {
+       while ((opt = getopt(argc, argv, "tfs:e:")) != -1) {
                switch (opt) {
                case 't':
                        check_support++;
                        break;
+               case 'f':
+                       allow_default_behavior = 0;
+                       break;
+               case 's':
+                       teststart = strtol(optarg, NULL, 10);
+                       if (teststart <= 0 || teststart > numtests) {
+                               fprintf(stdout, "Invalid starting test: %s\n",
+                                       optarg);
+                               usage(argv[0]);
+                       }
+                       break;
+               case 'e':
+                       testend = strtol(optarg, NULL, 10);
+                       if (testend <= 0 || testend > numtests) {
+                               fprintf(stdout, "Invalid final test: %s\n",
+                                       optarg);
+                               usage(argv[0]);
+                       }
+                       break;
                default:
-                       fprintf(stderr, "Usage: %s [-t] base_file_path\n",
-                               argv[0]);
-                       return ret;
+                       usage(argv[0]);
                }
        }
 
+       /* should be exactly one arg left, the filename */
+       if (optind != argc - 1)
+               usage(argv[0]);
+
+       if (teststart > testend) {
+               fprintf(stdout, "Starting test is larger than final test!\n");
+               usage(argv[0]);
+       }
+
        base_file_path = (char *)strdup(argv[optind]);
 
        ret = test_basic_support();
@@ -688,9 +1276,12 @@ int main(int argc, char **argv)
                goto out;
 
        for (i = 0; i < numtests; ++i) {
-               ret = run_test(&seek_tests[i]);
-               if (ret)
-                       break;
+               if (seek_tests[i].test_num >= teststart &&
+                   seek_tests[i].test_num <= testend) {
+                       ret = run_test(&seek_tests[i]);
+                       if (ret)
+                               break;
+               }
        }
 
 out: