Avoid running some tests if the file system does not support holes

Message ID 20180817150750.83DB3402F9143@oldenburg.str.redhat.com
State Committed
Headers

Commit Message

Florian Weimer Aug. 17, 2018, 3:07 p.m. UTC
  Otherwise, these tests fills up the entire disk (or just run very
slowly and eventually time out).

2018-08-17  Florian Weimer  <fweimer@redhat.com>

	* support/support_descriptor_supports_holes.c: New file.
	* support/Makefile (libsupport-routines): Add
	support_descriptor_supports_holes.
	* support/support.h (support_descriptor_supports_holes): Declare.
	* io/tst-copy_file_range.c: Call support_descriptor_supports_holes
	and stop testing if holes are not supported.
	* io/test-lfs.c (do_prepare): Likewise.
	* sysdeps/unix/sysv/linux/tst-fallocate-common.c (do_prepare):
	Likewise.
	* timezone/tst-tzset.c (create_tz_file): Likewise.
	* misc/tst-preadvwritev-common.c (temp_fd_supports_holes) New
	variable.
	(do_prepare): Set it.
	* misc/tst-preadvwritev64.c (do_test): Use temp_fd_supports_holes.
  

Comments

Carlos O'Donell Aug. 21, 2018, 1:16 a.m. UTC | #1
On 08/17/2018 11:07 AM, Florian Weimer wrote:
> Otherwise, these tests fills up the entire disk (or just run very
> slowly and eventually time out).
> 
> 2018-08-17  Florian Weimer  <fweimer@redhat.com>
> 
> 	* support/support_descriptor_supports_holes.c: New file.
> 	* support/Makefile (libsupport-routines): Add
> 	support_descriptor_supports_holes.
> 	* support/support.h (support_descriptor_supports_holes): Declare.
> 	* io/tst-copy_file_range.c: Call support_descriptor_supports_holes
> 	and stop testing if holes are not supported.
> 	* io/test-lfs.c (do_prepare): Likewise.
> 	* sysdeps/unix/sysv/linux/tst-fallocate-common.c (do_prepare):
> 	Likewise.
> 	* timezone/tst-tzset.c (create_tz_file): Likewise.
> 	* misc/tst-preadvwritev-common.c (temp_fd_supports_holes) New
> 	variable.
> 	(do_prepare): Set it.
> 	* misc/tst-preadvwritev64.c (do_test): Use temp_fd_supports_holes.
> 

Thanks for increasing the robustness of tests :-)

OK for master with the additional comment.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> diff --git a/io/test-lfs.c b/io/test-lfs.c
> index f7721a5995..52120e9bca 100644
> --- a/io/test-lfs.c
> +++ b/io/test-lfs.c
> @@ -25,6 +25,7 @@
>  #include <error.h>
>  #include <errno.h>
>  #include <sys/resource.h>
> +#include <support/check.h>
>  
>  /* Prototype for our test function.  */
>  extern void do_prepare (int argc, char *argv[]);
> @@ -70,6 +71,8 @@ do_prepare (int argc, char *argv[])
>        else
>  	error (EXIT_FAILURE, errno, "cannot create temporary file");
>      }
> +  if (!support_descriptor_supports_holes (fd))
> +    FAIL_UNSUPPORTED ("File %s does not support holes", name);

OK.

>    add_temp_file (name);
>  
>    if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
> diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
> index 3d531a1937..e5b46f91db 100644
> --- a/io/tst-copy_file_range.c
> +++ b/io/tst-copy_file_range.c
> @@ -739,7 +739,12 @@ do_test (void)
>      *p = rand () >> 24;
>  
>    infd = create_temp_file ("tst-copy_file_range-in-", &infile);
> -  xclose (create_temp_file ("tst-copy_file_range-out-", &outfile));
> +  {
> +    int outfd = create_temp_file ("tst-copy_file_range-out-", &outfile);
> +    if (!support_descriptor_supports_holes (outfd))
> +      FAIL_UNSUPPORTED ("File %s does not support holes", outfile);
> +    xclose (outfd);
> +  }

OK.

