1 .\" Copyright (c) 2007 Silicon Graphics, Inc. All Rights Reserved
2 .\" Written by Dave Chinner <dgc@sgi.com>
4 .\" SPDX-License-Identifier: GPL-2.0-only
6 .\" 2011-09-19: Added FALLOC_FL_PUNCH_HOLE
7 .\" 2011-09-19: Substantial restructuring of the page
9 .TH FALLOCATE 2 2022-09-09 "Linux man-pages (unreleased)"
11 fallocate \- manipulate file space
14 .RI ( libc ", " \-lc )
17 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
20 .BI "int fallocate(int " fd ", int " mode ", off_t " offset \
24 This is a nonportable, Linux-specific system call.
25 For the portable, POSIX.1-specified method of ensuring that space
26 is allocated for a file, see
27 .BR posix_fallocate (3).
30 allows the caller to directly manipulate the allocated disk space
31 for the file referred to by
33 for the byte range starting at
41 argument determines the operation to be performed on the given range.
42 Details of the supported operations are given in the subsections below.
43 .SS Allocating disk space
44 The default operation (i.e.,
48 allocates the disk space within the range specified by
52 The file size (as reported by
56 is greater than the file size.
57 Any subregion within the range specified by
61 that did not contain data before the call will be initialized to zero.
62 This default behavior closely resembles the behavior of the
63 .BR posix_fallocate (3)
65 and is intended as a method of optimally implementing that function.
67 After a successful call, subsequent writes into the range specified by
71 are guaranteed not to fail because of lack of disk space.
74 .B FALLOC_FL_KEEP_SIZE
77 the behavior of the call is similar,
78 but the file size will not be changed even if
80 is greater than the file size.
81 Preallocating zeroed blocks beyond the end of the file in this manner
82 is useful for optimizing append workloads.
85 .B FALLOC_FL_UNSHARE_RANGE
88 shared file data extents will be made private to the file to guarantee
89 that a subsequent write will not fail due to lack of space.
90 Typically, this will be done by performing a copy-on-write operation on
91 all shared data in the file.
92 This flag may not be supported by all filesystems.
94 Because allocation is done in block size chunks,
96 may allocate a larger range of disk space than was specified.
97 .SS Deallocating file space
99 .B FALLOC_FL_PUNCH_HOLE
100 flag (available since Linux 2.6.38) in
102 deallocates space (i.e., creates a hole)
103 in the byte range starting at
108 Within the specified range, partial filesystem blocks are zeroed,
109 and whole filesystem blocks are removed from the file.
110 After a successful call,
111 subsequent reads from this range will return zeros.
114 .B FALLOC_FL_PUNCH_HOLE
115 flag must be ORed with
116 .B FALLOC_FL_KEEP_SIZE
119 in other words, even when punching off the end of the file, the file size
124 Not all filesystems support
125 .BR FALLOC_FL_PUNCH_HOLE ;
126 if a filesystem doesn't support the operation, an error is returned.
127 The operation is supported on at least the following filesystems:
129 XFS (since Linux 2.6.38)
131 ext4 (since Linux 3.0)
132 .\" commit a4bb6b64e39abc0e41ca077725f2a72c868e7622
134 Btrfs (since Linux 3.7)
138 .\" commit 83e4fa9c16e4af7122e31be3eca5d57881d236fe
142 .\" commit 4e56a6411fbce6f859566e17298114c2434391a4
143 .SS Collapsing file space
144 .\" commit 00f5e61998dd17f5375d9dfc01331f104b83f841
146 .B FALLOC_FL_COLLAPSE_RANGE
147 flag (available since Linux 3.15) in
149 removes a byte range from a file, without leaving a hole.
150 The byte range to be collapsed starts at
155 At the completion of the operation,
156 the contents of the file starting at the location
158 will be appended at the location
164 A filesystem may place limitations on the granularity of the operation,
165 in order to ensure efficient implementation.
170 must be a multiple of the filesystem logical block size,
171 which varies according to the filesystem type and configuration.
172 If a filesystem has such a requirement,
176 if this requirement is violated.
178 If the region specified by
182 reaches or passes the end of file, an error is returned;
187 No other flags may be specified in
190 .BR FALLOC_FL_COLLAPSE_RANGE .
193 .B FALLOC_FL_COLLAPSE_RANGE
195 ext4 (only for extent-based files)
196 .\" commit 9eb79482a97152930b113b51dff530aba9e28c8e
198 .\" commit e1d8fb88a64c1f8094b9f6c3b6d2d9e6719c970d
199 .SS Zeroing file space
201 .B FALLOC_FL_ZERO_RANGE
202 flag (available since Linux 3.15)
203 .\" commit 409332b65d3ed8cfa7a8030f1e9d52f372219642
206 zeros space in the byte range starting at
211 Within the specified range, blocks are preallocated for the regions
212 that span the holes in the file.
213 After a successful call, subsequent
214 reads from this range will return zeros.
216 Zeroing is done within the filesystem preferably by converting the range into
218 This approach means that the specified range will not be physically zeroed
219 out on the device (except for partial blocks at the either end of the range),
220 and I/O is (otherwise) required only to update metadata.
223 .B FALLOC_FL_KEEP_SIZE
224 flag is additionally specified in
226 the behavior of the call is similar,
227 but the file size will not be changed even if
229 is greater than the file size.
230 This behavior is the same as when preallocating space with
231 .B FALLOC_FL_KEEP_SIZE
234 Not all filesystems support
235 .BR FALLOC_FL_ZERO_RANGE ;
236 if a filesystem doesn't support the operation, an error is returned.
237 The operation is supported on at least the following filesystems:
239 XFS (since Linux 3.15)
240 .\" commit 376ba313147b4172f3e8cf620b9fb591f3e8cdfa
242 ext4, for extent-based files (since Linux 3.15)
243 .\" commit b8a8684502a0fc852afa0056c6bb2a9273f6fcc0
245 SMB3 (since Linux 3.17)
246 .\" commit 30175628bf7f521e9ee31ac98fa6d6fe7441a556
248 Btrfs (since Linux 4.16)
249 .\" commit f27451f229966874a8793995b8e6b74326d125df
250 .SS Increasing file space
252 .B FALLOC_FL_INSERT_RANGE
254 (available since Linux 4.1)
255 .\" commit dd46c787788d5bf5b974729d43e4c405814a4c7d
258 increases the file space by inserting a hole within the file size without
259 overwriting any existing data.
260 The hole will start at
265 When inserting the hole inside file, the contents of the file starting at
267 will be shifted upward (i.e., to a higher file offset) by
270 Inserting a hole inside a file increases the file size by
274 This mode has the same limitations as
275 .B FALLOC_FL_COLLAPSE_RANGE
276 regarding the granularity of the operation.
277 If the granularity requirements are not met,
283 is equal to or greater than the end of file, an error is returned.
284 For such operations (i.e., inserting a hole at the end of file),
288 No other flags may be specified in
291 .BR FALLOC_FL_INSERT_RANGE .
293 .B FALLOC_FL_INSERT_RANGE
294 requires filesystem support.
295 Filesystems that support this operation include
296 XFS (since Linux 4.1)
297 .\" commit a904b1ca5751faf5ece8600e18cd3b674afcca1b
298 and ext4 (since Linux 4.2).
299 .\" commit 331573febb6a224bc50322e3670da326cb7f4cfc
300 .\" f2fs also has support since Linux 4.2
301 .\" commit f62185d0e283e9d311e3ac1020f159d95f0aab39
306 On error, \-1 is returned and
308 is set to indicate the error.
313 is not a valid file descriptor, or is not opened for writing.
317 exceeds the maximum file size.
322 .BR FALLOC_FL_INSERT_RANGE ,
323 and the current file size+\fIlen\fP exceeds the maximum file size.
326 A signal was caught during execution; see
333 .\" FIXME . (raise a kernel bug) Probably the len==0 case should be
334 .\" a no-op, rather than an error. That would be consistent with
335 .\" similar APIs for the len==0 case.
336 .\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition"
338 .\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526
339 was less than or equal to 0.
344 .B FALLOC_FL_COLLAPSE_RANGE
345 and the range specified by
349 reaches or passes the end of the file.
354 .B FALLOC_FL_INSERT_RANGE
355 and the range specified by
357 reaches or passes the end of the file.
362 .B FALLOC_FL_COLLAPSE_RANGE
364 .BR FALLOC_FL_INSERT_RANGE ,
369 is not a multiple of the filesystem block size.
374 .B FALLOC_FL_COLLAPSE_RANGE
376 .B FALLOC_FL_INSERT_RANGE
377 and also other flags;
378 no other flags are permitted with
379 .B FALLOC_FL_COLLAPSE_RANGE
381 .BR FALLOC_FL_INSERT_RANGE .
386 .BR FALLOC_FL_COLLAPSE_RANGE ,
387 .BR FALLOC_FL_ZERO_RANGE ,
389 .BR FALLOC_FL_INSERT_RANGE ,
390 but the file referred to by
392 is not a regular file.
393 .\" There was an inconsistency in 3.15-rc1, that should be resolved so that all
394 .\" filesystems use this error for this case. (Tytso says ex4 will change.)
395 .\" http://thread.gmane.org/gmane.comp.file-systems.xfs.general/60485/focus=5521
396 .\" From: Michael Kerrisk (man-pages <mtk.manpages@...>
397 .\" Subject: Re: [PATCH v5 10/10] manpage: update FALLOC_FL_COLLAPSE_RANGE flag in fallocate
398 .\" Newsgroups: gmane.linux.man, gmane.linux.file-systems
399 .\" Date: 2014-04-17 13:40:05 GMT
402 An I/O error occurred while reading from or writing to a filesystem.
406 does not refer to a regular file or a directory.
409 is a pipe or FIFO, a different error results.)
412 There is not enough space left on the device containing the file
417 This kernel does not implement
421 The filesystem containing the file referred to by
423 does not support this operation;
426 is not supported by the filesystem containing the file referred to by
430 The file referred to by
432 is marked immutable (see
438 .BR FALLOC_FL_PUNCH_HOLE ,
439 .BR FALLOC_FL_COLLAPSE_RANGE ,
441 .B FALLOC_FL_INSERT_RANGE
443 the file referred to by
445 is marked append-only
450 The operation was prevented by a file seal; see
455 refers to a pipe or FIFO.
460 .B FALLOC_FL_COLLAPSE_RANGE
462 .BR FALLOC_FL_INSERT_RANGE ,
463 but the file referred to by
465 is currently being executed.
468 is available on Linux since kernel 2.6.23.
469 Support is provided by glibc since version 2.10.
472 flags are defined in glibc headers only since version 2.18.
473 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964
480 .BR posix_fadvise (3),
481 .BR posix_fallocate (3)