diff options
author | Florian Weimer <fweimer@redhat.com> | 2018-08-21 12:56:53 +0200 |
---|---|---|
committer | Florian Weimer <fweimer@redhat.com> | 2018-08-21 12:56:53 +0200 |
commit | aa42b3dbcb0326badf377fec2c7fb2f34fdabecd (patch) | |
tree | 818cd4e9c75b0dba8c982ce5d3b5def2d358ba05 /support | |
parent | c7627f41baaeb7f1a7b73ff66ef48eaafc1990f6 (diff) | |
download | glibc-aa42b3dbcb0326badf377fec2c7fb2f34fdabecd.tar glibc-aa42b3dbcb0326badf377fec2c7fb2f34fdabecd.tar.gz glibc-aa42b3dbcb0326badf377fec2c7fb2f34fdabecd.tar.bz2 glibc-aa42b3dbcb0326badf377fec2c7fb2f34fdabecd.zip |
Avoid running some tests if the file system does not support holes
Otherwise, these tests fills up the entire disk (or just run very
slowly and eventually time out).
Diffstat (limited to 'support')
-rw-r--r-- | support/Makefile | 1 | ||||
-rw-r--r-- | support/support.h | 6 | ||||
-rw-r--r-- | support/support_descriptor_supports_holes.c | 87 |
3 files changed, 94 insertions, 0 deletions
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..c7099ca67c --- /dev/null +++ b/support/support_descriptor_supports_holes.c @@ -0,0 +1,87 @@ +/* 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 (see + block_limit below). */ + 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); + + /* This limit is arbitrary. The file system needs to store + somewhere that data exists at the write offset, and this may + moderately increase the number of blocks used by the file, in + proportion to the initial block count, but not in proportion to + the write offset. */ + 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; +} |