fallocate(2)
๐ fallocate(2) ๐
okay bestie, welcome to fallocate(2) โ that's section 2 of the system calls manual, iykyk ๐ซถ this one lives in the System Calls Manual and it's literally the girlboss of file space manipulation. no cap.
โจ NAME โจ
fallocate โ manipulate file space ๐
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐
๐ธ SYNOPSIS ๐ธ
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <fcntl.h>
int fallocate(int fd, int mode, off_t offset, off_t size);
like look at her. she's gorgeous. she's #define _GNU_SOURCE coded. ๐ญ
๐ DESCRIPTION ๐
So listen. This is a nonportable, Linux-specific system call โ she's not like other girls, she does NOT travel well. If you want the portable, POSIX.1-specified way of making sure space is allocated for a file, go check out posix_fallocate(3) instead bestie.
fallocate() lets the caller directly manipulate the allocated disk space for the file referred to by fd, for the byte range starting at offset and continuing for size bytes. periodt.
The mode argument decides which operation gets performed on the given range. all the deets on the supported operations are in the subsections below, so scroll, babe ๐
๐ Allocating disk space ๐
The default operation โ i.e., when mode is zero โ of fallocate() allocates the disk space within the range specified by offset and size.
The file size (as reported by stat(2)) will be changed if offset+size is greater than the file size.
Any subregion within the range specified by offset and size that didn't contain data before the call gets initialized to zero. This default behavior is like, so close to the behavior of the posix_fallocate(3) library function, and it's intended as a method of optimally implementing that function. iconic.
After a successful call, subsequent writes into the range specified by offset and size are guaranteed not to fail because of lack of disk space. no more panic spirals. ๐
If the FALLOC_FL_KEEP_SIZE flag is specified in mode, the behavior of the call is similar, but the file size will not be changed even if offset+size is greater than the file size. Preallocating zeroed blocks beyond the end of the file in this manner is useful for optimizing append workloads. slay.
If the FALLOC_FL_UNSHARE_RANGE flag is specified in mode, shared file data extents get made private to the file, to guarantee that a subsequent write will not fail due to lack of space. Typically this gets done by performing a copy-on-write operation on all shared data in the file. This flag may not be supported by all filesystems โ so like, vibe check your fs first.
Because allocation is done in block size chunks, fallocate() may allocate a larger range of disk space than was specified. she's generous like that. ๐
๐ณ๏ธ Deallocating file space ๐ณ๏ธ
Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38) in mode deallocates space โ i.e., creates a hole โ in the byte range starting at offset and continuing for size bytes.
Within the specified range, partial filesystem blocks are zeroed, and whole filesystem blocks are removed from the file. After a successful call, subsequent reads from this range will return zeros. it's giving nothing. literally. ๐ธ
The FALLOC_FL_PUNCH_HOLE flag must be ORed with FALLOC_FL_KEEP_SIZE in mode; in other words, even when punching off the end of the file, the file size (as reported by stat(2)) does not change.
Not all filesystems support FALLOC_FL_PUNCH_HOLE; if a filesystem doesn't support the operation, an error is returned. The operation is supported on at least the following filesystems:
- XFS (since Linux 2.6.38)
- ext4 (since Linux 3.0)
- Btrfs (since Linux 3.7)
tmpfs(5)(since Linux 3.5)gfs2(5)(since Linux 4.16)
๐ซ Collapsing file space ๐ซ
Specifying the FALLOC_FL_COLLAPSE_RANGE flag (available since Linux 3.15) in mode removes a byte range from a file, without leaving a hole.
The byte range to be collapsed starts at offset and continues for size bytes. At the completion of the operation, the contents of the file starting at the location offset+size will be appended at the location offset, and the file will be size bytes smaller. she's shrinking, she's thriving ๐
A filesystem may place limitations on the granularity of the operation, in order to ensure efficient implementation. Typically, offset and size must be a multiple of the filesystem logical block size, which varies according to the filesystem type and configuration. If a filesystem has such a requirement, fallocate() fails with the error EINVAL if this requirement is violated. ๐ญ
If the region specified by offset plus size reaches or passes the end of file, an error is returned; instead, use ftruncate(2) to truncate a file.
๐จ No other flags may be specified inmodein conjunction withFALLOC_FL_COLLAPSE_RANGE.
As at Linux 3.15, FALLOC_FL_COLLAPSE_RANGE is supported by ext4 (only for extent-based files) and XFS.
0๏ธโฃ Zeroing file space 0๏ธโฃ
Specifying the FALLOC_FL_ZERO_RANGE flag (available since Linux 3.15) in mode zeros space in the byte range starting at offset and continuing for size bytes.
Within the specified range, blocks are preallocated for the regions that span the holes in the file. After a successful call, subsequent reads from this range will return zeros.
Zeroing is done within the filesystem, preferably by converting the range into unwritten extents. This approach means that the specified range will not be physically zeroed out on the device (except for partial blocks at the either end of the range), and I/O is (otherwise) required only to update metadata. efficient queen. โจ
If the FALLOC_FL_KEEP_SIZE flag is additionally specified in mode, the behavior of the call is similar, but the file size will not be changed even if offset+size is greater than the file size. This behavior is the same as when preallocating space with FALLOC_FL_KEEP_SIZE specified.
Not all filesystems support FALLOC_FL_ZERO_RANGE; if a filesystem doesn't support the operation, an error is returned. The operation is supported on at least the following filesystems:
- XFS (since Linux 3.15)
- ext4, for extent-based files (since Linux 3.15)
- SMB3 (since Linux 3.17)
- Btrfs (since Linux 4.16)
โ๏ธ Writing zeroes in file space โ๏ธ
Specifying the FALLOC_FL_WRITE_ZEROES flag (available since Linux 6.17) in mode physically zeros space in the byte range starting at offset and continuing for size bytes.
Within the specified range, blocks are preallocated for the regions that span the holes in the file. After a successful call, subsequent reads from this range will return zeros and subsequent writes to that range do not require further changes to the file mapping metadata. obsessed. ๐ฆ
Zeroing is done within the filesystem. The filesystem may use a hardware-accelerated zeroing command or may submit regular writes. The behavior depends on the filesystem design and the available hardware.
๐จ No other flags may be specified inmodein conjunction withFALLOC_FL_WRITE_ZEROES.
Not all filesystems support FALLOC_FL_WRITE_ZEROES; if a filesystem doesn't support the operation, an error is returned. The operation is supported only in ext4 (only for extent-based files) since Linux 6.17. that's it. that's the list. ๐
๐ Increasing file space ๐
Specifying the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1) in mode increases the file space by inserting a hole within the file size without overwriting any existing data.
The hole will start at offset and continue for size bytes. When inserting the hole inside the file, the contents of the file starting at offset get shifted upward (i.e., to a higher file offset) by size bytes. Inserting a hole inside a file increases the file size by size bytes. main character energy tbh ๐
This mode has the same limitations as FALLOC_FL_COLLAPSE_RANGE regarding the granularity of the operation. If the granularity requirements are not met, fallocate() fails with the error EINVAL. If the offset is equal to or greater than the end of file, an error is returned. For such operations (i.e., inserting a hole at the end of file), ftruncate(2) should be used.
๐จ No other flags may be specified inmodein conjunction withFALLOC_FL_INSERT_RANGE.
FALLOC_FL_INSERT_RANGE requires filesystem support. Filesystems that support this operation include XFS (since Linux 4.1) and ext4 (since Linux 4.2).
๐ซถ RETURN VALUE ๐ซถ
On success, fallocate() returns zero. ๐
On error, -1 is returned and errno is set to indicate the error. not the vibe but okay.
๐ ERRORS ๐
buckle up bestie, there's a lot of them. each one is a different flavor of "no".
EBADFโfdis not a valid file descriptor, or is not opened for writing.EFBIGโoffset+sizeexceeds the maximum file size.EFBIGโmodeisFALLOC_FL_INSERT_RANGE, and the current file size+lenexceeds the maximum file size.EINTRโ A signal was caught during execution; seesignal(7).EINVALโoffsetwas less than 0, orsizewas less than or equal to 0.EINVALโmodeisFALLOC_FL_COLLAPSE_RANGEand the range specified byoffsetplussizereaches or passes the end of the file.EINVALโmodeisFALLOC_FL_INSERT_RANGEand the range specified byoffsetreaches or passes the end of the file.EINVALโmodeisFALLOC_FL_COLLAPSE_RANGEorFALLOC_FL_INSERT_RANGE, but eitheroffsetorsizeis not a multiple of the filesystem block size.EINVALโmodecontains one ofFALLOC_FL_COLLAPSE_RANGEorFALLOC_FL_INSERT_RANGEand also other flags; no other flags are permitted withFALLOC_FL_COLLAPSE_RANGEorFALLOC_FL_INSERT_RANGE.EINVALโmodeisFALLOC_FL_COLLAPSE_RANGE,FALLOC_FL_ZERO_RANGE,FALLOC_FL_WRITE_ZEROES, orFALLOC_FL_INSERT_RANGE, but the file referred to byfdis not a regular file.EIOโ An I/O error occurred while reading from or writing to a filesystem.ENODEVโfddoes not refer to a regular file or a directory. (Iffdis a pipe or FIFO, a different error results.)ENOSPCโ There is not enough space left on the device containing the file referred to byfd.ENOSYSโ This kernel does not implementfallocate().EOPNOTSUPPโ The filesystem containing the file referred to byfddoes not support this operation; or themodeis not supported by the filesystem containing the file referred to byfd.EPERMโ The file referred to byfdis marked immutable (seechattr(1)).EPERMโmodespecifiesFALLOC_FL_PUNCH_HOLE,FALLOC_FL_COLLAPSE_RANGE, orFALLOC_FL_INSERT_RANGEand the file referred to byfdis marked append-only (seechattr(1)).EPERMโ The operation was prevented by a file seal; seefcntl(2).ESPIPEโfdrefers to a pipe or FIFO.ETXTBSYโmodespecifiesFALLOC_FL_COLLAPSE_RANGEorFALLOC_FL_INSERT_RANGE, but the file referred to byfdis currently being executed.
๐ STANDARDS ๐
Linux. that's the whole section. she said what she said. ๐
โญ HISTORY โญ
fallocate()โ Linux 2.6.23, glibc 2.10.FALLOC_FL_*โ glibc 2.18.
๐ฅ SEE ALSO ๐ฅ
fallocate(1), ftruncate(2), posix_fadvise(3), posix_fallocate(3)
and that's the whole page, bestie!! ๐ฅฐ go forth and allocate responsibly. this came to you courtesy of Linux man-pages 6.20, dated 2026-09-11 โ love her, follow her, read the source. ๐โจ๐๐ฆ๐ธ๐ ๐ซถ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: GPL-2.0-only
- Copyright 2007, Silicon Graphics, Inc
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms