]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/fallocate.2
icmp.7: wfix
[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.\"
8392a3b3 11.TH FALLOCATE 2 2015-01-22 "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>
c8250206 18
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).
01b7704f 27
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.
37
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.
65
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.
f52e0be8
MK
71
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.
d9a0b2a5 82.PP
f52e0be8
MK
83Because allocation is done in block size chunks,
84.BR fallocate ()
85may allocate a larger range of disk space than was specified.
86.SS Deallocating file space
87Specifying the
88.BR FALLOC_FL_PUNCH_HOLE
89flag (available since Linux 2.6.38) in
90.I mode
91deallocates space (i.e., creates a hole)
96575bbc 92in the byte range starting at
1c7857e9
LAG
93.I offset
94and continuing for
95.I len
f52e0be8 96bytes.
9ee4a2b6
MK
97Within the specified range, partial filesystem blocks are zeroed,
98and whole filesystem blocks are removed from the file.
96575bbc
MK
99After a successful call,
100subsequent reads from this range will return zeroes.
f52e0be8 101
96575bbc 102The
1c7857e9 103.BR FALLOC_FL_PUNCH_HOLE
96575bbc 104flag must be ORed with
1c7857e9 105.BR FALLOC_FL_KEEP_SIZE
f52e0be8
MK
106in
107.IR mode ;
96575bbc
MK
108in other words, even when punching off the end of the file, the file size
109(as reported by
1c7857e9 110.BR stat (2))
96575bbc 111does not change.
f52e0be8 112
9ee4a2b6 113Not all filesystems support
f52e0be8 114.BR FALLOC_FL_PUNCH_HOLE ;
9ee4a2b6 115if a filesystem doesn't support the operation, an error is returned.
ce430f41 116The operation is supported on at least the following filesystems:
49995543
MK
117.IP * 3
118XFS (since Linux 2.6.38)
119.IP *
120ext4 (since Linux 3.0)
121.\" commit a4bb6b64e39abc0e41ca077725f2a72c868e7622
122.IP *
123Btrfs (since Linux 3.7)
124.IP *
125tmpfs (since Linux 3.5)
126.\" commit 83e4fa9c16e4af7122e31be3eca5d57881d236fe
ea56dddf 127.SS Collapsing file space
1ecfa5b3 128.\" commit 00f5e61998dd17f5375d9dfc01331f104b83f841
f78da076 129Specifying the
ea56dddf 130.BR FALLOC_FL_COLLAPSE_RANGE
1ecfa5b3 131flag (available since Linux 3.15) in
ea56dddf 132.I mode
1ecfa5b3
MK
133removes a byte range from a file, without leaving a hole.
134The byte range to be collapsed starts at
ea56dddf 135.I offset
1ecfa5b3 136and continues for
ea56dddf 137.I len
f78da076 138bytes.
1ecfa5b3
MK
139At the completion of the operation,
140the contents of the file starting at the location
141.I offset+len
142will be appended at the location
143.IR offset ,
144and the file will be
145.I len
146bytes smaller.
ea56dddf 147
1ecfa5b3
MK
148A filesystem may place limitations on the granularity of the operation,
149in order to ensure efficient implementation.
150Typically,
151.I offset
152and
153.I len
154must be a multiple of the filesystem logical block size,
155which varies according to the filesystem type and configuration.
156If a filesystem has such a requirement,
157.BR fallocate ()
158will fail with the error
159.BR EINVAL
160if this requirement is violated.
ea56dddf 161
1ecfa5b3
MK
162If the region specified by
163.I offset
164plus
165.I len
166reaches or passes the end of file, an error is returned;
167instead, use
168.BR ftruncate (2)
169to truncate a file.
170
171No other flags may be specified in
172.IR mode
173in conjunction with
174.BR FALLOC_FL_COLLAPSE_RANGE .
ea56dddf 175
1ecfa5b3
MK
176As at Linux 3.15,
177.B FALLOC_FL_COLLAPSE_RANGE
178is supported by
179ext4 (only for extent-based files)
180.\" commit 9eb79482a97152930b113b51dff530aba9e28c8e
181and XFS.
182.\" commit e1d8fb88a64c1f8094b9f6c3b6d2d9e6719c970d
7dc489e3 183.SS Zeroing file space
44e4132a 184Specifying the
7dc489e3 185.BR FALLOC_FL_ZERO_RANGE
44e4132a
MK
186flag (available since Linux 3.14)
187.\" commit 409332b65d3ed8cfa7a8030f1e9d52f372219642
188in
7dc489e3
LC
189.I mode
190zeroes space in the byte range starting at
191.I offset
192and continuing for
193.I len
194bytes.
195Within the specified range, blocks are preallocated for the regions
44e4132a
MK
196that span the holes in the file.
197After a successful call, subsequent
7dc489e3
LC
198reads from this range will return zeroes.
199
7c7960f8
MK
200Zeroing is done within the filesystem preferably by converting the range into
201unwritten extents.
202This approach means that the specified range will not be physically zeroed
4cb27742 203out on the device (except for partial blocks at the either end of the range),
b9ddebe2 204and I/O is (otherwise) required only to update metadata.
7dc489e3
LC
205
206If the
207.B FALLOC_FL_KEEP_SIZE
7c7960f8 208flag is additionally specified in
7dc489e3
LC
209.IR mode ,
210the behavior of the call is similar,
211but the file size will not be changed even if
212.IR offset + len
44e4132a 213is greater than the file size.
a1fa36af 214This behavior is the same as when preallocating space with
7dc489e3
LC
215.B FALLOC_FL_KEEP_SIZE
216specified.
217
218Not all filesystems support
219.BR FALLOC_FL_ZERO_RANGE ;
220if a filesystem doesn't support the operation, an error is returned.
44e4132a 221The operation is supported on at least the following filesystems:
7dc489e3 222.IP * 3
7c7960f8 223XFS (since Linux 3.14)
7dc489e3
LC
224.\" commit 376ba313147b4172f3e8cf620b9fb591f3e8cdfa
225.IP *
5737bfa0 226ext4, for extent-based files (since Linux 3.14)
7dc489e3 227.\" commit b8a8684502a0fc852afa0056c6bb2a9273f6fcc0
d9a0b2a5 228.SH RETURN VALUE
276939a6 229On success,
d9a0b2a5 230.BR fallocate ()
47a97908
MK
231returns zero.
232On error, \-1 is returned and
233.I errno
234is set to indicate the error.
d9a0b2a5
MK
235.SH ERRORS
236.TP
237.B EBADF
238.I fd
239is not a valid file descriptor, or is not opened for writing.
240.TP
241.B EFBIG
242.IR offset + len
243exceeds the maximum file size.
244.TP
245.B EINTR
246A signal was caught during execution.
247.TP
248.B EINVAL
249.I offset
250was less than 0, or
251.I len
bea08fec 252.\" FIXME . (raise a kernel bug) Probably the len==0 case should be
c0c3d019
MK
253.\" a no-op, rather than an error. That would be consistent with
254.\" similar APIs for the len==0 case.
255.\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition"
256.\" 21 Sep 2012
257.\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526
d9a0b2a5
MK
258was less than or equal to 0.
259.TP
1ecfa5b3
MK
260.B EINVAL
261.I mode
262is
263.BR FALLOC_FL_COLLAPSE_RANGE
264and the range specified by
265.I offset
266plus
267.I len
268reaches or passes the end of the file.
269.TP
270.B EINVAL
271.I mode
272is
273.BR FALLOC_FL_COLLAPSE_RANGE ,
274but either
275.I offset
276or
277.I len
278is not a multiple of the filesystem block size.
279.TP
280.B EINVAL
718778ad
SP
281.I mode
282contains both
1ecfa5b3
MK
283.B FALLOC_FL_COLLAPSE_RANGE
284and other flags;
285no other flags are permitted with
286.BR FALLOC_FL_COLLAPSE_RANGE .
718778ad
SP
287.TP
288.B EINVAL
2af4d967
MK
289.I mode
290is
7dc489e3
LC
291.BR FALLOC_FL_COLLAPSE_RANGE
292or
18c9919b 293.BR FALLOC_FL_ZERO_RANGE ,
2af4d967
MK
294but the file referred to by
295.I fd
296is not a regular file.
297.\" There was a inconsistency in 3.15-rc1, that should be resolved so that all
298.\" filesystems use this error for this case. (Tytso says ex4 will change.)
299.\" http://thread.gmane.org/gmane.comp.file-systems.xfs.general/60485/focus=5521
300.\" From: Michael Kerrisk (man-pages <mtk.manpages@...>
301.\" Subject: Re: [PATCH v5 10/10] manpage: update FALLOC_FL_COLLAPSE_RANGE flag in fallocate
302.\" Newsgroups: gmane.linux.man, gmane.linux.file-systems
303.\" Date: 2014-04-17 13:40:05 GMT
1ecfa5b3 304.TP
d9a0b2a5 305.B EIO
9ee4a2b6 306An I/O error occurred while reading from or writing to a filesystem.
d9a0b2a5
MK
307.TP
308.B ENODEV
309.I fd
310does not refer to a regular file or a directory.
311(If
312.I fd
313is a pipe or FIFO, a different error results.)
314.TP
315.B ENOSPC
316There is not enough space left on the device containing the file
317referred to by
318.IR fd .
319.TP
320.B ENOSYS
7cb4d005
MK
321This kernel does not implement
322.BR fallocate ().
d9a0b2a5
MK
323.TP
324.B EOPNOTSUPP
9ee4a2b6 325The filesystem containing the file referred to by
7cb4d005
MK
326.I fd
327does not support this operation;
328or the
d9a0b2a5 329.I mode
9ee4a2b6 330is not supported by the filesystem containing the file referred to by
d9a0b2a5 331.IR fd .
45eb0d22
MK
332.TP
333.B EPERM
334The file referred to by
928b1970 335.I fd
45eb0d22 336is marked immutable (see
7eb92881 337.BR chattr (1)).
95dcbfb5
MK
338.TP
339.B EPERM
54d25eca
MK
340.I mode
341specifies
342.BR FALLOC_FL_PUNCH_HOLE
1ecfa5b3
MK
343or
344.BR FALLOC_FL_COLLAPSE_RANGE
54d25eca
MK
345and
346the file referred to by
347.I fd
348is marked append-only
349(see
928b1970 350.BR chattr (1)).
45eb0d22 351.TP
fbab10e5
MK
352.B EPERM
353The operation was prevented by a file seal; see
354.BR fcntl (2).
355.TP
45eb0d22
MK
356.B ESPIPE
357.I fd
928b1970 358refers to a pipe or FIFO.
1ecfa5b3
MK
359.TP
360.B ETXTBSY
361.I mode
362specifies
363.BR FALLOC_FL_COLLAPSE_RANGE ,
364but the file referred to by
365.IR fd
366is currently being executed.
d9a0b2a5
MK
367.SH VERSIONS
368.BR fallocate ()
d9a0b2a5 369is available on Linux since kernel 2.6.23.
ff6db399 370Support is provided by glibc since version 2.10.
7f26805b
MK
371The
372.BR FALLOC_FL_*
373flags are defined in glibc headers only since version 2.18.
374.\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964
90e261f2 375.SH CONFORMING TO
d9a0b2a5 376.BR fallocate ()
8382f16d 377is Linux-specific.
d9a0b2a5 378.SH SEE ALSO
1b946741 379.BR fallocate (1),
d9a0b2a5 380.BR ftruncate (2),
f0c34053
MK
381.BR posix_fadvise (3),
382.BR posix_fallocate (3)