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 in mode in conjunction with FALLOC_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 in mode in conjunction with FALLOC_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 in mode in conjunction with FALLOC_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 โ€” fd is not a valid file descriptor, or is not opened for writing.
  • EFBIG โ€” offset+size exceeds the maximum file size.
  • EFBIG โ€” mode is FALLOC_FL_INSERT_RANGE, and the current file size+len exceeds the maximum file size.
  • EINTR โ€” A signal was caught during execution; see signal(7).
  • EINVAL โ€” offset was less than 0, or size was less than or equal to 0.
  • EINVAL โ€” mode is FALLOC_FL_COLLAPSE_RANGE and the range specified by offset plus size reaches or passes the end of the file.
  • EINVAL โ€” mode is FALLOC_FL_INSERT_RANGE and the range specified by offset reaches or passes the end of the file.
  • EINVAL โ€” mode is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE, but either offset or size is not a multiple of the filesystem block size.
  • EINVAL โ€” mode contains one of FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE and also other flags; no other flags are permitted with FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE.
  • EINVAL โ€” mode is FALLOC_FL_COLLAPSE_RANGE, FALLOC_FL_ZERO_RANGE, FALLOC_FL_WRITE_ZEROES, or FALLOC_FL_INSERT_RANGE, but the file referred to by fd is not a regular file.
  • EIO โ€” An I/O error occurred while reading from or writing to a filesystem.
  • ENODEV โ€” fd does not refer to a regular file or a directory. (If fd is a pipe or FIFO, a different error results.)
  • ENOSPC โ€” There is not enough space left on the device containing the file referred to by fd.
  • ENOSYS โ€” This kernel does not implement fallocate().
  • EOPNOTSUPP โ€” The filesystem containing the file referred to by fd does not support this operation; or the mode is not supported by the filesystem containing the file referred to by fd.
  • EPERM โ€” The file referred to by fd is marked immutable (see chattr(1)).
  • EPERM โ€” mode specifies FALLOC_FL_PUNCH_HOLE, FALLOC_FL_COLLAPSE_RANGE, or FALLOC_FL_INSERT_RANGE and the file referred to by fd is marked append-only (see chattr(1)).
  • EPERM โ€” The operation was prevented by a file seal; see fcntl(2).
  • ESPIPE โ€” fd refers to a pipe or FIFO.
  • ETXTBSY โ€” mode specifies FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE, but the file referred to by fd is 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms