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: errno is not set. she returns the error number directly, she does not do the errno thing. do not check errno. i'm so serious. ๐Ÿ˜ญ

๐Ÿ’€ ERRORS

  • EBADF โ€” fd is not a valid file descriptor, or it's not opened for writing. (rude.)
  • EFBIG โ€” offset+size exceeds the maximum file size. that's too much, babe.
  • EINTR โ€” a signal was caught during execution. ๐Ÿ™ˆ
  • EINVAL โ€” offset was less than 0, or size was less than or equal to 0, or the underlying filesystem just does not support this operation. nope, not supported, moving on.
  • ENODEV โ€” fd does 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 by fd. the disk said "no" ๐Ÿ’€
  • EOPNOTSUPP โ€” the filesystem containing the file referred to by fd does 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 โ€” fd refers 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 EINVAL error if size was 0, or offset was less than 0.
  • POSIX.1-2001 says that an implementation shall give the EINVAL error if size is less than 0, or offset was less than 0, and may give the error if size equals 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 fd has been opened with the O_APPEND or O_WRONLY flags, the function fails with the error EBADF.

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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms