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