]>
Commit | Line | Data |
---|---|---|
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 | .\" | |
4b8c67d9 | 11 | .TH FALLOCATE 2 2017-09-15 "Linux" "Linux Programmer's Manual" |
d9a0b2a5 MK |
12 | .SH NAME |
13 | fallocate \- 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 | 23 | This is a nonportable, Linux-specific system call. |
01b7704f MK |
24 | For the portable, POSIX.1-specified method of ensuring that space |
25 | is allocated for a file, see | |
8c56fcec | 26 | .BR posix_fallocate (3). |
efeece04 | 27 | .PP |
d9a0b2a5 MK |
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. | |
efeece04 | 37 | .PP |
d9a0b2a5 MK |
38 | The |
39 | .I mode | |
40 | argument determines the operation to be performed on the given range. | |
f52e0be8 MK |
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 () | |
c166fac5 | 47 | allocates the disk space within the range specified by |
d9a0b2a5 MK |
48 | .I offset |
49 | and | |
50 | .IR len . | |
f52e0be8 MK |
51 | The file size (as reported by |
52 | .BR stat (2)) | |
53 | will be changed if | |
381ee84e | 54 | .IR offset + len |
f52e0be8 | 55 | is greater than the file size. |
4d4cecd6 | 56 | Any subregion within the range specified by |
c166fac5 CH |
57 | .I offset |
58 | and | |
ddacd026 | 59 | .IR len |
c166fac5 | 60 | that did not contain data before the call will be initialized to zero. |
f52e0be8 MK |
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. | |
efeece04 | 65 | .PP |
f52e0be8 MK |
66 | After a successful call, subsequent writes into the range specified by |
67 | .IR offset | |
68 | and | |
69 | .IR len | |
d9a0b2a5 | 70 | are guaranteed not to fail because of lack of disk space. |
efeece04 | 71 | .PP |
f52e0be8 MK |
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 | |
381ee84e | 78 | .IR offset + len |
f52e0be8 MK |
79 | is greater than the file size. |
80 | Preallocating zeroed blocks beyond the end of the file in this manner | |
d9a0b2a5 | 81 | is useful for optimizing append workloads. |
efeece04 | 82 | .PP |
63a599c6 DW |
83 | If the |
84 | .B FALLOC_FL_UNSHARE | |
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. | |
d9a0b2a5 | 92 | .PP |
f52e0be8 MK |
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) | |
96575bbc | 102 | in the byte range starting at |
1c7857e9 LAG |
103 | .I offset |
104 | and continuing for | |
105 | .I len | |
f52e0be8 | 106 | bytes. |
9ee4a2b6 MK |
107 | Within the specified range, partial filesystem blocks are zeroed, |
108 | and whole filesystem blocks are removed from the file. | |
96575bbc MK |
109 | After a successful call, |
110 | subsequent reads from this range will return zeroes. | |
efeece04 | 111 | .PP |
96575bbc | 112 | The |
1c7857e9 | 113 | .BR FALLOC_FL_PUNCH_HOLE |
96575bbc | 114 | flag must be ORed with |
1c7857e9 | 115 | .BR FALLOC_FL_KEEP_SIZE |
f52e0be8 MK |
116 | in |
117 | .IR mode ; | |
96575bbc MK |
118 | in other words, even when punching off the end of the file, the file size |
119 | (as reported by | |
1c7857e9 | 120 | .BR stat (2)) |
96575bbc | 121 | does not change. |
efeece04 | 122 | .PP |
9ee4a2b6 | 123 | Not all filesystems support |
f52e0be8 | 124 | .BR FALLOC_FL_PUNCH_HOLE ; |
9ee4a2b6 | 125 | if a filesystem doesn't support the operation, an error is returned. |
ce430f41 | 126 | The operation is supported on at least the following filesystems: |
49995543 MK |
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 * | |
4e07c70f | 135 | .BR tmpfs "(5) (since Linux 3.5)" |
49995543 | 136 | .\" commit 83e4fa9c16e4af7122e31be3eca5d57881d236fe |
ea56dddf | 137 | .SS Collapsing file space |
1ecfa5b3 | 138 | .\" commit 00f5e61998dd17f5375d9dfc01331f104b83f841 |
f78da076 | 139 | Specifying the |
ea56dddf | 140 | .BR FALLOC_FL_COLLAPSE_RANGE |
1ecfa5b3 | 141 | flag (available since Linux 3.15) in |
ea56dddf | 142 | .I mode |
1ecfa5b3 MK |
143 | removes a byte range from a file, without leaving a hole. |
144 | The byte range to be collapsed starts at | |
ea56dddf | 145 | .I offset |
1ecfa5b3 | 146 | and continues for |
ea56dddf | 147 | .I len |
f78da076 | 148 | bytes. |
1ecfa5b3 MK |
149 | At the completion of the operation, |
150 | the contents of the file starting at the location | |
151 | .I offset+len | |
152 | will be appended at the location | |
153 | .IR offset , | |
154 | and the file will be | |
155 | .I len | |
156 | bytes smaller. | |
efeece04 | 157 | .PP |
1ecfa5b3 MK |
158 | A filesystem may place limitations on the granularity of the operation, |
159 | in order to ensure efficient implementation. | |
160 | Typically, | |
161 | .I offset | |
162 | and | |
163 | .I len | |
164 | must be a multiple of the filesystem logical block size, | |
165 | which varies according to the filesystem type and configuration. | |
166 | If a filesystem has such a requirement, | |
167 | .BR fallocate () | |
26cd31fd | 168 | fails with the error |
1ecfa5b3 MK |
169 | .BR EINVAL |
170 | if this requirement is violated. | |
efeece04 | 171 | .PP |
1ecfa5b3 MK |
172 | If the region specified by |
173 | .I offset | |
174 | plus | |
175 | .I len | |
176 | reaches or passes the end of file, an error is returned; | |
177 | instead, use | |
178 | .BR ftruncate (2) | |
179 | to truncate a file. | |
efeece04 | 180 | .PP |
1ecfa5b3 MK |
181 | No other flags may be specified in |
182 | .IR mode | |
183 | in conjunction with | |
184 | .BR FALLOC_FL_COLLAPSE_RANGE . | |
efeece04 | 185 | .PP |
1ecfa5b3 MK |
186 | As at Linux 3.15, |
187 | .B FALLOC_FL_COLLAPSE_RANGE | |
188 | is supported by | |
189 | ext4 (only for extent-based files) | |
190 | .\" commit 9eb79482a97152930b113b51dff530aba9e28c8e | |
191 | and XFS. | |
192 | .\" commit e1d8fb88a64c1f8094b9f6c3b6d2d9e6719c970d | |
7dc489e3 | 193 | .SS Zeroing file space |
44e4132a | 194 | Specifying the |
7dc489e3 | 195 | .BR FALLOC_FL_ZERO_RANGE |
4a401602 | 196 | flag (available since Linux 3.15) |
44e4132a MK |
197 | .\" commit 409332b65d3ed8cfa7a8030f1e9d52f372219642 |
198 | in | |
7dc489e3 LC |
199 | .I mode |
200 | zeroes space in the byte range starting at | |
201 | .I offset | |
202 | and continuing for | |
203 | .I len | |
204 | bytes. | |
205 | Within the specified range, blocks are preallocated for the regions | |
44e4132a MK |
206 | that span the holes in the file. |
207 | After a successful call, subsequent | |
7dc489e3 | 208 | reads from this range will return zeroes. |
efeece04 | 209 | .PP |
7c7960f8 MK |
210 | Zeroing is done within the filesystem preferably by converting the range into |
211 | unwritten extents. | |
212 | This approach means that the specified range will not be physically zeroed | |
4cb27742 | 213 | out on the device (except for partial blocks at the either end of the range), |
b9ddebe2 | 214 | and I/O is (otherwise) required only to update metadata. |
efeece04 | 215 | .PP |
7dc489e3 LC |
216 | If the |
217 | .B FALLOC_FL_KEEP_SIZE | |
7c7960f8 | 218 | flag is additionally specified in |
7dc489e3 LC |
219 | .IR mode , |
220 | the behavior of the call is similar, | |
221 | but the file size will not be changed even if | |
222 | .IR offset + len | |
44e4132a | 223 | is greater than the file size. |
a1fa36af | 224 | This behavior is the same as when preallocating space with |
7dc489e3 LC |
225 | .B FALLOC_FL_KEEP_SIZE |
226 | specified. | |
efeece04 | 227 | .PP |
7dc489e3 LC |
228 | Not all filesystems support |
229 | .BR FALLOC_FL_ZERO_RANGE ; | |
230 | if a filesystem doesn't support the operation, an error is returned. | |
44e4132a | 231 | The operation is supported on at least the following filesystems: |
7dc489e3 | 232 | .IP * 3 |
4a401602 | 233 | XFS (since Linux 3.15) |
7dc489e3 LC |
234 | .\" commit 376ba313147b4172f3e8cf620b9fb591f3e8cdfa |
235 | .IP * | |
4a401602 | 236 | ext4, for extent-based files (since Linux 3.15) |
7dc489e3 | 237 | .\" commit b8a8684502a0fc852afa0056c6bb2a9273f6fcc0 |
adf507a3 MK |
238 | .IP * |
239 | SMB3 (since Linux 3.17) | |
240 | .\" commit 30175628bf7f521e9ee31ac98fa6d6fe7441a556 | |
511e0bd0 MK |
241 | .IP * |
242 | Btrfs (since Linux 4.16) | |
243 | .\" commit f27451f229966874a8793995b8e6b74326d125df | |
7da70a01 | 244 | .SS Increasing file space |
7da70a01 NJ |
245 | Specifying the |
246 | .BR FALLOC_FL_INSERT_RANGE | |
ec9a2be0 MK |
247 | flag |
248 | (available since Linux 4.1) | |
249 | .\" commit dd46c787788d5bf5b974729d43e4c405814a4c7d | |
250 | in | |
7da70a01 | 251 | .I mode |
ec9a2be0 | 252 | increases the file space by inserting a hole within the file size without |
7da70a01 NJ |
253 | overwriting any existing data. |
254 | The hole will start at | |
255 | .I offset | |
256 | and continue for | |
257 | .I len | |
258 | bytes. | |
ec9a2be0 | 259 | When inserting the hole inside file, the contents of the file starting at |
7da70a01 | 260 | .I offset |
ec9a2be0 | 261 | will be shifted upward (i.e., to a higher file offset) by |
7da70a01 NJ |
262 | .I len |
263 | bytes. | |
ec9a2be0 | 264 | Inserting a hole inside a file increases the file size by |
7da70a01 NJ |
265 | .I len |
266 | bytes. | |
efeece04 | 267 | .PP |
ec9a2be0 | 268 | This mode has the same limitations as |
7da70a01 | 269 | .BR FALLOC_FL_COLLAPSE_RANGE |
ec9a2be0 | 270 | regarding the granularity of the operation. |
7da70a01 NJ |
271 | If the granularity requirements are not met, |
272 | .BR fallocate () | |
26cd31fd | 273 | fails with the error |
7da70a01 NJ |
274 | .BR EINVAL. |
275 | If the | |
276 | .I offset | |
ec9a2be0 MK |
277 | is equal to or greater than the end of file, an error is returned. |
278 | For such operations (i.e., inserting a hole at the end of file), | |
279 | .BR ftruncate (2) | |
7da70a01 | 280 | should be used. |
efeece04 | 281 | .PP |
7da70a01 NJ |
282 | No other flags may be specified in |
283 | .IR mode | |
284 | in conjunction with | |
285 | .BR FALLOC_FL_INSERT_RANGE . | |
efeece04 | 286 | .PP |
7da70a01 | 287 | .B FALLOC_FL_INSERT_RANGE |
bb2f7580 MK |
288 | requires filesystem support. |
289 | Filesystems that support this operation include | |
290 | XFS (since Linux 4.1) | |
7da70a01 | 291 | .\" commit a904b1ca5751faf5ece8600e18cd3b674afcca1b |
bb2f7580 MK |
292 | and 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 | 297 | On success, |
d9a0b2a5 | 298 | .BR fallocate () |
47a97908 MK |
299 | returns zero. |
300 | On error, \-1 is returned and | |
301 | .I errno | |
302 | is set to indicate the error. | |
d9a0b2a5 MK |
303 | .SH ERRORS |
304 | .TP | |
305 | .B EBADF | |
306 | .I fd | |
307 | is not a valid file descriptor, or is not opened for writing. | |
308 | .TP | |
309 | .B EFBIG | |
310 | .IR offset + len | |
311 | exceeds the maximum file size. | |
312 | .TP | |
7da70a01 NJ |
313 | .B EFBIG |
314 | .I mode | |
315 | is | |
316 | .BR FALLOC_FL_INSERT_RANGE , | |
ec9a2be0 | 317 | and the current file size+\fIlen\fP exceeds the maximum file size. |
7da70a01 | 318 | .TP |
d9a0b2a5 | 319 | .B EINTR |
bb14af39 MK |
320 | A signal was caught during execution; see |
321 | .BR signal (7). | |
d9a0b2a5 MK |
322 | .TP |
323 | .B EINVAL | |
324 | .I offset | |
325 | was 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 |
333 | was less than or equal to 0. |
334 | .TP | |
1ecfa5b3 MK |
335 | .B EINVAL |
336 | .I mode | |
337 | is | |
338 | .BR FALLOC_FL_COLLAPSE_RANGE | |
339 | and the range specified by | |
340 | .I offset | |
341 | plus | |
342 | .I len | |
343 | reaches or passes the end of the file. | |
344 | .TP | |
345 | .B EINVAL | |
346 | .I mode | |
347 | is | |
7da70a01 NJ |
348 | .BR FALLOC_FL_INSERT_RANGE |
349 | and the range specified by | |
350 | .I offset | |
351 | reaches or passes the end of the file. | |
352 | .TP | |
353 | .B EINVAL | |
354 | .I mode | |
355 | is | |
356 | .BR FALLOC_FL_COLLAPSE_RANGE | |
357 | or | |
358 | .BR FALLOC_FL_INSERT_RANGE , | |
1ecfa5b3 MK |
359 | but either |
360 | .I offset | |
361 | or | |
362 | .I len | |
363 | is not a multiple of the filesystem block size. | |
364 | .TP | |
365 | .B EINVAL | |
718778ad | 366 | .I mode |
ec9a2be0 | 367 | contains one of |
1ecfa5b3 | 368 | .B FALLOC_FL_COLLAPSE_RANGE |
7da70a01 NJ |
369 | or |
370 | .B FALLOC_FL_INSERT_RANGE | |
ec9a2be0 | 371 | and also other flags; |
1ecfa5b3 | 372 | no other flags are permitted with |
7da70a01 NJ |
373 | .BR FALLOC_FL_COLLAPSE_RANGE |
374 | or | |
375 | .BR FALLOC_FL_INSERT_RANGE . | |
718778ad SP |
376 | .TP |
377 | .B EINVAL | |
2af4d967 MK |
378 | .I mode |
379 | is | |
7dc489e3 LC |
380 | .BR FALLOC_FL_COLLAPSE_RANGE |
381 | or | |
7da70a01 NJ |
382 | .BR FALLOC_FL_ZERO_RANGE |
383 | or | |
384 | .BR FALLOC_FL_INSERT_RANGE , | |
2af4d967 MK |
385 | but the file referred to by |
386 | .I fd | |
387 | is 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 | 397 | An I/O error occurred while reading from or writing to a filesystem. |
d9a0b2a5 MK |
398 | .TP |
399 | .B ENODEV | |
400 | .I fd | |
401 | does not refer to a regular file or a directory. | |
402 | (If | |
403 | .I fd | |
404 | is a pipe or FIFO, a different error results.) | |
405 | .TP | |
406 | .B ENOSPC | |
407 | There is not enough space left on the device containing the file | |
408 | referred to by | |
409 | .IR fd . | |
410 | .TP | |
411 | .B ENOSYS | |
7cb4d005 MK |
412 | This kernel does not implement |
413 | .BR fallocate (). | |
d9a0b2a5 MK |
414 | .TP |
415 | .B EOPNOTSUPP | |
9ee4a2b6 | 416 | The filesystem containing the file referred to by |
7cb4d005 MK |
417 | .I fd |
418 | does not support this operation; | |
419 | or the | |
d9a0b2a5 | 420 | .I mode |
9ee4a2b6 | 421 | is not supported by the filesystem containing the file referred to by |
d9a0b2a5 | 422 | .IR fd . |
45eb0d22 MK |
423 | .TP |
424 | .B EPERM | |
425 | The file referred to by | |
928b1970 | 426 | .I fd |
45eb0d22 | 427 | is marked immutable (see |
7eb92881 | 428 | .BR chattr (1)). |
95dcbfb5 MK |
429 | .TP |
430 | .B EPERM | |
54d25eca MK |
431 | .I mode |
432 | specifies | |
433 | .BR FALLOC_FL_PUNCH_HOLE | |
1ecfa5b3 MK |
434 | or |
435 | .BR FALLOC_FL_COLLAPSE_RANGE | |
7da70a01 NJ |
436 | or |
437 | .BR FALLOC_FL_INSERT_RANGE | |
54d25eca MK |
438 | and |
439 | the file referred to by | |
440 | .I fd | |
441 | is marked append-only | |
442 | (see | |
928b1970 | 443 | .BR chattr (1)). |
45eb0d22 | 444 | .TP |
fbab10e5 MK |
445 | .B EPERM |
446 | The operation was prevented by a file seal; see | |
447 | .BR fcntl (2). | |
448 | .TP | |
45eb0d22 MK |
449 | .B ESPIPE |
450 | .I fd | |
928b1970 | 451 | refers to a pipe or FIFO. |
1ecfa5b3 MK |
452 | .TP |
453 | .B ETXTBSY | |
454 | .I mode | |
455 | specifies | |
7da70a01 NJ |
456 | .BR FALLOC_FL_COLLAPSE_RANGE |
457 | or | |
458 | .BR FALLOC_FL_INSERT_RANGE , | |
1ecfa5b3 MK |
459 | but the file referred to by |
460 | .IR fd | |
461 | is currently being executed. | |
d9a0b2a5 MK |
462 | .SH VERSIONS |
463 | .BR fallocate () | |
d9a0b2a5 | 464 | is available on Linux since kernel 2.6.23. |
ff6db399 | 465 | Support is provided by glibc since version 2.10. |
7f26805b MK |
466 | The |
467 | .BR FALLOC_FL_* | |
468 | flags 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 | 472 | is 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) |