man2/fallocate.2: tfix documentation of shared blocks
[man-pages.git] / man2 / fallocate.2
blobb4cb3516f5764679c10bd45119f4f819f20cfa40
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 2019-11-19 "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_RANGE
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.
113 .BR FALLOC_FL_PUNCH_HOLE
114 flag must be ORed with
115 .BR FALLOC_FL_KEEP_SIZE
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.
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 .IP *
139 .BR gfs2 (5)
140 (since Linux 4.16)
141 .\" commit 4e56a6411fbce6f859566e17298114c2434391a4
142 .SS Collapsing file space
143 .\" commit 00f5e61998dd17f5375d9dfc01331f104b83f841
144 Specifying the
145 .BR FALLOC_FL_COLLAPSE_RANGE
146 flag (available since Linux 3.15) in
147 .I mode
148 removes a byte range from a file, without leaving a hole.
149 The byte range to be collapsed starts at
150 .I offset
151 and continues for
152 .I len
153 bytes.
154 At the completion of the operation,
155 the contents of the file starting at the location
156 .I offset+len
157 will be appended at the location
158 .IR offset ,
159 and the file will be
160 .I len
161 bytes smaller.
163 A filesystem may place limitations on the granularity of the operation,
164 in order to ensure efficient implementation.
165 Typically,
166 .I offset
168 .I len
169 must be a multiple of the filesystem logical block size,
170 which varies according to the filesystem type and configuration.
171 If a filesystem has such a requirement,
172 .BR fallocate ()
173 fails with the error
174 .BR EINVAL
175 if this requirement is violated.
177 If the region specified by
178 .I offset
179 plus
180 .I len
181 reaches or passes the end of file, an error is returned;
182 instead, use
183 .BR ftruncate (2)
184 to truncate a file.
186 No other flags may be specified in
187 .IR mode
188 in conjunction with
189 .BR FALLOC_FL_COLLAPSE_RANGE .
191 As at Linux 3.15,
192 .B FALLOC_FL_COLLAPSE_RANGE
193 is supported by
194 ext4 (only for extent-based files)
195 .\" commit 9eb79482a97152930b113b51dff530aba9e28c8e
196 and XFS.
197 .\" commit e1d8fb88a64c1f8094b9f6c3b6d2d9e6719c970d
198 .SS Zeroing file space
199 Specifying the
200 .BR FALLOC_FL_ZERO_RANGE
201 flag (available since Linux 3.15)
202 .\" commit 409332b65d3ed8cfa7a8030f1e9d52f372219642
204 .I mode
205 zeros space in the byte range starting at
206 .I offset
207 and continuing for
208 .I len
209 bytes.
210 Within the specified range, blocks are preallocated for the regions
211 that span the holes in the file.
212 After a successful call, subsequent
213 reads from this range will return zeros.
215 Zeroing is done within the filesystem preferably by converting the range into
216 unwritten extents.
217 This approach means that the specified range will not be physically zeroed
218 out on the device (except for partial blocks at the either end of the range),
219 and I/O is (otherwise) required only to update metadata.
221 If the
222 .B FALLOC_FL_KEEP_SIZE
223 flag is additionally specified in
224 .IR mode ,
225 the behavior of the call is similar,
226 but the file size will not be changed even if
227 .IR offset + len
228 is greater than the file size.
229 This behavior is the same as when preallocating space with
230 .B FALLOC_FL_KEEP_SIZE
231 specified.
233 Not all filesystems support
234 .BR FALLOC_FL_ZERO_RANGE ;
235 if a filesystem doesn't support the operation, an error is returned.
236 The operation is supported on at least the following filesystems:
237 .IP * 3
238 XFS (since Linux 3.15)
239 .\" commit 376ba313147b4172f3e8cf620b9fb591f3e8cdfa
240 .IP *
241 ext4, for extent-based files (since Linux 3.15)
242 .\" commit b8a8684502a0fc852afa0056c6bb2a9273f6fcc0
243 .IP *
244 SMB3 (since Linux 3.17)
245 .\" commit 30175628bf7f521e9ee31ac98fa6d6fe7441a556
246 .IP *
247 Btrfs (since Linux 4.16)
248 .\" commit f27451f229966874a8793995b8e6b74326d125df
249 .SS Increasing file space
250 Specifying the
251 .BR FALLOC_FL_INSERT_RANGE
252 flag
253 (available since Linux 4.1)
254 .\" commit dd46c787788d5bf5b974729d43e4c405814a4c7d
256 .I mode
257 increases the file space by inserting a hole within the file size without
258 overwriting any existing data.
259 The hole will start at
260 .I offset
261 and continue for
262 .I len
263 bytes.
264 When inserting the hole inside file, the contents of the file starting at
265 .I offset
266 will be shifted upward (i.e., to a higher file offset) by
267 .I len
268 bytes.
269 Inserting a hole inside a file increases the file size by
270 .I len
271 bytes.
273 This mode has the same limitations as
274 .BR FALLOC_FL_COLLAPSE_RANGE
275 regarding the granularity of the operation.
276 If the granularity requirements are not met,
277 .BR fallocate ()
278 fails with the error
279 .BR EINVAL .
280 If the
281 .I offset
282 is equal to or greater than the end of file, an error is returned.
283 For such operations (i.e., inserting a hole at the end of file),
284 .BR ftruncate (2)
285 should be used.
287 No other flags may be specified in
288 .IR mode
289 in conjunction with
290 .BR FALLOC_FL_INSERT_RANGE .
292 .B FALLOC_FL_INSERT_RANGE
293 requires filesystem support.
294 Filesystems that support this operation include
295 XFS (since Linux 4.1)
296 .\" commit a904b1ca5751faf5ece8600e18cd3b674afcca1b
297 and ext4 (since Linux 4.2).
298 .\" commit 331573febb6a224bc50322e3670da326cb7f4cfc
299 .\" f2fs also has support since Linux 4.2
300 .\"     commit f62185d0e283e9d311e3ac1020f159d95f0aab39
301 .SH RETURN VALUE
302 On success,
303 .BR fallocate ()
304 returns zero.
305 On error, \-1 is returned and
306 .I errno
307 is set to indicate the error.
308 .SH ERRORS
310 .B EBADF
311 .I fd
312 is not a valid file descriptor, or is not opened for writing.
314 .B EFBIG
315 .IR offset + len
316 exceeds the maximum file size.
318 .B EFBIG
319 .I mode
321 .BR FALLOC_FL_INSERT_RANGE ,
322 and the current file size+\fIlen\fP exceeds the maximum file size.
324 .B EINTR
325 A signal was caught during execution; see
326 .BR signal (7).
328 .B EINVAL
329 .I offset
330 was less than 0, or
331 .I len
332 .\" FIXME . (raise a kernel bug) Probably the len==0 case should be
333 .\" a no-op, rather than an error. That would be consistent with
334 .\" similar APIs for the len==0 case.
335 .\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition"
336 .\" 21 Sep 2012
337 .\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526
338 was less than or equal to 0.
340 .B EINVAL
341 .I mode
343 .BR FALLOC_FL_COLLAPSE_RANGE
344 and the range specified by
345 .I offset
346 plus
347 .I len
348 reaches or passes the end of the file.
350 .B EINVAL
351 .I mode
353 .BR FALLOC_FL_INSERT_RANGE
354 and the range specified by
355 .I offset
356 reaches or passes the end of the file.
358 .B EINVAL
359 .I mode
361 .BR FALLOC_FL_COLLAPSE_RANGE
363 .BR FALLOC_FL_INSERT_RANGE ,
364 but either
365 .I offset
367 .I len
368 is not a multiple of the filesystem block size.
370 .B EINVAL
371 .I mode
372 contains one of
373 .B FALLOC_FL_COLLAPSE_RANGE
375 .B FALLOC_FL_INSERT_RANGE
376 and also other flags;
377 no other flags are permitted with
378 .BR FALLOC_FL_COLLAPSE_RANGE
380 .BR FALLOC_FL_INSERT_RANGE .
382 .B EINVAL
383 .I mode
385 .BR FALLOC_FL_COLLAPSE_RANGE
387 .BR FALLOC_FL_ZERO_RANGE
389 .BR FALLOC_FL_INSERT_RANGE ,
390 but the file referred to by
391 .I fd
392 is not a regular file.
393 .\" There was an inconsistency in 3.15-rc1, that should be resolved so that all
394 .\" filesystems use this error for this case. (Tytso says ex4 will change.)
395 .\" http://thread.gmane.org/gmane.comp.file-systems.xfs.general/60485/focus=5521
396 .\" From: Michael Kerrisk (man-pages <mtk.manpages@...>
397 .\" Subject: Re: [PATCH v5 10/10] manpage: update FALLOC_FL_COLLAPSE_RANGE flag in fallocate
398 .\" Newsgroups: gmane.linux.man, gmane.linux.file-systems
399 .\" Date: 2014-04-17 13:40:05 GMT
401 .B EIO
402 An I/O error occurred while reading from or writing to a filesystem.
404 .B ENODEV
405 .I fd
406 does not refer to a regular file or a directory.
408 .I fd
409 is a pipe or FIFO, a different error results.)
411 .B ENOSPC
412 There is not enough space left on the device containing the file
413 referred to by
414 .IR fd .
416 .B ENOSYS
417 This kernel does not implement
418 .BR fallocate ().
420 .B EOPNOTSUPP
421 The filesystem containing the file referred to by
422 .I fd
423 does not support this operation;
424 or the
425 .I mode
426 is not supported by the filesystem containing the file referred to by
427 .IR fd .
429 .B EPERM
430 The file referred to by
431 .I fd
432 is marked immutable (see
433 .BR chattr (1)).
435 .B EPERM
436 .I mode
437 specifies
438 .BR FALLOC_FL_PUNCH_HOLE
440 .BR FALLOC_FL_COLLAPSE_RANGE
442 .BR FALLOC_FL_INSERT_RANGE
444 the file referred to by
445 .I fd
446 is marked append-only
447 (see
448 .BR chattr (1)).
450 .B EPERM
451 The operation was prevented by a file seal; see
452 .BR fcntl (2).
454 .B ESPIPE
455 .I fd
456 refers to a pipe or FIFO.
458 .B ETXTBSY
459 .I mode
460 specifies
461 .BR FALLOC_FL_COLLAPSE_RANGE
463 .BR FALLOC_FL_INSERT_RANGE ,
464 but the file referred to by
465 .IR fd
466 is currently being executed.
467 .SH VERSIONS
468 .BR fallocate ()
469 is available on Linux since kernel 2.6.23.
470 Support is provided by glibc since version 2.10.
472 .BR FALLOC_FL_*
473 flags are defined in glibc headers only since version 2.18.
474 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964
475 .SH CONFORMING TO
476 .BR fallocate ()
477 is Linux-specific.
478 .SH SEE ALSO
479 .BR fallocate (1),
480 .BR ftruncate (2),
481 .BR posix_fadvise (3),
482 .BR posix_fallocate (3)