1FALLOCATE(2)               Linux Programmer's Manual              FALLOCATE(2)
2
3
4

NAME

6       fallocate - manipulate file space
7

SYNOPSIS

9       #define _GNU_SOURCE             /* See feature_test_macros(7) */
10       #include <fcntl.h>
11
12       int fallocate(int fd, int mode, off_t offset, off_t len);
13

DESCRIPTION

15       This  is  a nonportable, Linux-specific system call.  For the portable,
16       POSIX.1-specified method of ensuring that  space  is  allocated  for  a
17       file, see posix_fallocate(3).
18
19       fallocate() allows the caller to directly manipulate the allocated disk
20       space for the file referred to by fd for the  byte  range  starting  at
21       offset and continuing for len bytes.
22
23       The mode argument determines the operation to be performed on the given
24       range.  Details of the supported operations are given  in  the  subsec‐
25       tions below.
26
27   Allocating disk space
28       The default operation (i.e., mode is zero) of fallocate() allocates the
29       disk space within the range specified by offset and len.  The file size
30       (as  reported by stat(2)) will be changed if offset+len is greater than
31       the file size.  Any subregion within the range specified by offset  and
32       len  that  did  not contain data before the call will be initialized to
33       zero.  This default behavior closely  resembles  the  behavior  of  the
34       posix_fallocate(3) library function, and is intended as a method of op‐
35       timally implementing that function.
36
37       After a successful call, subsequent writes into the range specified  by
38       offset  and  len  are  guaranteed  not  to fail because of lack of disk
39       space.
40
41       If the FALLOC_FL_KEEP_SIZE flag is specified in mode, the  behavior  of
42       the call is similar, but the file size will not be changed even if off‐
43       set+len is greater than the file size.  Preallocating zeroed blocks be‐
44       yond the end of the file in this manner is useful for optimizing append
45       workloads.
46
47       If the FALLOC_FL_UNSHARE_RANGE flag is specified in mode,  shared  file
48       data  extents will be made private to the file to guarantee that a sub‐
49       sequent write will not fail due to lack of space.  Typically, this will
50       be  done  by performing a copy-on-write operation on all shared data in
51       the file.  This flag may not be supported by all filesystems.
52
53       Because allocation is done in block size chunks, fallocate() may  allo‐
54       cate a larger range of disk space than was specified.
55
56   Deallocating file space
57       Specifying the FALLOC_FL_PUNCH_HOLE flag (available since Linux 2.6.38)
58       in mode deallocates space (i.e., creates a  hole)  in  the  byte  range
59       starting  at offset and continuing for len bytes.  Within the specified
60       range, partial filesystem  blocks  are  zeroed,  and  whole  filesystem
61       blocks  are removed from the file.  After a successful call, subsequent
62       reads from this range will return zeros.
63
64       The FALLOC_FL_PUNCH_HOLE flag must be ORed with FALLOC_FL_KEEP_SIZE  in
65       mode;  in  other words, even when punching off the end of the file, the
66       file size (as reported by stat(2)) does not change.
67
68       Not all  filesystems  support  FALLOC_FL_PUNCH_HOLE;  if  a  filesystem
69       doesn't  support the operation, an error is returned.  The operation is
70       supported on at least the following filesystems:
71
72       *  XFS (since Linux 2.6.38)
73
74       *  ext4 (since Linux 3.0)
75
76       *  Btrfs (since Linux 3.7)
77
78       *  tmpfs(5) (since Linux 3.5)
79
80       *  gfs2(5) (since Linux 4.16)
81
82   Collapsing file space
83       Specifying the FALLOC_FL_COLLAPSE_RANGE  flag  (available  since  Linux
84       3.15) in mode removes a byte range from a file, without leaving a hole.
85       The byte range to be collapsed starts at offset and continues  for  len
86       bytes.   At  the  completion of the operation, the contents of the file
87       starting at the location offset+len will be appended  at  the  location
88       offset, and the file will be len bytes smaller.
89
90       A filesystem may place limitations on the granularity of the operation,
91       in order to ensure efficient implementation.  Typically, offset and len
92       must  be  a multiple of the filesystem logical block size, which varies
93       according to the filesystem type and configuration.   If  a  filesystem
94       has such a requirement, fallocate() fails with the error EINVAL if this
95       requirement is violated.
96
97       If the region specified by offset plus len reaches or passes the end of
98       file,  an  error  is  returned; instead, use ftruncate(2) to truncate a
99       file.
100
101       No other flags may be  specified  in  mode  in  conjunction  with  FAL‐
102       LOC_FL_COLLAPSE_RANGE.
103
104       As  at  Linux 3.15, FALLOC_FL_COLLAPSE_RANGE is supported by ext4 (only
105       for extent-based files) and XFS.
106
107   Zeroing file space
108       Specifying the FALLOC_FL_ZERO_RANGE flag (available since  Linux  3.15)
109       in mode zeros space in the byte range starting at offset and continuing
110       for len bytes.  Within the specified range, blocks are preallocated for
111       the  regions that span the holes in the file.  After a successful call,
112       subsequent reads from this range will return zeros.
113
114       Zeroing is done within the  filesystem  preferably  by  converting  the
115       range  into  unwritten extents.  This approach means that the specified
116       range will not be physically zeroed out on the device (except for  par‐
117       tial blocks at the either end of the range), and I/O is (otherwise) re‐
118       quired only to update metadata.
119
120       If the FALLOC_FL_KEEP_SIZE flag is additionally specified in mode,  the
121       behavior  of the call is similar, but the file size will not be changed
122       even if offset+len is greater than the file size.  This behavior is the
123       same as when preallocating space with FALLOC_FL_KEEP_SIZE specified.
124
125       Not  all  filesystems  support  FALLOC_FL_ZERO_RANGE;  if  a filesystem
126       doesn't support the operation, an error is returned.  The operation  is
127       supported on at least the following filesystems:
128
129       *  XFS (since Linux 3.15)
130
131       *  ext4, for extent-based files (since Linux 3.15)
132
133       *  SMB3 (since Linux 3.17)
134
135       *  Btrfs (since Linux 4.16)
136
137   Increasing file space
138       Specifying  the FALLOC_FL_INSERT_RANGE flag (available since Linux 4.1)
139       in mode increases the file space by inserting a hole  within  the  file
140       size  without  overwriting  any  existing data.  The hole will start at
141       offset and continue for len bytes.   When  inserting  the  hole  inside
142       file,  the  contents of the file starting at offset will be shifted up‐
143       ward (i.e., to a higher file offset) by len bytes.   Inserting  a  hole
144       inside a file increases the file size by len bytes.
145
146       This  mode has the same limitations as FALLOC_FL_COLLAPSE_RANGE regard‐
147       ing the granularity of the operation.  If the granularity  requirements
148       are not met, fallocate() fails with the error EINVAL.  If the offset is
149       equal to or greater than the end of file, an error  is  returned.   For
150       such  operations  (i.e.,  inserting  a hole at the end of file), ftrun‐
151       cate(2) should be used.
152
153       No other flags may be  specified  in  mode  in  conjunction  with  FAL‐
154       LOC_FL_INSERT_RANGE.
155
156       FALLOC_FL_INSERT_RANGE  requires  filesystem support.  Filesystems that
157       support this operation include XFS (since Linux 4.1)  and  ext4  (since
158       Linux 4.2).
159

