generic: Test that SEEK_HOLE can find a punched hole
[xfstests-dev.git] / src / seek_sanity_test.c
index 9245464400e8712c4d0dd58817585d073db68d03..30e996e2e6a7af63884450757915c8ed24674d4b 100644 (file)
@@ -1,20 +1,7 @@
+// 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
@@ -29,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)
@@ -130,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;
 }
@@ -274,6 +265,52 @@ out:
        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
@@ -293,8 +330,17 @@ static int test20(int fd, int testnum)
        /* 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)
+       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);
@@ -1053,6 +1099,7 @@ struct testrec seek_tests[] = {
        { 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)
@@ -1123,16 +1170,27 @@ 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) == -1) {
+       if (fallocate(fd, 0, 0, alloc_size * 2) == -1) {
                if (errno == EOPNOTSUPP)
-                       fprintf(stderr, "File system does not support fallocate.");
+                       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);
@@ -1152,7 +1210,7 @@ out:
 
 void usage(char *cmd)
 {
-       fprintf(stdout, "Usage: %s [-t] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
+       fprintf(stdout, "Usage: %s [-tf] [-s <starttest>] [-e <endtest>] base_file_path\n", cmd);
        exit(1);
 }
 
@@ -1173,11 +1231,14 @@ int main(int argc, char **argv)
        teststart = 1;
        testend = 12;
 
-       while ((opt = getopt(argc, argv, "ts:e:")) != -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) {