>  
>    /* Try to find a different directory from the default input/output
>       file.  */
> diff --git a/misc/tst-preadvwritev-common.c b/misc/tst-preadvwritev-common.c
> index b59a3de465..4a5e53a140 100644
> --- a/misc/tst-preadvwritev-common.c
> +++ b/misc/tst-preadvwritev-common.c
> @@ -17,6 +17,7 @@
>     <http://www.gnu.org/licenses/>.  */
>  
>  #include <array_length.h>
> +#include <stdbool.h>
>  #include <stdio.h>
>  #include <stdint.h>
>  #include <errno.h>
> @@ -26,10 +27,12 @@
>  
>  #include <support/check.h>
>  #include <support/temp_file.h>
> +#include <support/support.h>
>  #include <support/xunistd.h>
>  
>  static char *temp_filename;
>  static int temp_fd;
> +static bool temp_fd_supports_holes;

OK.

>  
>  static int do_test (void);
>  
> @@ -39,6 +42,7 @@ do_prepare (int argc, char **argv)
>    temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
>    if (temp_fd == -1)
>      FAIL_EXIT1 ("cannot create temporary file");
> +  temp_fd_supports_holes = support_descriptor_supports_holes (temp_fd);

OK.

>  }
>  #define PREPARE do_prepare
>  
> diff --git a/misc/tst-preadvwritev64.c b/misc/tst-preadvwritev64.c
> index b718da6be5..451d03e4a8 100644
> --- a/misc/tst-preadvwritev64.c
> +++ b/misc/tst-preadvwritev64.c
> @@ -26,6 +26,12 @@ do_test (void)
>  
>    ret = do_test_with_offset (0);
>  
> +  if (!temp_fd_supports_holes)
> +    {
> +      puts ("warning: partial test due to lack of support for holes");
> +      return ret;
> +    }

OK.

> +
>    /* Create a sparse file larger than 4GB to check if offset is handled
>       correctly in p{write,read}v64. */
>    off_t base_offset = UINT32_MAX + 2048LL;
> diff --git a/support/Makefile b/support/Makefile
> index 652d2cdf69..9063046c23 100644
> --- a/support/Makefile
> +++ b/support/Makefile
> @@ -43,6 +43,7 @@ libsupport-routines = \
>    support_capture_subprocess \
>    support_capture_subprocess_check \
>    support_chroot \
> +  support_descriptor_supports_holes \

OK.

>    support_enter_mount_namespace \
>    support_enter_network_namespace \
>    support_format_address_family \
> diff --git a/support/support.h b/support/support.h
> index b61fe0735c..34ae6e10cd 100644
> --- a/support/support.h
> +++ b/support/support.h
> @@ -65,6 +65,12 @@ void support_write_file_string (const char *path, const char *contents);
>     the result).  */
>  char *support_quote_blob (const void *blob, size_t length);
>  
> +/* Returns non-zero if the file descriptor is a regular file on a file
> +   system which supports holes (that is, seeking and writing does not
> +   allocate storage for the range of zeros).  FD must refer to a
> +   regular file open for writing, and initially empty.  */
> +int support_descriptor_supports_holes (int fd);

OK.

