1 .\" Copyright (c) 2007 Silicon Graphics, Inc. All Rights Reserved
2 .\" Written by Dave Chinner <dgc@sgi.com>
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" May be distributed as per GNU General Public License version 2.
8 .\" 2011-09-19: Added FALLOC_FL_PUNCH_HOLE
9 .\" 2011-09-19: Substantial restructuring of the page
11 .TH FALLOCATE 2 2016-12-12 "Linux" "Linux Programmer's Manual"
13 fallocate \- manipulate file space
16 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
19 .BI "int fallocate(int " fd ", int " mode ", off_t " offset \
23 This is a nonportable, Linux-specific system call.
24 For the portable, POSIX.1-specified method of ensuring that space
25 is allocated for a file, see
26 .BR posix_fallocate (3).
29 allows the caller to directly manipulate the allocated disk space
30 for the file referred to by
32 for the byte range starting at
40 argument determines the operation to be performed on the given range.
41 Details of the supported operations are given in the subsections below.
42 .SS Allocating disk space
43 The default operation (i.e.,
47 allocates the disk space within the range specified by
51 The file size (as reported by
55 is greater than the file size.
56 Any subregion within the range specified by
60 that did not contain data before the call will be initialized to zero.
61 This default behavior closely resembles the behavior of the
62 .BR posix_fallocate (3)
64 and is intended as a method of optimally implementing that function.
66 After a successful call, subsequent writes into the range specified by
70 are guaranteed not to fail because of lack of disk space.
73 .B FALLOC_FL_KEEP_SIZE
76 the behavior of the call is similar,
77 but the file size will not be changed even if
79 is greater than the file size.
80 Preallocating zeroed blocks beyond the end of the file in this manner
81 is useful for optimizing append workloads.
87 shared file data extents will be made private to the file to guarantee
88 that a subsequent write will not fail due to lack of space.
89 Typically, this will be done by performing a copy-on-write operation on
90 all shared data in the file.
91 This flag may not be supported by all filesystems.
93 Because allocation is done in block size chunks,
95 may allocate a larger range of disk space than was specified.
96 .SS Deallocating file space
98 .BR FALLOC_FL_PUNCH_HOLE
99 flag (available since Linux 2.6.38) in
101 deallocates space (i.e., creates a hole)
102 in the byte range starting at
107 Within the specified range, partial filesystem blocks are zeroed,
108 and whole filesystem blocks are removed from the file.
109 After a successful call,
110 subsequent reads from this range will return zeroes.
113 .BR FALLOC_FL_PUNCH_HOLE
114 flag must be ORed with
115 .BR FALLOC_FL_KEEP_SIZE
118 in other words, even when punching off the end of the file, the file size
123 Not all filesystems support
124 .BR FALLOC_FL_PUNCH_HOLE ;
125 if a filesystem doesn't support the operation, an error is returned.
126 The operation is supported on at least the following filesystems:
128 XFS (since Linux 2.6.38)
130 ext4 (since Linux 3.0)
131 .\" commit a4bb6b64e39abc0e41ca077725f2a72c868e7622
133 Btrfs (since Linux 3.7)
135 .BR tmpfs "(5) (since Linux 3.5)"
136 .\" commit 83e4fa9c16e4af7122e31be3eca5d57881d236fe
137 .SS Collapsing file space
138 .\" commit 00f5e61998dd17f5375d9dfc01331f104b83f841
140 .BR FALLOC_FL_COLLAPSE_RANGE
141 flag (available since Linux 3.15) in
143 removes a byte range from a file, without leaving a hole.
144 The byte range to be collapsed starts at
149 At the completion of the operation,
150 the contents of the file starting at the location
152 will be appended at the location
158 A filesystem may place limitations on the granularity of the operation,
159 in order to ensure efficient implementation.
164 must be a multiple of the filesystem logical block size,
165 which varies according to the filesystem type and configuration.
166 If a filesystem has such a requirement,
168 will fail with the error
170 if this requirement is violated.
172 If the region specified by
176 reaches or passes the end of file, an error is returned;
181 No other flags may be specified in
184 .BR FALLOC_FL_COLLAPSE_RANGE .
187 .B FALLOC_FL_COLLAPSE_RANGE
189 ext4 (only for extent-based files)
190 .\" commit 9eb79482a97152930b113b51dff530aba9e28c8e
192 .\" commit e1d8fb88a64c1f8094b9f6c3b6d2d9e6719c970d
193 .SS Zeroing file space
195 .BR FALLOC_FL_ZERO_RANGE
196 flag (available since Linux 3.15)
197 .\" commit 409332b65d3ed8cfa7a8030f1e9d52f372219642
200 zeroes space in the byte range starting at
205 Within the specified range, blocks are preallocated for the regions
206 that span the holes in the file.
207 After a successful call, subsequent
208 reads from this range will return zeroes.
210 Zeroing is done within the filesystem preferably by converting the range into
212 This approach means that the specified range will not be physically zeroed
213 out on the device (except for partial blocks at the either end of the range),
214 and I/O is (otherwise) required only to update metadata.
217 .B FALLOC_FL_KEEP_SIZE
218 flag is additionally specified in
220 the behavior of the call is similar,
221 but the file size will not be changed even if
223 is greater than the file size.
224 This behavior is the same as when preallocating space with
225 .B FALLOC_FL_KEEP_SIZE
228 Not all filesystems support
229 .BR FALLOC_FL_ZERO_RANGE ;
230 if a filesystem doesn't support the operation, an error is returned.
231 The operation is supported on at least the following filesystems:
233 XFS (since Linux 3.15)
234 .\" commit 376ba313147b4172f3e8cf620b9fb591f3e8cdfa
236 ext4, for extent-based files (since Linux 3.15)
237 .\" commit b8a8684502a0fc852afa0056c6bb2a9273f6fcc0
239 SMB3 (since Linux 3.17)
240 .\" commit 30175628bf7f521e9ee31ac98fa6d6fe7441a556
241 .SS Increasing file space
243 .BR FALLOC_FL_INSERT_RANGE
245 (available since Linux 4.1)
246 .\" commit dd46c787788d5bf5b974729d43e4c405814a4c7d
249 increases the file space by inserting a hole within the file size without
250 overwriting any existing data.
251 The hole will start at
256 When inserting the hole inside file, the contents of the file starting at
258 will be shifted upward (i.e., to a higher file offset) by
261 Inserting a hole inside a file increases the file size by
265 This mode has the same limitations as
266 .BR FALLOC_FL_COLLAPSE_RANGE
267 regarding the granularity of the operation.
268 If the granularity requirements are not met,
270 will fail with the error
274 is equal to or greater than the end of file, an error is returned.
275 For such operations (i.e., inserting a hole at the end of file),
279 No other flags may be specified in
282 .BR FALLOC_FL_INSERT_RANGE .
284 .B FALLOC_FL_INSERT_RANGE
285 requires filesystem support.
286 Filesystems that support this operation include
287 XFS (since Linux 4.1)
288 .\" commit a904b1ca5751faf5ece8600e18cd3b674afcca1b
289 and ext4 (since Linux 4.2).
290 .\" commit 331573febb6a224bc50322e3670da326cb7f4cfc
291 .\" f2fs also has support since Linux 4.2
292 .\" commit f62185d0e283e9d311e3ac1020f159d95f0aab39
297 On error, \-1 is returned and
299 is set to indicate the error.
304 is not a valid file descriptor, or is not opened for writing.
308 exceeds the maximum file size.
313 .BR FALLOC_FL_INSERT_RANGE ,
314 and the current file size+\fIlen\fP exceeds the maximum file size.
317 A signal was caught during execution; see
324 .\" FIXME . (raise a kernel bug) Probably the len==0 case should be
325 .\" a no-op, rather than an error. That would be consistent with
326 .\" similar APIs for the len==0 case.
327 .\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition"
329 .\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526
330 was less than or equal to 0.
335 .BR FALLOC_FL_COLLAPSE_RANGE
336 and the range specified by
340 reaches or passes the end of the file.
345 .BR FALLOC_FL_INSERT_RANGE
346 and the range specified by
348 reaches or passes the end of the file.
353 .BR FALLOC_FL_COLLAPSE_RANGE
355 .BR FALLOC_FL_INSERT_RANGE ,
360 is not a multiple of the filesystem block size.
365 .B FALLOC_FL_COLLAPSE_RANGE
367 .B FALLOC_FL_INSERT_RANGE
368 and also other flags;
369 no other flags are permitted with
370 .BR FALLOC_FL_COLLAPSE_RANGE
372 .BR FALLOC_FL_INSERT_RANGE .
377 .BR FALLOC_FL_COLLAPSE_RANGE
379 .BR FALLOC_FL_ZERO_RANGE
381 .BR FALLOC_FL_INSERT_RANGE ,
382 but the file referred to by
384 is not a regular file.
385 .\" There was a inconsistency in 3.15-rc1, that should be resolved so that all
386 .\" filesystems use this error for this case. (Tytso says ex4 will change.)
387 .\" http://thread.gmane.org/gmane.comp.file-systems.xfs.general/60485/focus=5521
388 .\" From: Michael Kerrisk (man-pages <mtk.manpages@...>
389 .\" Subject: Re: [PATCH v5 10/10] manpage: update FALLOC_FL_COLLAPSE_RANGE flag in fallocate
390 .\" Newsgroups: gmane.linux.man, gmane.linux.file-systems
391 .\" Date: 2014-04-17 13:40:05 GMT
394 An I/O error occurred while reading from or writing to a filesystem.
398 does not refer to a regular file or a directory.
401 is a pipe or FIFO, a different error results.)
404 There is not enough space left on the device containing the file
409 This kernel does not implement
413 The filesystem containing the file referred to by
415 does not support this operation;
418 is not supported by the filesystem containing the file referred to by
422 The file referred to by
424 is marked immutable (see
430 .BR FALLOC_FL_PUNCH_HOLE
432 .BR FALLOC_FL_COLLAPSE_RANGE
434 .BR FALLOC_FL_INSERT_RANGE
436 the file referred to by
438 is marked append-only
443 The operation was prevented by a file seal; see
448 refers to a pipe or FIFO.
453 .BR FALLOC_FL_COLLAPSE_RANGE
455 .BR FALLOC_FL_INSERT_RANGE ,
456 but the file referred to by
458 is currently being executed.
461 is available on Linux since kernel 2.6.23.
462 Support is provided by glibc since version 2.10.
465 flags are defined in glibc headers only since version 2.18.
466 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964
473 .BR posix_fadvise (3),
474 .BR posix_fallocate (3)