]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/fallocate.2
549d6db73f80288759a4eea6515c579308c9a79b
[thirdparty/man-pages.git] / man2 / fallocate.2
1 .\" Copyright (c) 2007 Silicon Graphics, Inc. All Rights Reserved
2 .\" Written by Dave Chinner <dgc@sgi.com>
3 .\"
4 .\" %%%LICENSE_START(GPLv2_ONELINE)
5 .\" May be distributed as per GNU General Public License version 2.
6 .\" %%%LICENSE_END
7 .\"
8 .\" 2011-09-19: Added FALLOC_FL_PUNCH_HOLE
9 .\" 2011-09-19: Substantial restructuring of the page
10 .\"
11 .TH FALLOCATE 2 2018-04-30 "Linux" "Linux Programmer's Manual"
12 .SH NAME
13 fallocate \- manipulate file space
14 .SH SYNOPSIS
15 .nf
16 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
17 .B #include <fcntl.h>
18 .PP
19 .BI "int fallocate(int " fd ", int " mode ", off_t " offset \
20 ", off_t " len ");
21 .fi
22 .SH DESCRIPTION
23 This is a nonportable, Linux-specific system call.
24 For the portable, POSIX.1-specified method of ensuring that space
25 is allocated for a file, see
26 .BR posix_fallocate (3).
27 .PP
28 .BR fallocate ()
29 allows the caller to directly manipulate the allocated disk space
30 for the file referred to by
31 .I fd
32 for the byte range starting at
33 .I offset
34 and continuing for
35 .I len
36 bytes.
37 .PP
38 The
39 .I mode
40 argument determines the operation to be performed on the given range.
41 Details of the supported operations are given in the subsections below.
42 .SS Allocating disk space
43 The default operation (i.e.,
44 .I mode
45 is zero) of
46 .BR fallocate ()
47 allocates the disk space within the range specified by
48 .I offset
49 and
50 .IR len .
51 The file size (as reported by
52 .BR stat (2))
53 will be changed if
54 .IR offset + len
55 is greater than the file size.
56 Any subregion within the range specified by
57 .I offset
58 and
59 .IR len
60 that did not contain data before the call will be initialized to zero.
61 This default behavior closely resembles the behavior of the
62 .BR posix_fallocate (3)
63 library function,
64 and is intended as a method of optimally implementing that function.
65 .PP
66 After a successful call, subsequent writes into the range specified by
67 .IR offset
68 and
69 .IR len
70 are guaranteed not to fail because of lack of disk space.
71 .PP
72 If the
73 .B FALLOC_FL_KEEP_SIZE
74 flag is specified in
75 .IR mode ,
76 the behavior of the call is similar,
77 but the file size will not be changed even if
78 .IR offset + len
79 is greater than the file size.
80 Preallocating zeroed blocks beyond the end of the file in this manner
81 is useful for optimizing append workloads.
82 .PP
83 If the
84 .B FALLOC_FL_UNSHARE
85 flag is specified in
86 .IR mode ,
87 shared file data extents will be made private to the file to guarantee
88 that a subsequent write will not fail due to lack of space.
89 Typically, this will be done by performing a copy-on-write operation on
90 all shared data in the file.
91 This flag may not be supported by all filesystems.
92 .PP
93 Because allocation is done in block size chunks,
94 .BR fallocate ()
95 may allocate a larger range of disk space than was specified.
96 .SS Deallocating file space
97 Specifying the
98 .BR FALLOC_FL_PUNCH_HOLE
99 flag (available since Linux 2.6.38) in
100 .I mode
101 deallocates space (i.e., creates a hole)
102 in the byte range starting at
103 .I offset
104 and continuing for
105 .I len
106 bytes.
107 Within the specified range, partial filesystem blocks are zeroed,
108 and whole filesystem blocks are removed from the file.
109 After a successful call,
110 subsequent reads from this range will return zeros.
111 .PP
112 The
113 .BR FALLOC_FL_PUNCH_HOLE
114 flag must be ORed with
115 .BR FALLOC_FL_KEEP_SIZE
116 in
117 .IR mode ;
118 in other words, even when punching off the end of the file, the file size
119 (as reported by
120 .BR stat (2))
121 does not change.
122 .PP
123 Not all filesystems support
124 .BR FALLOC_FL_PUNCH_HOLE ;
125 if a filesystem doesn't support the operation, an error is returned.
126 The operation is supported on at least the following filesystems:
127 .IP * 3
128 XFS (since Linux 2.6.38)
129 .IP *
130 ext4 (since Linux 3.0)
131 .\" commit a4bb6b64e39abc0e41ca077725f2a72c868e7622
132 .IP *
133 Btrfs (since Linux 3.7)
134 .IP *
135 .BR tmpfs (5)
136 (since Linux 3.5)"
137 .\" commit 83e4fa9c16e4af7122e31be3eca5d57881d236fe
138 .SS Collapsing file space
139 .\" commit 00f5e61998dd17f5375d9dfc01331f104b83f841
140 Specifying the
141 .BR FALLOC_FL_COLLAPSE_RANGE
142 flag (available since Linux 3.15) in
143 .I mode
144 removes a byte range from a file, without leaving a hole.
145 The byte range to be collapsed starts at
146 .I offset
147 and continues for
148 .I len
149 bytes.
150 At the completion of the operation,
151 the contents of the file starting at the location
152 .I offset+len
153 will be appended at the location
154 .IR offset ,
155 and the file will be
156 .I len
157 bytes smaller.
158 .PP
159 A filesystem may place limitations on the granularity of the operation,
160 in order to ensure efficient implementation.
161 Typically,
162 .I offset
163 and
164 .I len
165 must be a multiple of the filesystem logical block size,
166 which varies according to the filesystem type and configuration.
167 If a filesystem has such a requirement,
168 .BR fallocate ()
169 fails with the error
170 .BR EINVAL
171 if this requirement is violated.
172 .PP
173 If the region specified by
174 .I offset
175 plus
176 .I len
177 reaches or passes the end of file, an error is returned;
178 instead, use
179 .BR ftruncate (2)
180 to truncate a file.
181 .PP
182 No other flags may be specified in
183 .IR mode
184 in conjunction with
185 .BR FALLOC_FL_COLLAPSE_RANGE .
186 .PP
187 As at Linux 3.15,
188 .B FALLOC_FL_COLLAPSE_RANGE
189 is supported by
190 ext4 (only for extent-based files)
191 .\" commit 9eb79482a97152930b113b51dff530aba9e28c8e
192 and XFS.
193 .\" commit e1d8fb88a64c1f8094b9f6c3b6d2d9e6719c970d
194 .SS Zeroing file space
195 Specifying the
196 .BR FALLOC_FL_ZERO_RANGE
197 flag (available since Linux 3.15)
198 .\" commit 409332b65d3ed8cfa7a8030f1e9d52f372219642
199 in
200 .I mode
201 zeros space in the byte range starting at
202 .I offset
203 and continuing for
204 .I len
205 bytes.
206 Within the specified range, blocks are preallocated for the regions
207 that span the holes in the file.
208 After a successful call, subsequent
209 reads from this range will return zeros.
210 .PP
211 Zeroing is done within the filesystem preferably by converting the range into
212 unwritten extents.
213 This approach means that the specified range will not be physically zeroed
214 out on the device (except for partial blocks at the either end of the range),
215 and I/O is (otherwise) required only to update metadata.
216 .PP
217 If the
218 .B FALLOC_FL_KEEP_SIZE
219 flag is additionally specified in
220 .IR mode ,
221 the behavior of the call is similar,
222 but the file size will not be changed even if
223 .IR offset + len
224 is greater than the file size.
225 This behavior is the same as when preallocating space with
226 .B FALLOC_FL_KEEP_SIZE
227 specified.
228 .PP
229 Not all filesystems support
230 .BR FALLOC_FL_ZERO_RANGE ;
231 if a filesystem doesn't support the operation, an error is returned.
232 The operation is supported on at least the following filesystems:
233 .IP * 3
234 XFS (since Linux 3.15)
235 .\" commit 376ba313147b4172f3e8cf620b9fb591f3e8cdfa
236 .IP *
237 ext4, for extent-based files (since Linux 3.15)
238 .\" commit b8a8684502a0fc852afa0056c6bb2a9273f6fcc0
239 .IP *
240 SMB3 (since Linux 3.17)
241 .\" commit 30175628bf7f521e9ee31ac98fa6d6fe7441a556
242 .IP *
243 Btrfs (since Linux 4.16)
244 .\" commit f27451f229966874a8793995b8e6b74326d125df
245 .SS Increasing file space
246 Specifying the
247 .BR FALLOC_FL_INSERT_RANGE
248 flag
249 (available since Linux 4.1)
250 .\" commit dd46c787788d5bf5b974729d43e4c405814a4c7d
251 in
252 .I mode
253 increases the file space by inserting a hole within the file size without
254 overwriting any existing data.
255 The hole will start at
256 .I offset
257 and continue for
258 .I len
259 bytes.
260 When inserting the hole inside file, the contents of the file starting at
261 .I offset
262 will be shifted upward (i.e., to a higher file offset) by
263 .I len
264 bytes.
265 Inserting a hole inside a file increases the file size by
266 .I len
267 bytes.
268 .PP
269 This mode has the same limitations as
270 .BR FALLOC_FL_COLLAPSE_RANGE
271 regarding the granularity of the operation.
272 If the granularity requirements are not met,
273 .BR fallocate ()
274 fails with the error
275 .BR EINVAL.
276 If the
277 .I offset
278 is equal to or greater than the end of file, an error is returned.
279 For such operations (i.e., inserting a hole at the end of file),
280 .BR ftruncate (2)
281 should be used.
282 .PP
283 No other flags may be specified in
284 .IR mode
285 in conjunction with
286 .BR FALLOC_FL_INSERT_RANGE .
287 .PP
288 .B FALLOC_FL_INSERT_RANGE
289 requires filesystem support.
290 Filesystems that support this operation include
291 XFS (since Linux 4.1)
292 .\" commit a904b1ca5751faf5ece8600e18cd3b674afcca1b
293 and ext4 (since Linux 4.2).
294 .\" commit 331573febb6a224bc50322e3670da326cb7f4cfc
295 .\" f2fs also has support since Linux 4.2
296 .\" commit f62185d0e283e9d311e3ac1020f159d95f0aab39
297 .SH RETURN VALUE
298 On success,
299 .BR fallocate ()
300 returns zero.
301 On error, \-1 is returned and
302 .I errno
303 is set to indicate the error.
304 .SH ERRORS
305 .TP
306 .B EBADF
307 .I fd
308 is not a valid file descriptor, or is not opened for writing.
309 .TP
310 .B EFBIG
311 .IR offset + len
312 exceeds the maximum file size.
313 .TP
314 .B EFBIG
315 .I mode
316 is
317 .BR FALLOC_FL_INSERT_RANGE ,
318 and the current file size+\fIlen\fP exceeds the maximum file size.
319 .TP
320 .B EINTR
321 A signal was caught during execution; see
322 .BR signal (7).
323 .TP
324 .B EINVAL
325 .I offset
326 was less than 0, or
327 .I len
328 .\" FIXME . (raise a kernel bug) Probably the len==0 case should be
329 .\" a no-op, rather than an error. That would be consistent with
330 .\" similar APIs for the len==0 case.
331 .\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition"
332 .\" 21 Sep 2012
333 .\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526
334 was less than or equal to 0.
335 .TP
336 .B EINVAL
337 .I mode
338 is
339 .BR FALLOC_FL_COLLAPSE_RANGE
340 and the range specified by
341 .I offset
342 plus
343 .I len
344 reaches or passes the end of the file.
345 .TP
346 .B EINVAL
347 .I mode
348 is
349 .BR FALLOC_FL_INSERT_RANGE
350 and the range specified by
351 .I offset
352 reaches or passes the end of the file.
353 .TP
354 .B EINVAL
355 .I mode
356 is
357 .BR FALLOC_FL_COLLAPSE_RANGE
358 or
359 .BR FALLOC_FL_INSERT_RANGE ,
360 but either
361 .I offset
362 or
363 .I len
364 is not a multiple of the filesystem block size.
365 .TP
366 .B EINVAL
367 .I mode
368 contains one of
369 .B FALLOC_FL_COLLAPSE_RANGE
370 or
371 .B FALLOC_FL_INSERT_RANGE
372 and also other flags;
373 no other flags are permitted with
374 .BR FALLOC_FL_COLLAPSE_RANGE
375 or
376 .BR FALLOC_FL_INSERT_RANGE .
377 .TP
378 .B EINVAL
379 .I mode
380 is
381 .BR FALLOC_FL_COLLAPSE_RANGE
382 or
383 .BR FALLOC_FL_ZERO_RANGE
384 or
385 .BR FALLOC_FL_INSERT_RANGE ,
386 but the file referred to by
387 .I fd
388 is not a regular file.
389 .\" There was an inconsistency in 3.15-rc1, that should be resolved so that all
390 .\" filesystems use this error for this case. (Tytso says ex4 will change.)
391 .\" http://thread.gmane.org/gmane.comp.file-systems.xfs.general/60485/focus=5521
392 .\" From: Michael Kerrisk (man-pages <mtk.manpages@...>
393 .\" Subject: Re: [PATCH v5 10/10] manpage: update FALLOC_FL_COLLAPSE_RANGE flag in fallocate
394 .\" Newsgroups: gmane.linux.man, gmane.linux.file-systems
395 .\" Date: 2014-04-17 13:40:05 GMT
396 .TP
397 .B EIO
398 An I/O error occurred while reading from or writing to a filesystem.
399 .TP
400 .B ENODEV
401 .I fd
402 does not refer to a regular file or a directory.
403 (If
404 .I fd
405 is a pipe or FIFO, a different error results.)
406 .TP
407 .B ENOSPC
408 There is not enough space left on the device containing the file
409 referred to by
410 .IR fd .
411 .TP
412 .B ENOSYS
413 This kernel does not implement
414 .BR fallocate ().
415 .TP
416 .B EOPNOTSUPP
417 The filesystem containing the file referred to by
418 .I fd
419 does not support this operation;
420 or the
421 .I mode
422 is not supported by the filesystem containing the file referred to by
423 .IR fd .
424 .TP
425 .B EPERM
426 The file referred to by
427 .I fd
428 is marked immutable (see
429 .BR chattr (1)).
430 .TP
431 .B EPERM
432 .I mode
433 specifies
434 .BR FALLOC_FL_PUNCH_HOLE
435 or
436 .BR FALLOC_FL_COLLAPSE_RANGE
437 or
438 .BR FALLOC_FL_INSERT_RANGE
439 and
440 the file referred to by
441 .I fd
442 is marked append-only
443 (see
444 .BR chattr (1)).
445 .TP
446 .B EPERM
447 The operation was prevented by a file seal; see
448 .BR fcntl (2).
449 .TP
450 .B ESPIPE
451 .I fd
452 refers to a pipe or FIFO.
453 .TP
454 .B ETXTBSY
455 .I mode
456 specifies
457 .BR FALLOC_FL_COLLAPSE_RANGE
458 or
459 .BR FALLOC_FL_INSERT_RANGE ,
460 but the file referred to by
461 .IR fd
462 is currently being executed.
463 .SH VERSIONS
464 .BR fallocate ()
465 is available on Linux since kernel 2.6.23.
466 Support is provided by glibc since version 2.10.
467 The
468 .BR FALLOC_FL_*
469 flags are defined in glibc headers only since version 2.18.
470 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964
471 .SH CONFORMING TO
472 .BR fallocate ()
473 is Linux-specific.
474 .SH SEE ALSO
475 .BR fallocate (1),
476 .BR ftruncate (2),
477 .BR posix_fadvise (3),
478 .BR posix_fallocate (3)