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). */
43 if (!S_ISREG (st
.st_mode
))
44 FAIL_EXIT1 ("descriptor %d does not refer to a regular file", fd
);
46 FAIL_EXIT1 ("descriptor %d does not refer to an empty file", fd
);
47 if (st
.st_blocks
> block_headroom
)
48 FAIL_EXIT1 ("descriptor %d refers to a pre-allocated file (%lld blocks)",
49 fd
, (long long int) st
.st_blocks
);
51 /* Write a single byte at the start of the file to compute the block
52 usage for a single byte. */
53 xlseek (fd
, 0, SEEK_SET
);
56 /* Attempt to bypass delayed allocation. */
57 TEST_COMPARE (fsync (fd
), 0);
60 /* This limit is arbitrary. The file system needs to store
61 somewhere that data exists at the write offset, and this may
62 moderately increase the number of blocks used by the file, in
63 proportion to the initial block count, but not in proportion to
65 unsigned long long int block_limit
= 2 * st
.st_blocks
+ block_headroom
;
67 /* Write a single byte at 16 megabytes. */
68 xlseek (fd
, write_offset
, SEEK_SET
);
70 /* Attempt to bypass delayed allocation. */
71 TEST_COMPARE (fsync (fd
), 0);
73 bool supports_holes
= st
.st_blocks
<= block_limit
;
75 /* Also check that extending the file does not fill up holes. */
76 xftruncate (fd
, 2 * write_offset
);
77 /* Attempt to bypass delayed allocation. */
78 TEST_COMPARE (fsync (fd
), 0);
80 supports_holes
= supports_holes
&& st
.st_blocks
<= block_limit
;
82 /* Return to a zero-length file. */
84 xlseek (fd
, 0, SEEK_SET
);
86 return supports_holes
;