POSIX_FALLOCATE(2)

πŸ’– POSIX_FALLOCATE(2) πŸ’–

okay so bestie, welcome to the System Calls Manual, section 2 β€” this is where the kernel-adjacent girlies live, and today we're doing posix_fallocate 🌸


✨ NAME ✨

posix_fallocate β€” pre-allocate storage for a range in a file 🫢


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) β€” she's built in, no extra shopping required πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <fcntl.h>

int
posix_fallocate(int fd, off_t offset, off_t len);

🌈 DESCRIPTION 🌈

So here's the tea, bestie: the required storage for the range from offset to offset + len in the file that fd points at is guaranteed to be allocated the moment this call returns successfully. ✨

That means β€” if posix_fallocate() comes back slaying, any writes you do later to that file data are not gonna fail because the file system storage media ran out of free space. The reservation is locked in, no cap. πŸ”’

Any file data that already lives in the specified range? Untouched, unbothered, exactly as it was. πŸ’–

And if offset + len goes past the current file size, then posix_fallocate() will adjust the file size to offset + len. Otherwise, the file size stays exactly the same β€” no sneaky resizing. 🌸

Space that posix_fallocate() allocated gets freed by a successful call to creat(2) or open(2) that truncates the size of the file. 🧚

Also β€” space allocated via posix_fallocate() may be freed by a successful call to ftruncate(2) that reduces the file size to something smaller than offset + len. πŸ‘‘


⭐ RETURN VALUES ⭐

  • Success? She returns zero. That's the slay. πŸ’…
  • Failure? She returns an error, and β€” plot twist β€” she does not set errno. 😭
bestie, read that again. no errno on failure. the return value IS the error code. do not go checking errno, she will not be there for you. πŸ™ˆ

πŸ’€ ERRORS πŸ’€

Possible failure conditions, aka the vibe-ruiners:

  • [EBADF] β€” the fd argument is not a valid file descriptor. πŸ’€
  • [EBADF] β€” the fd argument references a file that was opened without write permission. (same error code, different crime ✨)
  • [EFBIG] β€” the value of offset + len is greater than the maximum file size. too big bestie 😭
  • [EINTR] β€” a signal was caught during execution. rude interruption fr fr πŸ™ˆ
  • [EINVAL] β€” the len argument was less than or equal to zero, the offset argument was less than zero, or the operation is not supported by the file system. (three-for-one special πŸ’…)
  • [EIO] β€” an I/O error occurred while reading from or writing to a file system. 🫠
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system. πŸ’€
  • [ENODEV] β€” the fd argument does not refer to a regular file. not a regular file? not a regular girlie. 🚫
  • [ENOSPC] β€” there is insufficient free space remaining on the file system storage media. the disk said "no <3" 😭
  • [ENOTCAPABLE] β€” the file descriptor fd has insufficient rights. πŸŽ€
  • [ESPIPE] β€” the fd argument is associated with a pipe or FIFO. wrong kind of thing entirely, bestie πŸ¦‹

πŸ‘€ SEE ALSO πŸ‘€

creat(2), ftruncate(2), open(2), unlink(2) β€” the friend group 🫢


πŸ“œ STANDARDS πŸ“œ

The posix_fallocate() system call conforms to IEEE Std 1003.1-2004 (β€œPOSIX.1”). certified iconic, periodt. πŸ‘‘


πŸ¦‹ HISTORY πŸ¦‹

The posix_fallocate() function appeared in FreeBSD 9.0. a whole era. ✨


πŸ’œ AUTHORS πŸ’œ

posix_fallocate() and this manual page were initially written by Matthew Fleming <mdf@FreeBSD.org>. thank you for your service, king πŸ«‘πŸ’–


and that's the whole page, bestie!! this lil Debian build of the page is dated March 30, 2020 β€” yeah she's a vintage, she's still slaying, she's still canon πŸŒΈπŸ’… mwah, go reserve some disk space responsibly πŸŽ€βœ¨

πŸ“¦ where this page comes from