1FALLOCATE(2) Linux Programmer's Manual FALLOCATE(2)
2
3
4
6 fallocate - manipulate file space
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <fcntl.h>
11
12 int fallocate(int fd, int mode, off_t offset, off_t len);
13
15 This is a nonportable, Linux-specific system call. For the portable,
16 POSIX.1-specified method of ensuring that space is allocated for a
17 file, see posix_fallocate(3).
18
19 fallocate() allows the caller to directly manipulate the allocated disk
20 space for the file referred to by fd for the byte range starting at
21 offset and continuing for len bytes.
22
23 The mode argument determines the operation to be performed on the given
24 range. Details of the supported operations are given in the subsec‐
25 tions below.
26
27 Allocating disk space
28 The default operation (i.e., mode is zero) of fallocate() allocates the
29 disk space within the range specified by offset and len. The file size
30 (as reported by stat(2)) will be changed if offset+len is greater than
31 the file size. Any subregion within the range specified by offset and
32 len that did not contain data before the call will be initialized to
33 zero. This default behavior closely resembles the behavior of the
34 posix_fallocate(3) library function, and is intended as a method of
35 optimally implementing that function.
36
37 After a successful call, subsequent writes into the range specified by
38 offset and len are guaranteed not to fail because of lack of disk
39 space.
40
41 If the FALLOC_FL_KEEP_SIZE flag is specified in mode, the behavior of
42 the call is similar, but the file size will not be changed even if off‐
43 set+len is greater than the file size. Preallocating zeroed blocks
44 beyond the end of the file in this manner is useful for optimizing
45 append workloads.
46
47 If the FALLOC_FL_UNSHARE flag is specified in mode, shared file data
48 extents will be made private to the file to guarantee that a subsequent
49 write will not fail due to lack of space. Typically, this will be done
50 by performing a copy-on-write operation on all shared data in the file.
51 This flag may not be supported by all filesystems.
52
53 Because allocation is done in block size chunks, fallocate() may allo‐
54 cate a larger range of disk space than was specified.
55
56 Deallocating file space
57 Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38)
58 in mode deallocates space (i.e., creates a hole) in the byte range
59 starting at offset and continuing for len bytes. Within the specified
60 range, partial filesystem blocks are zeroed, and whole filesystem
61 blocks are removed from the file. After a successful call, subsequent
62 reads from this range will return zeroes.
63
64 The FALLOC_FL_PUNCH_HOLE flag must be ORed with FALLOC_FL_KEEP_SIZE in
65 mode; in other words, even when punching off the end of the file, the
66 file size (as reported by stat(2)) does not change.
67
68 Not all filesystems support FALLOC_FL_PUNCH_HOLE; if a filesystem
69 doesn't support the operation, an error is returned. The operation is
70 supported on at least the following filesystems:
71
72 * XFS (since Linux 2.6.38)
73
74 * ext4 (since Linux 3.0)
75
76 * Btrfs (since Linux 3.7)
77
78 * tmpfs(5) (since Linux 3.5)
79
80 Collapsing file space
81 Specifying the FALLOC_FL_COLLAPSE_RANGE flag (available since Linux
82 3.15) in mode removes a byte range from a file, without leaving a hole.
83 The byte range to be collapsed starts at offset and continues for len
84 bytes. At the completion of the operation, the contents of the file
85 starting at the location offset+len will be appended at the location
86 offset, and the file will be len bytes smaller.
87
88 A filesystem may place limitations on the granularity of the operation,
89 in order to ensure efficient implementation. Typically, offset and len
90 must be a multiple of the filesystem logical block size, which varies
91 according to the filesystem type and configuration. If a filesystem
92 has such a requirement, fallocate() fails with the error EINVAL if this
93 requirement is violated.
94
95 If the region specified by offset plus len reaches or passes the end of
96 file, an error is returned; instead, use ftruncate(2) to truncate a
97 file.
98
99 No other flags may be specified in mode in conjunction with FAL‐
100 LOC_FL_COLLAPSE_RANGE.
101
102 As at Linux 3.15, FALLOC_FL_COLLAPSE_RANGE is supported by ext4 (only
103 for extent-based files) and XFS.
104
105 Zeroing file space
106 Specifying the FALLOC_FL_ZERO_RANGE flag (available since Linux 3.15)
107 in mode zeroes space in the byte range starting at offset and continu‐
108 ing for len bytes. Within the specified range, blocks are preallocated
109 for the regions that span the holes in the file. After a successful
110 call, subsequent reads from this range will return zeroes.
111
112 Zeroing is done within the filesystem preferably by converting the
113 range into unwritten extents. This approach means that the specified
114 range will not be physically zeroed out on the device (except for par‐
115 tial blocks at the either end of the range), and I/O is (otherwise)
116 required only to update metadata.
117
118 If the FALLOC_FL_KEEP_SIZE flag is additionally specified in mode, the
119 behavior of the call is similar, but the file size will not be changed
120 even if offset+len is greater than the file size. This behavior is the
121 same as when preallocating space with FALLOC_FL_KEEP_SIZE specified.
122
123 Not all filesystems support FALLOC_FL_ZERO_RANGE; if a filesystem
124 doesn't support the operation, an error is returned. The operation is
125 supported on at least the following filesystems:
126
127 * XFS (since Linux 3.15)
128
129 * ext4, for extent-based files (since Linux 3.15)
130
131 * SMB3 (since Linux 3.17)
132
133 Increasing file space
134 Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1)
135 in mode increases the file space by inserting a hole within the file
136 size without overwriting any existing data. The hole will start at
137 offset and continue for len bytes. When inserting the hole inside
138 file, the contents of the file starting at offset will be shifted
139 upward (i.e., to a higher file offset) by len bytes. Inserting a hole
140 inside a file increases the file size by len bytes.
141
142 This mode has the same limitations as FALLOC_FL_COLLAPSE_RANGE regard‐
143 ing the granularity of the operation. If the granularity requirements
144 are not met, fallocate() fails with the error EINVAL. If the offset is
145 equal to or greater than the end of file, an error is returned. For
146 such operations (i.e., inserting a hole at the end of file), ftrun‐
147 cate(2) should be used.
148
149 No other flags may be specified in mode in conjunction with FAL‐
150 LOC_FL_INSERT_RANGE.
151
152 FALLOC_FL_INSERT_RANGE requires filesystem support. Filesystems that
153 support this operation include XFS (since Linux 4.1) and ext4 (since
154 Linux 4.2).
155
157 On success, fallocate() returns zero. On error, -1 is returned and
158 errno is set to indicate the error.
159
161 EBADF fd is not a valid file descriptor, or is not opened for writing.
162
163 EFBIG offset+len exceeds the maximum file size.
164
165 EFBIG mode is FALLOC_FL_INSERT_RANGE, and the current file size+len
166 exceeds the maximum file size.
167
168 EINTR A signal was caught during execution; see signal(7).
169
170 EINVAL offset was less than 0, or len was less than or equal to 0.
171
172 EINVAL mode is FALLOC_FL_COLLAPSE_RANGE and the range specified by off‐
173 set plus len reaches or passes the end of the file.
174
175 EINVAL mode is FALLOC_FL_INSERT_RANGE and the range specified by offset
176 reaches or passes the end of the file.
177
178 EINVAL mode is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE, but
179 either offset or len is not a multiple of the filesystem block
180 size.
181
182 EINVAL mode contains one of FALLOC_FL_COLLAPSE_RANGE or FAL‐
183 LOC_FL_INSERT_RANGE and also other flags; no other flags are
184 permitted with FALLOC_FL_COLLAPSE_RANGE or FAL‐
185 LOC_FL_INSERT_RANGE.
186
187 EINVAL mode is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_ZERO_RANGE or FAL‐
188 LOC_FL_INSERT_RANGE, but the file referred to by fd is not a
189 regular file.
190
191 EIO An I/O error occurred while reading from or writing to a
192 filesystem.
193
194 ENODEV fd does not refer to a regular file or a directory. (If fd is a
195 pipe or FIFO, a different error results.)
196
197 ENOSPC There is not enough space left on the device containing the file
198 referred to by fd.
199
200 ENOSYS This kernel does not implement fallocate().
201
202 EOPNOTSUPP
203 The filesystem containing the file referred to by fd does not
204 support this operation; or the mode is not supported by the
205 filesystem containing the file referred to by fd.
206
207 EPERM The file referred to by fd is marked immutable (see chattr(1)).
208
209 EPERM mode specifies FALLOC_FL_PUNCH_HOLE or FALLOC_FL_COLLAPSE_RANGE
210 or FALLOC_FL_INSERT_RANGE and the file referred to by fd is
211 marked append-only (see chattr(1)).
212
213 EPERM The operation was prevented by a file seal; see fcntl(2).
214
215 ESPIPE fd refers to a pipe or FIFO.
216
217 ETXTBSY
218 mode specifies FALLOC_FL_COLLAPSE_RANGE or FAL‐
219 LOC_FL_INSERT_RANGE, but the file referred to by fd is currently
220 being executed.
221
223 fallocate() is available on Linux since kernel 2.6.23. Support is pro‐
224 vided by glibc since version 2.10. The FALLOC_FL_* flags are defined
225 in glibc headers only since version 2.18.
226
228 fallocate() is Linux-specific.
229
231 fallocate(1), ftruncate(2), posix_fadvise(3), posix_fallocate(3)
232
234 This page is part of release 4.15 of the Linux man-pages project. A
235 description of the project, information about reporting bugs, and the
236 latest version of this page, can be found at
237 https://www.kernel.org/doc/man-pages/.
238
239
240
241Linux 2017-09-15 FALLOCATE(2)