> +
>  /* Error-checking wrapper functions which terminate the process on
>     error.  */
>  
> diff --git a/support/support_descriptor_supports_holes.c b/support/support_descriptor_supports_holes.c
> new file mode 100644
> index 0000000000..b04498664e
> --- /dev/null
> +++ b/support/support_descriptor_supports_holes.c
> @@ -0,0 +1,80 @@
> +/* Test for file system hole support.
> +   Copyright (C) 2018 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library 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
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <http://www.gnu.org/licenses/>.  */
> +
> +#include <stdbool.h>
> +#include <support.h>
> +#include <support/check.h>
> +#include <sys/stat.h>
> +#include <xunistd.h>
> +
> +int
> +support_descriptor_supports_holes (int fd)
> +{
> +  enum
> +    {
> +      /* Write offset for the enlarged file.  This value is arbitrary
> +         and hopefully large enough to trigger the creation of holes.
> +         We cannot use the file system block size as a reference here
> +         because it is incorrect for network file systems.  */
> +      write_offset = 16 * 1024 * 1024,

OK. Excellent comment.

> +
> +      /* Our write may add this number of additional blocks.  */
> +      block_headroom = 8,
> +    };
> +
> +  struct stat64 st;
> +  xfstat (fd, &st);
> +  if (!S_ISREG (st.st_mode))
> +    FAIL_EXIT1 ("descriptor %d does not refer to a regular file", fd);
> +  if (st.st_size != 0)
> +    FAIL_EXIT1 ("descriptor %d does not refer to an empty file", fd);
> +  if (st.st_blocks > block_headroom)
> +    FAIL_EXIT1 ("descriptor %d refers to a pre-allocated file (%lld blocks)",
> +                fd, (long long int) st.st_blocks);
> +
> +  /* Write a single byte at the start of the file to compute the block
> +     usage for a single byte.  */
> +  xlseek (fd, 0, SEEK_SET);
> +  char b = '@';
> +  xwrite (fd, &b, 1);
> +  /* Attempt to bypass delayed allocation.  */
> +  TEST_COMPARE (fsync (fd), 0);

OK.

> +  xfstat (fd, &st);
Needs a comment explaining why you pick block_limit to be this value. Is it
arbitrary or chosen specifically given some specific knowledge.

> +  unsigned long long int block_limit = 2 * st.st_blocks + block_headroom;
> +
> +  /* Write a single byte at 16 megabytes.  */
> +  xlseek (fd, write_offset, SEEK_SET);

OK.

> +  xwrite (fd, &b, 1);
> +  /* Attempt to bypass delayed allocation.  */
> +  TEST_COMPARE (fsync (fd), 0);
> +  xfstat (fd, &st);
> +  bool supports_holes = st.st_blocks <= block_limit;

OK.

> +
> +  /* Also check that extending the file does not fill up holes.  */
> +  xftruncate (fd, 2 * write_offset);
> +  /* Attempt to bypass delayed allocation.  */
> +  TEST_COMPARE (fsync (fd), 0);
> +  xfstat (fd, &st);
> +  supports_holes = supports_holes && st.st_blocks <= block_limit;

OK.

> +
> +  /* Return to a zero-length file.  */
> +  xftruncate (fd, 0);
> +  xlseek (fd, 0, SEEK_SET);
> +
> +  return supports_holes;
> +}
> diff --git a/sysdeps/unix/sysv/linux/tst-fallocate-common.c b/sysdeps/unix/sysv/linux/tst-fallocate-common.c
> index a6ba403c62..bc54cf5134 100644
> --- a/sysdeps/unix/sysv/linux/tst-fallocate-common.c
> +++ b/sysdeps/unix/sysv/linux/tst-fallocate-common.c
> @@ -41,6 +41,8 @@ do_prepare (int argc, char **argv)
>    temp_fd = create_temp_file ("tst-fallocate.", &temp_filename);
>    if (temp_fd == -1)
>      FAIL_EXIT1 ("cannot create temporary file: %m");
> +  if (!support_descriptor_supports_holes (temp_fd))
> +    FAIL_UNSUPPORTED ("File %s does not support holes", temp_filename);

OK.

>  }
>  #define PREPARE do_prepare
>  
> diff --git a/timezone/tst-tzset.c b/timezone/tst-tzset.c
> index 27f986b56d..744c583f7e 100644
> --- a/timezone/tst-tzset.c
> +++ b/timezone/tst-tzset.c
> @@ -24,6 +24,7 @@
>  #include <sys/resource.h>
>  #include <time.h>
>  #include <unistd.h>
> +#include <support/check.h>
>  
>  #define TIMEOUT 5
>  static int do_test (void);
> @@ -38,6 +39,8 @@ create_tz_file (off64_t size)
>    int fd = create_temp_file ("tst-tzset-", &path);
>    if (fd < 0)
>      exit (1);
> +  if (!support_descriptor_supports_holes (fd))
> +    FAIL_UNSUPPORTED ("File %s does not support holes", path);

OK.

>  
>    // Reopen for large-file support.
>    close (fd);
>
  

Patch

diff --git a/io/test-lfs.c b/io/test-lfs.c
index f7721a5995..52120e9bca 100644
--- a/io/test-lfs.c
+++ b/io/test-lfs.c
@@ -25,6 +25,7 @@ 
 #include <error.h>
 #include <errno.h>
 #include <sys/resource.h>
