(root)/
glibc-2.38/
support/
support_descriptor_supports_holes.c
       1  /* Test for file system hole support.
       2     Copyright (C) 2018-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <stdbool.h>
      20  #include <support.h>
      21  #include <support/check.h>
      22  #include <sys/stat.h>
      23  #include <xunistd.h>
      24  
      25  int
      26  support_descriptor_supports_holes (int fd)
      27  {
      28    enum
      29      {
      30        /* Write offset for the enlarged file.  This value is arbitrary
      31           and hopefully large enough to trigger the creation of holes.
      32           We cannot use the file system block size as a reference here
      33           because it is incorrect for network file systems.  */
      34        write_offset = 16 * 1024 * 1024,
      35  
      36        /* Our write may add this number of additional blocks (see
      37           block_limit below): writing at offset 16M can require two data block
      38           indirections, each of which can be as large as 8KB on ext2, thus 32
      39           512B sectors.  */
      40        block_headroom = 32,
      41      };
      42  
      43    struct stat st;
      44    xfstat (fd, &st);
      45    if (!S_ISREG (st.st_mode))
      46      FAIL_EXIT1 ("descriptor %d does not refer to a regular file", fd);
      47    if (st.st_size != 0)
      48      FAIL_EXIT1 ("descriptor %d does not refer to an empty file", fd);
      49    if (st.st_blocks > block_headroom)
      50      FAIL_EXIT1 ("descriptor %d refers to a pre-allocated file (%lld blocks)",
      51                  fd, (long long int) st.st_blocks);
      52  
      53    /* Write a single byte at the start of the file to compute the block
      54       usage for a single byte.  */
      55    xlseek (fd, 0, SEEK_SET);
      56    char b = '@';
      57    xwrite (fd, &b, 1);
      58    /* Attempt to bypass delayed allocation.  */
      59    TEST_COMPARE (fsync (fd), 0);
      60    xfstat (fd, &st);
      61  
      62    /* This limit is arbitrary.  The file system needs to store
      63       somewhere that data exists at the write offset, and this may
      64       moderately increase the number of blocks used by the file, in
      65       proportion to the initial block count, but not in proportion to
      66       the write offset.  */
      67    unsigned long long int block_limit = 2 * st.st_blocks + block_headroom;
      68  
      69    /* Write a single byte at 16 megabytes.  */
      70    xlseek (fd, write_offset, SEEK_SET);
      71    xwrite (fd, &b, 1);
      72    /* Attempt to bypass delayed allocation.  */
      73    TEST_COMPARE (fsync (fd), 0);
      74    xfstat (fd, &st);
      75    bool supports_holes = st.st_blocks <= block_limit;
      76  
      77    /* Also check that extending the file does not fill up holes.  */
      78    xftruncate (fd, 2 * write_offset);
      79    /* Attempt to bypass delayed allocation.  */
      80    TEST_COMPARE (fsync (fd), 0);
      81    xfstat (fd, &st);
      82    supports_holes = supports_holes && st.st_blocks <= block_limit;
      83  
      84    /* Return to a zero-length file.  */
      85    xftruncate (fd, 0);
      86    xlseek (fd, 0, SEEK_SET);
      87  
      88    return supports_holes;
      89  }