RETURN VALUE

161       On success, fallocate() returns zero.  On error, -1 is returned and er‐
162       rno is set to indicate the error.
163

ERRORS

165       EBADF  fd is not a valid file descriptor, or is not opened for writing.
166
167       EFBIG  offset+len exceeds the maximum file size.
168
169       EFBIG  mode is FALLOC_FL_INSERT_RANGE, and the  current  file  size+len
170              exceeds the maximum file size.
171
172       EINTR  A signal was caught during execution; see signal(7).
173
174       EINVAL offset was less than 0, or len was less than or equal to 0.
175
176       EINVAL mode is FALLOC_FL_COLLAPSE_RANGE and the range specified by off‐
177              set plus len reaches or passes the end of the file.
178
179       EINVAL mode is FALLOC_FL_INSERT_RANGE and the range specified by offset
180              reaches or passes the end of the file.
181
182       EINVAL mode  is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE, but
183              either offset or len is not a multiple of the  filesystem  block
184              size.
185
186       EINVAL mode  contains  one of FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_IN‐
187              SERT_RANGE and also other flags; no other  flags  are  permitted
188              with FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_INSERT_RANGE.
189
190       EINVAL mode is FALLOC_FL_COLLAPSE_RANGE or FALLOC_FL_ZERO_RANGE or FAL‐
191              LOC_FL_INSERT_RANGE, but the file referred to by  fd  is  not  a
192              regular file.
193
194       EIO    An  I/O  error  occurred  while  reading  from  or  writing to a
195              filesystem.
196
197       ENODEV fd does not refer to a regular file or a directory.  (If fd is a
198              pipe or FIFO, a different error results.)
199
200       ENOSPC There is not enough space left on the device containing the file
201              referred to by fd.
202
203       ENOSYS This kernel does not implement fallocate().
204
205       EOPNOTSUPP
206              The filesystem containing the file referred to by  fd  does  not
207              support  this  operation;  or  the  mode is not supported by the
208              filesystem containing the file referred to by fd.
209
210       EPERM  The file referred to by fd is marked immutable (see chattr(1)).
211
212       EPERM  mode specifies FALLOC_FL_PUNCH_HOLE or  FALLOC_FL_COLLAPSE_RANGE
213              or  FALLOC_FL_INSERT_RANGE  and  the  file  referred to by fd is
214              marked append-only (see chattr(1)).
215
216       EPERM  The operation was prevented by a file seal; see fcntl(2).
217
218       ESPIPE fd refers to a pipe or FIFO.
219
220       ETXTBSY
221              mode   specifies   FALLOC_FL_COLLAPSE_RANGE   or   FALLOC_FL_IN‐
222              SERT_RANGE,  but  the  file referred to by fd is currently being
223              executed.
224

VERSIONS

226       fallocate() is available on Linux since kernel 2.6.23.  Support is pro‐
227       vided  by  glibc since version 2.10.  The FALLOC_FL_* flags are defined
228       in glibc headers only since version 2.18.
229

CONFORMING TO

231       fallocate() is Linux-specific.
232

SEE ALSO

234       fallocate(1), ftruncate(2), posix_fadvise(3), posix_fallocate(3)
235

COLOPHON

237       This page is part of release 5.13 of the Linux  man-pages  project.   A
238       description  of  the project, information about reporting bugs, and the
239       latest    version    of    this    page,    can     be     found     at
240       https://www.kernel.org/doc/man-pages/.
241
242
243
244Linux                             2019-11-19                      FALLOCATE(2)
Impressum