+#include <support/check.h>
 
 /* Prototype for our test function.  */
 extern void do_prepare (int argc, char *argv[]);
@@ -70,6 +71,8 @@  do_prepare (int argc, char *argv[])
       else
 	error (EXIT_FAILURE, errno, "cannot create temporary file");
     }
+  if (!support_descriptor_supports_holes (fd))
+    FAIL_UNSUPPORTED ("File %s does not support holes", name);
   add_temp_file (name);
 
   if (getrlimit64 (RLIMIT_FSIZE, &rlim) != 0)
diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
index 3d531a1937..e5b46f91db 100644
--- a/io/tst-copy_file_range.c
+++ b/io/tst-copy_file_range.c
@@ -739,7 +739,12 @@  do_test (void)
     *p = rand () >> 24;
 
   infd = create_temp_file ("tst-copy_file_range-in-", &infile);
-  xclose (create_temp_file ("tst-copy_file_range-out-", &outfile));
+  {
+    int outfd = create_temp_file ("tst-copy_file_range-out-", &outfile);
+    if (!support_descriptor_supports_holes (outfd))
+      FAIL_UNSUPPORTED ("File %s does not support holes", outfile);
+    xclose (outfd);
+  }
 
   /* Try to find a different directory from the default input/output
      file.  */
diff --git a/misc/tst-preadvwritev-common.c b/misc/tst-preadvwritev-common.c
index b59a3de465..4a5e53a140 100644
--- a/misc/tst-preadvwritev-common.c
+++ b/misc/tst-preadvwritev-common.c
@@ -17,6 +17,7 @@ 
    <http://www.gnu.org/licenses/>.  */
 
 #include <array_length.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <errno.h>
@@ -26,10 +27,12 @@ 
 
 #include <support/check.h>
 #include <support/temp_file.h>
+#include <support/support.h>
 #include <support/xunistd.h>
 
 static char *temp_filename;
 static int temp_fd;
+static bool temp_fd_supports_holes;
 
 static int do_test (void);
 
@@ -39,6 +42,7 @@  do_prepare (int argc, char **argv)
   temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
   if (temp_fd == -1)
     FAIL_EXIT1 ("cannot create temporary file");
+  temp_fd_supports_holes = support_descriptor_supports_holes (temp_fd);
 }
 #define PREPARE do_prepare
 
diff --git a/misc/tst-preadvwritev64.c b/misc/tst-preadvwritev64.c
index b718da6be5..451d03e4a8 100644
--- a/misc/tst-preadvwritev64.c
+++ b/misc/tst-preadvwritev64.c
@@ -26,6 +26,12 @@  do_test (void)
 
   ret = do_test_with_offset (0);
 
+  if (!temp_fd_supports_holes)
+    {
+      puts ("warning: partial test due to lack of support for holes");
+      return ret;
+    }
+
   /* Create a sparse file larger than 4GB to check if offset is handled
      correctly in p{write,read}v64. */
   off_t base_offset = UINT32_MAX + 2048LL;
diff --git a/support/Makefile b/support/Makefile
index 652d2cdf69..9063046c23 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -43,6 +43,7 @@  libsupport-routines = \
   support_capture_subprocess \
   support_capture_subprocess_check \
   support_chroot \
+  support_descriptor_supports_holes \
   support_enter_mount_namespace \
   support_enter_network_namespace \
   support_format_address_family \
diff --git a/support/support.h b/support/support.h
index b61fe0735c..34ae6e10cd 100644
--- a/support/support.h
+++ b/support/support.h
@@ -65,6 +65,12 @@  void support_write_file_string (const char *path, const char *contents);
    the result).  */
 char *support_quote_blob (const void *blob, size_t length);
 
+/* Returns non-zero if the file descriptor is a regular file on a file
+   system which supports holes (that is, seeking and writing does not
+   allocate storage for the range of zeros).  FD must refer to a
+   regular file open for writing, and initially empty.  */
+int support_descriptor_supports_holes (int fd);
+
 /* Error-checking wrapper functions which terminate the process on
    error.  */
 
