1posix_fallocate(3) Library Functions Manual posix_fallocate(3)
2
3
4
6 posix_fallocate - allocate file space
7
9 Standard C library (libc, -lc)
10
12 #include <fcntl.h>
13
14 int posix_fallocate(int fd, off_t offset, off_t len);
15
16 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
17
18 posix_fallocate():
19 _POSIX_C_SOURCE >= 200112L
20
22 The function posix_fallocate() ensures that disk space is allocated for
23 the file referred to by the file descriptor fd for the bytes in the
24 range starting at offset and continuing for len bytes. After a suc‐
25 cessful call to posix_fallocate(), subsequent writes to bytes in the
26 specified range are guaranteed not to fail because of lack of disk
27 space.
28
29 If the size of the file is less than offset+len, then the file is in‐
30 creased to this size; otherwise the file size is left unchanged.
31
33 posix_fallocate() returns zero on success, or an error number on fail‐
34 ure. Note that errno is not set.
35
37 EBADF fd is not a valid file descriptor, or is not opened for writing.
38
39 EFBIG offset+len exceeds the maximum file size.
40
41 EINTR A signal was caught during execution.
42
43 EINVAL offset was less than 0, or len was less than or equal to 0, or
44 the underlying filesystem does not support the operation.
45
46 ENODEV fd does not refer to a regular file.
47
48 ENOSPC There is not enough space left on the device containing the file
49 referred to by fd.
50
51 EOPNOTSUPP
52 The filesystem containing the file referred to by fd does not
53 support this operation. This error code can be returned by C
54 libraries that don't perform the emulation shown in NOTES, such
55 as musl libc.
56
57 ESPIPE fd refers to a pipe.
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 └──────────────────┴───────────────┴───────────────────────────────────┘
68
70 POSIX.1-2008.
71
73 glibc 2.1.94. POSIX.1-2001
74
75 POSIX.1-2008 says that an implementation shall give the EINVAL error if
76 len was 0, or offset was less than 0. POSIX.1-2001 says that an imple‐
77 mentation shall give the EINVAL error if len is less than 0, or offset
78 was less than 0, and may give the error if len equals zero.
79
81 In the glibc implementation, posix_fallocate() is implemented using the
82 fallocate(2) system call, which is MT-safe. If the underlying filesys‐
83 tem does not support fallocate(2), then the operation is emulated with
84 the following caveats:
85
86 • The emulation is inefficient.
87
88 • There is a race condition where concurrent writes from another
89 thread or process could be overwritten with null bytes.
90
91 • There is a race condition where concurrent file size increases by
92 another thread or process could result in a file whose size is
93 smaller than expected.
94
95 • If fd has been opened with the O_APPEND or O_WRONLY flags, the func‐
96 tion fails with the error EBADF.
97
98 In general, the emulation is not MT-safe. On Linux, applications may
99 use fallocate(2) if they cannot tolerate the emulation caveats. In
100 general, this is only recommended if the application plans to terminate
101 the operation if EOPNOTSUPP is returned, otherwise the application it‐
102 self will need to implement a fallback with all the same problems as
103 the emulation provided by glibc.
104
106 fallocate(1), fallocate(2), lseek(2), posix_fadvise(2)
107
108
109
110Linux man-pages 6.04 2023-03-30 posix_fallocate(3)