posix_fallocate(3)
โจ posix_fallocate โ allocate file space โจ
okay bestie, welcome to the posix_fallocate(3) page ๐ this is a Linux man-pages classic, and we're gonna get you fluent. no cap.
๐ซถ NAME
posix_fallocate โ allocate file space ๐ธ
๐ LIBRARY
Standard C library (libc, -lc) โ yeah, the usual queen ๐
๐ SYNOPSIS
#include <fcntl.h>
int posix_fallocate(int fd, off_t offset, off_t size);
๐ง Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
posix_fallocate():_POSIX_C_SOURCE >= 200112L
yeah you gotta opt in, she doesn't just show up uninvited ๐
๐ DESCRIPTION
so here's the tea: posix_fallocate() makes sure that disk space is genuinely allocated for the file that fd (that file descriptor) is pointing at โ specifically for the bytes in the range that starts at offset and keeps going for size bytes. ๐ฆ
and here's the slay part: once posix_fallocate() returns successfully, any later writes to bytes inside that range are guaranteed not to fail because of lack of disk space. like, the space is reserved, it's hers, nobody can take it. periodt. ๐ฅ
if the file's size is less than offset+size, then the file gets increased to that size. otherwise? file size is left exactly as it was. unchanged. untouched. iconic behaviour honestly. โจ
๐ซ RETURN VALUE
posix_fallocate() returns zero on success, or an error number on failure.
โ ๏ธ okay pay attention bestie:errnois not set. she returns the error number directly, she does not do theerrnothing. do not checkerrno. i'm so serious. ๐ญ
๐ ERRORS
EBADFโfdis not a valid file descriptor, or it's not opened for writing. (rude.)EFBIGโoffset+sizeexceeds the maximum file size. that's too much, babe.EINTRโ a signal was caught during execution. ๐EINVALโoffsetwas less than0, orsizewas less than or equal to0, or the underlying filesystem just does not support this operation. nope, not supported, moving on.ENODEVโfddoes not refer to a regular file. she's special and she only does regular files ๐ENOSPCโ there is not enough space left on the device containing the file referred to byfd. the disk said "no" ๐EOPNOTSUPPโ the filesystem containing the file referred to byfddoes not support this operation. this error code can be returned by C libraries that don't perform the emulation shown in CAVEATS, such as musl libc. (musl said "we're not doing that" ๐)ESPIPEโfdrefers to a pipe. pipes don't get disk space allocated, that's not a thing ๐ธ
๐ ATTRIBUTES
for an explanation of the terms used in this section, see attributes(7). ๐
- Interface:
posix_fallocate() - Attribute: Thread safety
- Value: MT-Safe (but see CAVEATS)
โญ STANDARDS
POSIX.1-2008. that's the vibe check she passed. ๐
๐ฆ HISTORY
- glibc 2.1.94.
- POSIX.1-2001.
and here's the โจloreโจ on the error behaviour:
- POSIX.1-2008 says that an implementation shall give the
EINVALerror ifsizewas0, oroffsetwas less than0. - POSIX.1-2001 says that an implementation shall give the
EINVALerror ifsizeis less than0, oroffsetwas less than0, and may give the error ifsizeequals zero.
so basically the standards changed their minds a lil bit about the zero case. character development ๐
๐ CAVEATS
okay so this is the part where you sit down and drink some water, bestie. ๐
in the glibc implementation, posix_fallocate() is implemented using the fallocate(2) system call, which is MT-safe. good. great. lovely.
but โ if the underlying filesystem does not support fallocate(2), then the operation gets emulated, and that emulation comes with the following caveats:
- the emulation is inefficient. ๐ญ
- there is a race condition where concurrent writes from another thread or process could be overwritten with null bytes.
- there is a race condition where concurrent file size increases by another thread or process could result in a file whose size is smaller than expected.
- if
fdhas been opened with theO_APPENDorO_WRONLYflags, the function fails with the errorEBADF.
in general, the emulation is not MT-safe. ๐จ on Linux, applications may use fallocate(2) if they cannot tolerate the emulation caveats. in general, this is only recommended if the application plans to terminate the operation if EOPNOTSUPP is returned โ otherwise the application itself will need to implement a fallback with all the same problems as the emulation provided by glibc. so like... you're just rebuilding the same mess. okay. sure. ๐
โ ๏ธ fr fr, read this section twice. the emulation is doing its best but its best is lowkey chaotic.
๐ธ SEE ALSO
and that's the whole page, bestie!! ๐๐ฆ๐ go forth and allocate responsibly. you got this โจ๐
this was Linux man-pages 6.20, dated 2026-02-08 โ mwah, love u, bye!! ๐ ๐ซถ๐ธ
๐ฆ 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: Linux-man-pages-copyleft
- Copyright 2006, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms