1 | /* Test for file system hole support. |
2 | Copyright (C) 2018-2019 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 | <http://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). */ |
38 | block_headroom = 8, |
39 | }; |
40 | |
41 | struct stat64 st; |
42 | xfstat (fd, &st); |
43 | if (!S_ISREG (st.st_mode)) |
44 | FAIL_EXIT1 ("descriptor %d does not refer to a regular file" , fd); |
45 | if (st.st_size != 0) |
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); |
50 | |
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); |
54 | char b = '@'; |
55 | xwrite (fd, &b, 1); |
56 | /* Attempt to bypass delayed allocation. */ |
57 | TEST_COMPARE (fsync (fd), 0); |
58 | xfstat (fd, &st); |
59 | |
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 |
64 | the write offset. */ |
65 | unsigned long long int block_limit = 2 * st.st_blocks + block_headroom; |
66 | |
67 | /* Write a single byte at 16 megabytes. */ |
68 | xlseek (fd, write_offset, SEEK_SET); |
69 | xwrite (fd, &b, 1); |
70 | /* Attempt to bypass delayed allocation. */ |
71 | TEST_COMPARE (fsync (fd), 0); |
72 | xfstat (fd, &st); |
73 | bool supports_holes = st.st_blocks <= block_limit; |
74 | |
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); |
79 | xfstat (fd, &st); |
80 | supports_holes = supports_holes && st.st_blocks <= block_limit; |
81 | |
82 | /* Return to a zero-length file. */ |
83 | xftruncate (fd, 0); |
84 | xlseek (fd, 0, SEEK_SET); |
85 | |
86 | return supports_holes; |
87 | } |
88 | |