posix_fallocate(), posix_fallocate64()

Updated: April 19, 2023

Allocate space for a file on the filesystem

Synopsis:

#include <fcntl.h>

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

int posix_fallocate64( int fd,
                       off64_t offset,
                       off64_t len );

Arguments:

fd
A file descriptor for the file you want to allocate space for.
offset
The offset into the file where you want to allocate space.
len
The number of bytes to allocate.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The posix_fallocate() and posix_fallocate64() functions ensure that any required storage for regular file data starting at offset and continuing for len bytes is allocated on the filesystem storage media. The posix_fallocate64() function is a large-file support version of posix_fallocate().

Note: In QNX Neutrino 6.6 or later, the large-file support functions and data types appear in the name space only if you define _LARGEFILE64_SOURCE when you compile your code. For more information, see Classification in What's in a Function Description?

If posix_fallocate() returns successfully, subsequent writes to the specified file data won't fail due to the lack of free space on the filesystem storage media.

If the offset + len is beyond the current file size, then posix_fallocate() adjusts the file size to be offset + len. Otherwise, the file size isn't changed.

You can free the allocated space by using creat() or open() to truncate the file, or by calling ftruncate() to make the size of the file less than offset + len.

Returns:

EOK
Success.
EBADF
The fd argument isn't a valid file descriptor.
EBADF
The fd argument references a file that was opened without write permission.
EFBIG
The value of offset + len is greater than the maximum file size.
EINTR
A signal was caught during execution.
EINVAL
The len or offset argument is less than zero, or the underlying filesystem doesn't support this operation.
EIO
An I/O error occurred while reading from or writing to a filesystem.
ENODEV
The fd argument doesn't refer to a regular file.
ENOSPC
There's insufficient free space remaining on the filesystem storage media.
ESPIPE
The fd argument is associated with a pipe, FIFO, or socket.

Classification:

posix_fallocate() is POSIX 1003.1 ADV; posix_fallocate64() is Large-file support

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes