fallocate(2) System Calls Manual fallocate(2) fallocate - C (libc, -lc) #define _GNU_SOURCE /* feature_test_macros(7) */ #include int fallocate(int fd, int mode, off_t offset, off_t size); , Linux. POSIX.1 , ( posix_fallocate(3)). fallocate() allows the caller to 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. mode , . . The default operation (i.e., 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 did not contain data before the call will be initialized to zero. This default behavior closely resembles the behavior of the posix_fallocate(3) library function, and is intended as a method of optimally implementing that function. 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. 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. mode FALLOC_FL_UNSHARE_RANGE, extent- , , - . , . . , fallocate() , . 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. FALLOC_FL_PUNCH_HOLE FALLOC_FL_KEEP_SIZE mode; , (punching) , ( stat(2)) . FALLOC_FL_PUNCH_HOLE; , . , , : o XFS ( Linux 2.6.38) o ext4 ( Linux 3.0) o Btrfs ( Linux 3.7) o tmpfs(5) ( Linux 3.5) o gfs2(5) ( Linux 4.16) (Collapsing) 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. 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. FALLOC_FL_COLLAPSE_RANGE mode . Linux 3.15 FALLOC_FL_COLLAPSE_RANGE ext4 ( extent) XFS. 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. , , , extents. , ( ), - . 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. FALLOC_FL_ZERO_RANGE; , . , , : o XFS ( Linux 3.15) o ext4, extent ( Linux 3.15) o SMB3 ( Linux 3.17) o Btrfs ( Linux 4.16) 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 file, the contents of the file starting at offset will be 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. FALLOC_FL_COLLAPSE_RANGE, , fallocate() EINVAL. offset , . (. ., ) ftruncate(2). FALLOC_FL_INSERT_RANGE mode . FALLOC_FL_INSERT_RANGE ; XFS ( Linux 4.1) ext4 ( Linux 4.2). fallocate() 0; -1, errno . EBADF fd . EFBIG offset+size exceeds the maximum file size. EFBIG mode FALLOC_FL_INSERT_RANGE, +len . EINTR ; 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 FALLOC_FL_INSERT_RANGE, , offset, . 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 FALLOC_FL_COLLAPSE_RANGE FALLOC_FL_INSERT_RANGE, ; FALLOC_FL_COLLAPSE_RANGE FALLOC_FL_INSERT_RANGE . EINVAL mode is FALLOC_FL_COLLAPSE_RANGE, FALLOC_FL_ZERO_RANGE, or FALLOC_FL_INSERT_RANGE, but the file referred to by fd is not a regular file. EIO -. ENODEV fd ( fd -- FIFO, ). ENOSPC , , fd. ENOSYS fallocate() . EOPNOTSUPP , fd, ; mode , , fd. EPERM , fd, (immutable) ( 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 (file seal); fcntl(2). ESPIPE fd FIFO. ETXTBSY mode FALLOC_FL_COLLAPSE_RANGE FALLOC_FL_INSERT_RANGE, , fd, . Linux. fallocate() Linux 2.6.23, glibc 2.10. FALLOC_FL_* glibc 2.18. fallocate(1), ftruncate(2), posix_fadvise(3), posix_fallocate(3) () Azamat Hackimov , Dmitry Bolkhovskikh , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.15 17 2025 . fallocate(2)