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 zeros.
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 zeros space in the byte range starting at offset and continuing
108 for len bytes. Within the specified range, blocks are preallocated for
109 the regions that span the holes in the file. After a successful call,
110 subsequent reads from this range will return zeros.
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 * Btrfs (since Linux 4.16)
134
135 Increasing file space
136 Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1)
137 in mode increases the file space by inserting a hole within the file
138 size without overwriting any existing data. The hole will start at
139 offset and continue for len bytes. When inserting the hole inside
140 file, the contents of the file starting at offset will be shifted
141 upward (i.e., to a higher file offset) by len bytes. Inserting a hole
142 inside a file increases the file size by len bytes.
143
144 This mode has the same limitations as FALLOC_FL_COLLAPSE_RANGE regard‐
145 ing the granularity of the operation. If the granularity requirements
146 are not met, fallocate() fails with the error EINVAL. If the offset is
147 equal to or greater than the end of file, an error is returned. For
148 such operations (i.e., inserting a hole at the end of file), ftrun‐
149 cate(2) should be used.
150
151 No other flags may be specified in mode in conjunction with FAL‐
152 LOC_FL_INSERT_RANGE.
153
154 FALLOC_FL_INSERT_RANGE requires filesystem support. Filesystems that
155 support this operation include XFS (since Linux 4.1) and ext4 (since
156 Linux 4.2).
157
159 On success, fallocate() returns zero. On error, -1 is returned and
160 errno is set to indicate the error.
161
163 EBADF fd is not a valid file descriptor, or is not opened for writing.
164
165 EFBIG offset+len exceeds the maximum file size.
166
167 EFBIG mode is FALLOC_FL_INSERT_RANGE, and the current file size+len
168 exceeds the maximum file size.
169
170 EINTR A signal was caught during execution; see signal(7).
171
172 EINVAL offset was less than 0, or len was less than or equal to 0.
173
174 EINVAL mode is FALLOC_FL_COLLAPSE_RANGE and the range specified by off‐
175 set plus len reaches or passes the end of the file.
176
177 EINVAL mode is FALLOC_FL_INSERT_RANGE and the range specified by offset
178 reaches or passes the end of the file.
179
180 EINVAL mode is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE, but
181 either offset or len is not a multiple of the filesystem block
182 size.
183
184 EINVAL mode contains one of FALLOC_FL_COLLAPSE_RANGE or FAL‐
185 LOC_FL_INSERT_RANGE and also other flags; no other flags are
186 permitted with FALLOC_FL_COLLAPSE_RANGE or FAL‐
187 LOC_FL_INSERT_RANGE.
188
189 EINVAL mode is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_ZERO_RANGE or FAL‐
190 LOC_FL_INSERT_RANGE, but the file referred to by fd is not a
191 regular file.
192
193 EIO An I/O error occurred while reading from or writing to a
194 filesystem.
195
196 ENODEV fd does not refer to a regular file or a directory. (If fd is a
197 pipe or FIFO, a different error results.)
198
199 ENOSPC There is not enough space left on the device containing the file
200 referred to by fd.
201
202 ENOSYS This kernel does not implement fallocate().
203
204 EOPNOTSUPP
205 The filesystem containing the file referred to by fd does not
206 support this operation; or the mode is not supported by the
207 filesystem containing the file referred to by fd.
208
209 EPERM The file referred to by fd is marked immutable (see chattr(1)).
210
211 EPERM mode specifies FALLOC_FL_PUNCH_HOLE or FALLOC_FL_COLLAPSE_RANGE
212 or FALLOC_FL_INSERT_RANGE and the file referred to by fd is
213 marked append-only (see chattr(1)).
214
215 EPERM The operation was prevented by a file seal; see fcntl(2).
216
217 ESPIPE fd refers to a pipe or FIFO.
218
219 ETXTBSY
220 mode specifies FALLOC_FL_COLLAPSE_RANGE or FAL‐
221 LOC_FL_INSERT_RANGE, but the file referred to by fd is currently
222 being executed.
223
225 fallocate() is available on Linux since kernel 2.6.23. Support is pro‐
226 vided by glibc since version 2.10. The FALLOC_FL_* flags are defined
227 in glibc headers only since version 2.18.
228
230 fallocate() is Linux-specific.
231
233 fallocate(1), ftruncate(2), posix_fadvise(3), posix_fallocate(3)
234
236 This page is part of release 4.16 of the Linux man-pages project. A
237 description of the project, information about reporting bugs, and the
238 latest version of this page, can be found at
239 https://www.kernel.org/doc/man-pages/.
240
241
242
243Linux 2018-04-30 FALLOCATE(2)