diff --git a/support/support_descriptor_supports_holes.c b/support/support_descriptor_supports_holes.c
new file mode 100644
index 0000000000..b04498664e
--- /dev/null
+++ b/support/support_descriptor_supports_holes.c
@@ -0,0 +1,80 @@ 
+/* Test for file system hole support.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <stdbool.h>
+#include <support.h>
+#include <support/check.h>
+#include <sys/stat.h>
+#include <xunistd.h>
+
+int
+support_descriptor_supports_holes (int fd)
+{
+  enum
+    {
+      /* Write offset for the enlarged file.  This value is arbitrary
+         and hopefully large enough to trigger the creation of holes.
+         We cannot use the file system block size as a reference here
+         because it is incorrect for network file systems.  */
+      write_offset = 16 * 1024 * 1024,
+
+      /* Our write may add this number of additional blocks.  */
+      block_headroom = 8,
+    };
+
+  struct stat64 st;
+  xfstat (fd, &st);
+  if (!S_ISREG (st.st_mode))
+    FAIL_EXIT1 ("descriptor %d does not refer to a regular file", fd);
+  if (st.st_size != 0)
+    FAIL_EXIT1 ("descriptor %d does not refer to an empty file", fd);
+  if (st.st_blocks > block_headroom)
+    FAIL_EXIT1 ("descriptor %d refers to a pre-allocated file (%lld blocks)",
+                fd, (long long int) st.st_blocks);
+
+  /* Write a single byte at the start of the file to compute the block
+     usage for a single byte.  */
+  xlseek (fd, 0, SEEK_SET);
+  char b = '@';
+  xwrite (fd, &b, 1);
+  /* Attempt to bypass delayed allocation.  */
+  TEST_COMPARE (fsync (fd), 0);
+  xfstat (fd, &st);
+  unsigned long long int block_limit = 2 * st.st_blocks + block_headroom;
+
+  /* Write a single byte at 16 megabytes.  */
+  xlseek (fd, write_offset, SEEK_SET);
+  xwrite (fd, &b, 1);
+  /* Attempt to bypass delayed allocation.  */
+  TEST_COMPARE (fsync (fd), 0);
+  xfstat (fd, &st);
+  bool supports_holes = st.st_blocks <= block_limit;
+
+  /* Also check that extending the file does not fill up holes.  */
+  xftruncate (fd, 2 * write_offset);
+  /* Attempt to bypass delayed allocation.  */
+  TEST_COMPARE (fsync (fd), 0);
+  xfstat (fd, &st);
+  supports_holes = supports_holes && st.st_blocks <= block_limit;
+
+  /* Return to a zero-length file.  */
+  xftruncate (fd, 0);
+  xlseek (fd, 0, SEEK_SET);
+
+  return supports_holes;
+}
diff --git a/sysdeps/unix/sysv/linux/tst-fallocate-common.c b/sysdeps/unix/sysv/linux/tst-fallocate-common.c
index a6ba403c62..bc54cf5134 100644
--- a/sysdeps/unix/sysv/linux/tst-fallocate-common.c
+++ b/sysdeps/unix/sysv/linux/tst-fallocate-common.c
@@ -41,6 +41,8 @@  do_prepare (int argc, char **argv)
   temp_fd = create_temp_file ("tst-fallocate.", &temp_filename);
   if (temp_fd == -1)
     FAIL_EXIT1 ("cannot create temporary file: %m");
+  if (!support_descriptor_supports_holes (temp_fd))
+    FAIL_UNSUPPORTED ("File %s does not support holes", temp_filename);
 }
 #define PREPARE do_prepare
 
diff --git a/timezone/tst-tzset.c b/timezone/tst-tzset.c
index 27f986b56d..744c583f7e 100644
--- a/timezone/tst-tzset.c
+++ b/timezone/tst-tzset.c
@@ -24,6 +24,7 @@ 
 #include <sys/resource.h>
 #include <time.h>
 #include <unistd.h>
+#include <support/check.h>
 
 #define TIMEOUT 5
 static int do_test (void);
@@ -38,6 +39,8 @@  create_tz_file (off64_t size)
   int fd = create_temp_file ("tst-tzset-", &path);
   if (fd < 0)
     exit (1);
+  if (!support_descriptor_supports_holes (fd))
+    FAIL_UNSUPPORTED ("File %s does not support holes", path);
 
   // Reopen for large-file support.
   close (fd);