1 /* Test for file system hole support.
2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <support/check.h>
26 support_descriptor_supports_holes (int fd
)
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,
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
45 if (!S_ISREG (st
.st_mode
))
46 FAIL_EXIT1 ("descriptor %d does not refer to a regular file", fd
);
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
);
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
);
58 /* Attempt to bypass delayed allocation. */
59 TEST_COMPARE (fsync (fd
), 0);
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
67 unsigned long long int block_limit
= 2 * st
.st_blocks
+ block_headroom
;
69 /* Write a single byte at 16 megabytes. */
70 xlseek (fd
, write_offset
, SEEK_SET
);
72 /* Attempt to bypass delayed allocation. */
73 TEST_COMPARE (fsync (fd
), 0);
75 bool supports_holes
= st
.st_blocks
<= block_limit
;
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);
82 supports_holes
= supports_holes
&& st
.st_blocks
<= block_limit
;
84 /* Return to a zero-length file. */
86 xlseek (fd
, 0, SEEK_SET
);
88 return supports_holes
;