mount_setattr.2: ffix
[man-pages.git] / man3 / posix_fallocate.3
blob0946c5bc0e0abdec290fc304e9f992f58fe4c543
1 .\" Copyright (c) 2006, Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH POSIX_FALLOCATE 3  2021-03-22 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 posix_fallocate \- allocate file space
28 .SH SYNOPSIS
29 .nf
30 .B #include <fcntl.h>
31 .PP
32 .BI "int posix_fallocate(int " fd ", off_t " offset ", off_t " len );
33 .fi
34 .PP
35 .ad l
36 .RS -4
37 Feature Test Macro Requirements for glibc (see
38 .BR feature_test_macros (7)):
39 .RE
40 .PP
41 .BR posix_fallocate ():
42 .nf
43     _POSIX_C_SOURCE >= 200112L
44 .fi
45 .SH DESCRIPTION
46 The function
47 .BR posix_fallocate ()
48 ensures that disk space is allocated for the file referred to by the
49 file descriptor
50 .I fd
51 for the bytes in the range starting at
52 .I offset
53 and continuing for
54 .I len
55 bytes.
56 After a successful call to
57 .BR posix_fallocate (),
58 subsequent writes to bytes in the specified range are
59 guaranteed not to fail because of lack of disk space.
60 .PP
61 If the size of the file is less than
62 .IR offset + len ,
63 then the file is increased to this size;
64 otherwise the file size is left unchanged.
65 .SH RETURN VALUE
66 .BR posix_fallocate ()
67 returns zero on success, or an error number on failure.
68 Note that
69 .I errno
70 is not set.
71 .SH ERRORS
72 .TP
73 .B EBADF
74 .I fd
75 is not a valid file descriptor, or is not opened for writing.
76 .TP
77 .B EFBIG
78 .I offset+len
79 exceeds the maximum file size.
80 .TP
81 .B EINTR
82 A signal was caught during execution.
83 .TP
84 .B EINVAL
85 .I offset
86 was less than 0, or
87 .I len
88 was less than or equal to 0, or the underlying filesystem does not
89 support the operation.
90 .TP
91 .B ENODEV
92 .I fd
93 does not refer to a regular file.
94 .TP
95 .B ENOSPC
96 There is not enough space left on the device containing the file
97 referred to by
98 .IR fd .
99 .TP
100 .B EOPNOTSUPP
101 The filesystem containing the file referred to by
102 .I fd
103 does not support this operation.
104 This error code can be returned by C libraries that don't perform the
105 emulation shown in NOTES, such as musl libc.
107 .B ESPIPE
108 .I fd
109 refers to a pipe.
110 .SH VERSIONS
111 .BR posix_fallocate ()
112 is available since glibc 2.1.94.
113 .SH ATTRIBUTES
114 For an explanation of the terms used in this section, see
115 .BR attributes (7).
116 .ad l
119 allbox;
120 lb lb lbx
121 l l l.
122 Interface       Attribute       Value
124 .BR posix_fallocate ()
125 T}      Thread safety   T{
126 MT-Safe (but see NOTES)
131 .sp 1
132 .SH CONFORMING TO
133 POSIX.1-2001.
135 POSIX.1-2008 says that an implementation
136 .I shall
137 give the
138 .B EINVAL
139 error if
140 .I len
141 was 0, or
142 .I offset
143 was less than 0.
144 POSIX.1-2001 says that an implementation
145 .I shall
146 give the
147 .B EINVAL
148 error if
149 .I len
150 is less than 0, or
151 .I offset
152 was less than 0, and
153 .I may
154 give the error if
155 .I len
156 equals zero.
157 .SH NOTES
158 In the glibc implementation,
159 .BR posix_fallocate ()
160 is implemented using the
161 .BR fallocate (2)
162 system call, which is MT-safe.
163 If the underlying filesystem does not support
164 .BR fallocate (2),
165 then the operation is emulated with the following caveats:
166 .IP * 2
167 The emulation is inefficient.
168 .IP *
169 There is a race condition where concurrent writes from another thread or
170 process could be overwritten with null bytes.
171 .IP *
172 There is a race condition where concurrent file size increases by
173 another thread or process could result in a file whose size is smaller
174 than expected.
175 .IP *
177 .I fd
178 has been opened with the
179 .B O_APPEND
181 .B O_WRONLY
182 flags, the function fails with the error
183 .BR EBADF .
185 In general, the emulation is not MT-safe.
186 On Linux, applications may use
187 .BR fallocate (2)
188 if they cannot tolerate the emulation caveats.
189 In general, this is
190 only recommended if the application plans to terminate the operation if
191 .B EOPNOTSUPP
192 is returned, otherwise the application itself will need to implement a
193 fallback with all the same problems as the emulation provided by glibc.
194 .SH SEE ALSO
195 .BR fallocate (1),
196 .BR fallocate (2),
197 .BR lseek (2),
198 .BR posix_fadvise (2)