1POSIX_FALLOCATE(3) Linux Programmer's Manual POSIX_FALLOCATE(3)
2
3
4
6 posix_fallocate - allocate file space
7
9 #include <fcntl.h>
10
11 int posix_fallocate(int fd, off_t offset, off_t len);
12
13 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15 posix_fallocate():
16 _POSIX_C_SOURCE >= 200112L
17
19 The function posix_fallocate() ensures that disk space is allocated for
20 the file referred to by the file descriptor fd for the bytes in the
21 range starting at offset and continuing for len bytes. After a suc‐
22 cessful call to posix_fallocate(), subsequent writes to bytes in the
23 specified range are guaranteed not to fail because of lack of disk
24 space.
25
26 If the size of the file is less than offset+len, then the file is in‐
27 creased to this size; otherwise the file size is left unchanged.
28
30 posix_fallocate() returns zero on success, or an error number on fail‐
31 ure. Note that errno is not set.
32
34 EBADF fd is not a valid file descriptor, or is not opened for writing.
35
36 EFBIG offset+len exceeds the maximum file size.
37
38 EINTR A signal was caught during execution.
39
40 EINVAL offset was less than 0, or len was less than or equal to 0, or
41 the underlying filesystem does not support the operation.
42
43 ENODEV fd does not refer to a regular file.
44
45 ENOSPC There is not enough space left on the device containing the file
46 referred to by fd.
47
48 EOPNOTSUPP
49 The filesystem containing the file referred to by fd does not
50 support this operation. This error code can be returned by C
51 libraries that don't perform the emulation shown in NOTES, such
52 as musl libc.
53
54 ESPIPE fd refers to a pipe.
55
57 posix_fallocate() is available since glibc 2.1.94.
58
60 For an explanation of the terms used in this section, see at‐
61 tributes(7).
62
63 ┌──────────────────┬───────────────┬─────────────────────────┐
64 │Interface │ Attribute │ Value │
65 ├──────────────────┼───────────────┼─────────────────────────┤
66 │posix_fallocate() │ Thread safety │ MT-Safe (but see NOTES) │
67 └──────────────────┴───────────────┴─────────────────────────┘
69 POSIX.1-2001.
70
71 POSIX.1-2008 says that an implementation shall give the EINVAL error if
72 len was 0, or offset was less than 0. POSIX.1-2001 says that an imple‐
73 mentation shall give the EINVAL error if len is less than 0, or offset
74 was less than 0, and may give the error if len equals zero.
75
77 In the glibc implementation, posix_fallocate() is implemented using the
78 fallocate(2) system call, which is MT-safe. If the underlying filesys‐
79 tem does not support fallocate(2), then the operation is emulated with
80 the following caveats:
81
82 * The emulation is inefficient.
83
84 * There is a race condition where concurrent writes from another thread
85 or process could be overwritten with null bytes.
86
87 * There is a race condition where concurrent file size increases by an‐
88 other thread or process could result in a file whose size is smaller
89 than expected.
90
91 * If fd has been opened with the O_APPEND or O_WRONLY flags, the func‐
92 tion fails with the error EBADF.
93
94 In general, the emulation is not MT-safe. On Linux, applications may
95 use fallocate(2) if they cannot tolerate the emulation caveats. In
96 general, this is only recommended if the application plans to terminate
97 the operation if EOPNOTSUPP is returned, otherwise the application it‐
98 self will need to implement a fallback with all the same problems as
99 the emulation provided by glibc.
100
102 fallocate(1), fallocate(2), lseek(2), posix_fadvise(2)
103
105 This page is part of release 5.10 of the Linux man-pages project. A
106 description of the project, information about reporting bugs, and the
107 latest version of this page, can be found at
108 https://www.kernel.org/doc/man-pages/.
109
110
111
112GNU 2020-11-01 POSIX_FALLOCATE(3)