]>
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 | |
7da70a01 | 241 | .SS Increasing file space |
7da70a01 NJ |
242 | Specifying the |
243 | .BR FALLOC_FL_INSERT_RANGE | |
ec9a2be0 MK |
244 | flag |
245 | (available since Linux 4.1) | |
246 | .\" commit dd46c787788d5bf5b974729d43e4c405814a4c7d | |
247 | in | |
7da70a01 | 248 | .I mode |
ec9a2be0 | 249 | increases the file space by inserting a hole within the file size without |
7da70a01 NJ |
250 | overwriting any existing data. |
251 | The hole will start at | |
252 | .I offset | |
253 | and continue for | |
254 | .I len | |
255 | bytes. | |
ec9a2be0 | 256 | When inserting the hole inside file, the contents of the file starting at |
7da70a01 | 257 | .I offset |
ec9a2be0 | 258 | will be shifted upward (i.e., to a higher file offset) by |
7da70a01 NJ |
259 | .I len |
260 | bytes. | |
ec9a2be0 | 261 | Inserting a hole inside a file increases the file size by |
7da70a01 NJ |
262 | .I len |
263 | bytes. | |
efeece04 | 264 | .PP |
ec9a2be0 | 265 | This mode has the same limitations as |
7da70a01 | 266 | .BR FALLOC_FL_COLLAPSE_RANGE |
ec9a2be0 | 267 | regarding the granularity of the operation. |
7da70a01 NJ |
268 | If the granularity requirements are not met, |
269 | .BR fallocate () | |
26cd31fd | 270 | fails with the error |
7da70a01 NJ |
271 | .BR EINVAL. |
272 | If the | |
273 | .I offset | |
ec9a2be0 MK |
274 | is equal to or greater than the end of file, an error is returned. |
275 | For such operations (i.e., inserting a hole at the end of file), | |
276 | .BR ftruncate (2) | |
7da70a01 | 277 | should be used. |
efeece04 | 278 | .PP |
7da70a01 NJ |
279 | No other flags may be specified in |
280 | .IR mode | |
281 | in conjunction with | |
282 | .BR FALLOC_FL_INSERT_RANGE . | |
efeece04 | 283 | .PP |
7da70a01 | 284 | .B FALLOC_FL_INSERT_RANGE |
bb2f7580 MK |
285 | requires filesystem support. |
286 | Filesystems that support this operation include | |
287 | XFS (since Linux 4.1) | |
7da70a01 | 288 | .\" commit a904b1ca5751faf5ece8600e18cd3b674afcca1b |
bb2f7580 MK |
289 | and ext4 (since Linux 4.2). |
290 | .\" commit 331573febb6a224bc50322e3670da326cb7f4cfc | |
291 | .\" f2fs also has support since Linux 4.2 | |
292 | .\" commit f62185d0e283e9d311e3ac1020f159d95f0aab39 | |
d9a0b2a5 | 293 | .SH RETURN VALUE |
276939a6 | 294 | On success, |
d9a0b2a5 | 295 | .BR fallocate () |
47a97908 MK |
296 | returns zero. |
297 | On error, \-1 is returned and | |
298 | .I errno | |
299 | is set to indicate the error. | |
d9a0b2a5 MK |
300 | .SH ERRORS |
301 | .TP | |
302 | .B EBADF | |
303 | .I fd | |
304 | is not a valid file descriptor, or is not opened for writing. | |
305 | .TP | |
306 | .B EFBIG | |
307 | .IR offset + len | |
308 | exceeds the maximum file size. | |
309 | .TP | |
7da70a01 NJ |
310 | .B EFBIG |
311 | .I mode | |
312 | is | |
313 | .BR FALLOC_FL_INSERT_RANGE , | |
ec9a2be0 | 314 | and the current file size+\fIlen\fP exceeds the maximum file size. |
7da70a01 | 315 | .TP |
d9a0b2a5 | 316 | .B EINTR |
bb14af39 MK |
317 | A signal was caught during execution; see |
318 | .BR signal (7). | |
d9a0b2a5 MK |
319 | .TP |
320 | .B EINVAL | |
321 | .I offset | |
322 | was less than 0, or | |
323 | .I len | |
bea08fec | 324 | .\" FIXME . (raise a kernel bug) Probably the len==0 case should be |
c0c3d019 MK |
325 | .\" a no-op, rather than an error. That would be consistent with |
326 | .\" similar APIs for the len==0 case. | |
327 | .\" See "Re: [PATCH] fallocate.2: add FALLOC_FL_PUNCH_HOLE flag definition" | |
328 | .\" 21 Sep 2012 | |
329 | .\" http://thread.gmane.org/gmane.linux.file-systems/48331/focus=1193526 | |
d9a0b2a5 MK |
330 | was less than or equal to 0. |
331 | .TP | |
1ecfa5b3 MK |
332 | .B EINVAL |
333 | .I mode | |
334 | is | |
335 | .BR FALLOC_FL_COLLAPSE_RANGE | |
336 | and the range specified by | |
337 | .I offset | |
338 | plus | |
339 | .I len | |
340 | reaches or passes the end of the file. | |
341 | .TP | |
342 | .B EINVAL | |
343 | .I mode | |
344 | is | |
7da70a01 NJ |
345 | .BR FALLOC_FL_INSERT_RANGE |
346 | and the range specified by | |
347 | .I offset | |
348 | reaches or passes the end of the file. | |
349 | .TP | |
350 | .B EINVAL | |
351 | .I mode | |
352 | is | |
353 | .BR FALLOC_FL_COLLAPSE_RANGE | |
354 | or | |
355 | .BR FALLOC_FL_INSERT_RANGE , | |
1ecfa5b3 MK |
356 | but either |
357 | .I offset | |
358 | or | |
359 | .I len | |
360 | is not a multiple of the filesystem block size. | |
361 | .TP | |
362 | .B EINVAL | |
718778ad | 363 | .I mode |
ec9a2be0 | 364 | contains one of |
1ecfa5b3 | 365 | .B FALLOC_FL_COLLAPSE_RANGE |
7da70a01 NJ |
366 | or |
367 | .B FALLOC_FL_INSERT_RANGE | |
ec9a2be0 | 368 | and also other flags; |
1ecfa5b3 | 369 | no other flags are permitted with |
7da70a01 NJ |
370 | .BR FALLOC_FL_COLLAPSE_RANGE |
371 | or | |
372 | .BR FALLOC_FL_INSERT_RANGE . | |
718778ad SP |
373 | .TP |
374 | .B EINVAL | |
2af4d967 MK |
375 | .I mode |
376 | is | |
7dc489e3 LC |
377 | .BR FALLOC_FL_COLLAPSE_RANGE |
378 | or | |
7da70a01 NJ |
379 | .BR FALLOC_FL_ZERO_RANGE |
380 | or | |
381 | .BR FALLOC_FL_INSERT_RANGE , | |
2af4d967 MK |
382 | but the file referred to by |
383 | .I fd | |
384 | is not a regular file. | |
6771ab48 | 385 | .\" There was an inconsistency in 3.15-rc1, that should be resolved so that all |
2af4d967 MK |
386 | .\" filesystems use this error for this case. (Tytso says ex4 will change.) |
387 | .\" http://thread.gmane.org/gmane.comp.file-systems.xfs.general/60485/focus=5521 | |
388 | .\" From: Michael Kerrisk (man-pages <mtk.manpages@...> | |
389 | .\" Subject: Re: [PATCH v5 10/10] manpage: update FALLOC_FL_COLLAPSE_RANGE flag in fallocate | |
390 | .\" Newsgroups: gmane.linux.man, gmane.linux.file-systems | |
391 | .\" Date: 2014-04-17 13:40:05 GMT | |
1ecfa5b3 | 392 | .TP |
d9a0b2a5 | 393 | .B EIO |
9ee4a2b6 | 394 | An I/O error occurred while reading from or writing to a filesystem. |
d9a0b2a5 MK |
395 | .TP |
396 | .B ENODEV | |
397 | .I fd | |
398 | does not refer to a regular file or a directory. | |
399 | (If | |
400 | .I fd | |
401 | is a pipe or FIFO, a different error results.) | |
402 | .TP | |
403 | .B ENOSPC | |
404 | There is not enough space left on the device containing the file | |
405 | referred to by | |
406 | .IR fd . | |
407 | .TP | |
408 | .B ENOSYS | |
7cb4d005 MK |
409 | This kernel does not implement |
410 | .BR fallocate (). | |
d9a0b2a5 MK |
411 | .TP |
412 | .B EOPNOTSUPP | |
9ee4a2b6 | 413 | The filesystem containing the file referred to by |
7cb4d005 MK |
414 | .I fd |
415 | does not support this operation; | |
416 | or the | |
d9a0b2a5 | 417 | .I mode |
9ee4a2b6 | 418 | is not supported by the filesystem containing the file referred to by |
d9a0b2a5 | 419 | .IR fd . |
45eb0d22 MK |
420 | .TP |
421 | .B EPERM | |
422 | The file referred to by | |
928b1970 | 423 | .I fd |
45eb0d22 | 424 | is marked immutable (see |
7eb92881 | 425 | .BR chattr (1)). |
95dcbfb5 MK |
426 | .TP |
427 | .B EPERM | |
54d25eca MK |
428 | .I mode |
429 | specifies | |
430 | .BR FALLOC_FL_PUNCH_HOLE | |
1ecfa5b3 MK |
431 | or |
432 | .BR FALLOC_FL_COLLAPSE_RANGE | |
7da70a01 NJ |
433 | or |
434 | .BR FALLOC_FL_INSERT_RANGE | |
54d25eca MK |
435 | and |
436 | the file referred to by | |
437 | .I fd | |
438 | is marked append-only | |
439 | (see | |
928b1970 | 440 | .BR chattr (1)). |
45eb0d22 | 441 | .TP |
fbab10e5 MK |
442 | .B EPERM |
443 | The operation was prevented by a file seal; see | |
444 | .BR fcntl (2). | |
445 | .TP | |
45eb0d22 MK |
446 | .B ESPIPE |
447 | .I fd | |
928b1970 | 448 | refers to a pipe or FIFO. |
1ecfa5b3 MK |
449 | .TP |
450 | .B ETXTBSY | |
451 | .I mode | |
452 | specifies | |
7da70a01 NJ |
453 | .BR FALLOC_FL_COLLAPSE_RANGE |
454 | or | |
455 | .BR FALLOC_FL_INSERT_RANGE , | |
1ecfa5b3 MK |
456 | but the file referred to by |
457 | .IR fd | |
458 | is currently being executed. | |
d9a0b2a5 MK |
459 | .SH VERSIONS |
460 | .BR fallocate () | |
d9a0b2a5 | 461 | is available on Linux since kernel 2.6.23. |
ff6db399 | 462 | Support is provided by glibc since version 2.10. |
7f26805b MK |
463 | The |
464 | .BR FALLOC_FL_* | |
465 | flags are defined in glibc headers only since version 2.18. | |
466 | .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=14964 | |
90e261f2 | 467 | .SH CONFORMING TO |
d9a0b2a5 | 468 | .BR fallocate () |
8382f16d | 469 | is Linux-specific. |
d9a0b2a5 | 470 | .SH SEE ALSO |
1b946741 | 471 | .BR fallocate (1), |
d9a0b2a5 | 472 | .BR ftruncate (2), |
f0c34053 MK |
473 | .BR posix_fadvise (3), |
474 | .BR posix_fallocate (3) |