]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/fallocate.2
lseek.2: Note which filesystems support SEEK_HOLE/SEEK_DATA
[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.\"
4d4cecd6 11.TH FALLOCATE 2 2013-11-08 "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
59.IR len .
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.
d9a0b2a5 116.SH RETURN VALUE
276939a6 117On success,
d9a0b2a5 118.BR fallocate ()
47a97908
MK
119returns zero.
120On error, \-1 is returned and
121.I errno
122is set to indicate the error.
d9a0b2a5
MK
123.SH ERRORS
124.TP
125.B EBADF
126.I fd
127is not a valid file descriptor, or is not opened for writing.
128.TP
129.B EFBIG
130.IR offset + len
131exceeds the maximum file size.
132.TP
133.B EINTR
134A signal was caught during execution.
135.TP
136.B EINVAL
137.I offset
138was less than 0, or
139.I len
c0c3d019
MK
140.\" FIXME (raise a kernel bug) Probably the len==0 case should be
141.\" a no-op, rather than an error. That would be consistent with
142.\" similar APIs for the len==0 case.
143.\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition"
144.\" 21 Sep 2012
145.\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526
d9a0b2a5
MK
146was less than or equal to 0.
147.TP
148.B EIO
9ee4a2b6 149An I/O error occurred while reading from or writing to a filesystem.
d9a0b2a5
MK
150.TP
151.B ENODEV
152.I fd
153does not refer to a regular file or a directory.
154(If
155.I fd
156is a pipe or FIFO, a different error results.)
157.TP
158.B ENOSPC
159There is not enough space left on the device containing the file
160referred to by
161.IR fd .
162.TP
163.B ENOSYS
7cb4d005
MK
164This kernel does not implement
165.BR fallocate ().
d9a0b2a5
MK
166.TP
167.B EOPNOTSUPP
9ee4a2b6 168The filesystem containing the file referred to by
7cb4d005
MK
169.I fd
170does not support this operation;
171or the
d9a0b2a5 172.I mode
9ee4a2b6 173is not supported by the filesystem containing the file referred to by
d9a0b2a5 174.IR fd .
45eb0d22
MK
175.TP
176.B EPERM
177The file referred to by
928b1970 178.I fd
45eb0d22 179is marked immutable (see
7eb92881 180.BR chattr (1)).
54d25eca
MK
181Or:
182.I mode
183specifies
184.BR FALLOC_FL_PUNCH_HOLE
185and
186the file referred to by
187.I fd
188is marked append-only
189(see
928b1970 190.BR chattr (1)).
45eb0d22
MK
191.TP
192.B ESPIPE
193.I fd
928b1970 194refers to a pipe or FIFO.
d9a0b2a5
MK
195.SH VERSIONS
196.BR fallocate ()
d9a0b2a5 197is available on Linux since kernel 2.6.23.
ff6db399 198Support is provided by glibc since version 2.10.
7f26805b
MK
199The
200.BR FALLOC_FL_*
201flags are defined in glibc headers only since version 2.18.
202.\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964
90e261f2 203.SH CONFORMING TO
d9a0b2a5 204.BR fallocate ()
8382f16d 205is Linux-specific.
d9a0b2a5 206.SH SEE ALSO
1b946741 207.BR fallocate (1),
d9a0b2a5 208.BR ftruncate (2),
f0c34053
MK
209.BR posix_fadvise (3),
210.BR posix_fallocate (3)