2 * fallocate - utility to use the fallocate system call
4 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
5 * Written by Eric Sandeen <sandeen@redhat.com>
6 * Karel Zak <kzak@redhat.com>
8 * cvtnum routine taken from xfsprogs,
9 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it would be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
37 #ifndef HAVE_FALLOCATE
38 # include <sys/syscall.h>
41 #if defined(HAVE_LINUX_FALLOC_H) && \
42 (!defined(FALLOC_FL_KEEP_SIZE) || !defined(FALLOC_FL_PUNCH_HOLE) || \
43 !defined(FALLOC_FL_COLLAPSE_RANGE) || !defined(FALLOC_FL_ZERO_RANGE) || \
44 !defined(FALLOC_FL_INSERT_RANGE))
45 # include <linux/falloc.h> /* non-libc fallback for FALLOC_FL_* flags */
49 #ifndef FALLOC_FL_KEEP_SIZE
50 # define FALLOC_FL_KEEP_SIZE 0x1
53 #ifndef FALLOC_FL_PUNCH_HOLE
54 # define FALLOC_FL_PUNCH_HOLE 0x2
57 #ifndef FALLOC_FL_COLLAPSE_RANGE
58 # define FALLOC_FL_COLLAPSE_RANGE 0x8
61 #ifndef FALLOC_FL_ZERO_RANGE
62 # define FALLOC_FL_ZERO_RANGE 0x10
65 #ifndef FALLOC_FL_INSERT_RANGE
66 # define FALLOC_FL_INSERT_RANGE 0x20
72 #include "closestream.h"
77 static char *filename
;
79 static void __attribute__((__noreturn__
)) usage(FILE *out
)
81 fputs(USAGE_HEADER
, out
);
83 _(" %s [options] <filename>\n"), program_invocation_short_name
);
85 fputs(USAGE_SEPARATOR
, out
);
86 fputs(_("Preallocate space to, or deallocate space from a file.\n"), out
);
88 fputs(USAGE_OPTIONS
, out
);
89 fputs(_(" -c, --collapse-range remove a range from the file\n"), out
);
90 fputs(_(" -d, --dig-holes detect zeroes and replace with holes\n"), out
);
91 fputs(_(" -i, --insert-range insert a hole at range, shifting existing data\n"), out
);
92 fputs(_(" -l, --length <num> length for range operations, in bytes\n"), out
);
93 fputs(_(" -n, --keep-size maintain the apparent size of the file\n"), out
);
94 fputs(_(" -o, --offset <num> offset for range operations, in bytes\n"), out
);
95 fputs(_(" -p, --punch-hole replace a range with a hole (implies -n)\n"), out
);
96 fputs(_(" -z, --zero-range zero and ensure allocation of a range\n"), out
);
97 #ifdef HAVE_POSIX_FALLOCATE
98 fputs(_(" -x, --posix use posix_fallocate(3) instead of fallocate(2)\n"), out
);
100 fputs(_(" -v, --verbose verbose mode\n"), out
);
102 fputs(USAGE_SEPARATOR
, out
);
103 fputs(USAGE_HELP
, out
);
104 fputs(USAGE_VERSION
, out
);
106 fprintf(out
, USAGE_MAN_TAIL("fallocate(1)"));
108 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
111 static loff_t
cvtnum(char *s
)
115 if (strtosize(s
, &x
))
121 static void xfallocate(int fd
, int mode
, off_t offset
, off_t length
)
124 #ifdef HAVE_FALLOCATE
125 error
= fallocate(fd
, mode
, offset
, length
);
127 error
= syscall(SYS_fallocate
, fd
, mode
, offset
, length
);
130 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
131 * ENOSYS: The filesystem does not support sys_fallocate
134 if ((mode
& FALLOC_FL_KEEP_SIZE
) && errno
== EOPNOTSUPP
)
135 errx(EXIT_FAILURE
, _("fallocate failed: keep size mode is unsupported"));
136 err(EXIT_FAILURE
, _("fallocate failed"));
140 #ifdef HAVE_POSIX_FALLOCATE
141 static void xposix_fallocate(int fd
, off_t offset
, off_t length
)
143 int error
= posix_fallocate(fd
, offset
, length
);
145 err(EXIT_FAILURE
, _("fallocate failed"));
150 static int skip_hole(int fd
, off_t
*off
)
155 newoff
= lseek(fd
, *off
, SEEK_DATA
);
157 /* ENXIO means that there is no more data -- probably sparse hole at
158 * the end of the file */
159 if (newoff
< 0 && errno
== ENXIO
)
164 return 0; /* success */
166 return -1; /* no hole */
169 /* The real buffer size has to be bufsize + sizeof(uintptr_t) */
170 static int is_nul(void *buf
, size_t bufsize
)
172 typedef uintptr_t word
;
174 char const *cbuf
= buf
, *cp
;
175 word
const *wp
= buf
;
178 memset((char *) buf
+ bufsize
, '\1', sizeof(word
));
180 /* Find first nonzero *word*, or the word with the sentinel. */
184 /* Find the first nonzero *byte*, or the sentinel. */
191 return cbuf
+ bufsize
< cp
;
194 static void dig_holes(int fd
, off_t off
, off_t len
)
196 off_t end
= len
? off
+ len
: 0;
197 off_t hole_start
= 0, hole_sz
= 0;
202 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
203 off_t cache_start
= off
;
205 * We don't want to call POSIX_FADV_DONTNEED to discard cached
206 * data in PAGE_SIZE steps. IMHO it's overkill (too many syscalls).
208 * Let's assume that 1MiB (on system with 4K page size) is just
212 const size_t cachesz
= getpagesize() * 256;
215 if (fstat(fd
, &st
) != 0)
216 err(EXIT_FAILURE
, _("stat of %s failed"), filename
);
218 bufsz
= st
.st_blksize
;
220 if (lseek(fd
, off
, SEEK_SET
) < 0)
221 err(EXIT_FAILURE
, _("seek on %s failed"), filename
);
223 /* buffer + extra space for is_nul() sentinel */
224 buf
= xmalloc(bufsz
+ sizeof(uintptr_t));
225 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
226 posix_fadvise(fd
, off
, 0, POSIX_FADV_SEQUENTIAL
);
229 while (end
== 0 || off
< end
) {
232 rsz
= pread(fd
, buf
, bufsz
, off
);
233 if (rsz
< 0 && errno
)
234 err(EXIT_FAILURE
, _("%s: read failed"), filename
);
235 if (end
&& rsz
> 0 && off
> end
- rsz
)
240 if (is_nul(buf
, rsz
)) {
241 if (!hole_sz
) { /* new hole detected */
242 int rc
= skip_hole(fd
, &off
);
244 continue; /* hole skipped */
246 break; /* end of file */
250 } else if (hole_sz
) {
251 xfallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
,
252 hole_start
, hole_sz
);
254 hole_sz
= hole_start
= 0;
257 #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
258 /* discard cached data */
259 if (off
- cache_start
> (off_t
) cachesz
) {
260 size_t clen
= off
- cache_start
;
262 clen
= (clen
/ cachesz
) * cachesz
;
263 posix_fadvise(fd
, cache_start
, clen
, POSIX_FADV_DONTNEED
);
264 cache_start
= cache_start
+ clen
;
271 xfallocate(fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
,
272 hole_start
, hole_sz
);
279 char *str
= size_to_human_string(SIZE_SUFFIX_3LETTER
| SIZE_SUFFIX_SPACE
, ct
);
280 fprintf(stdout
, _("%s: %s (%ju bytes) converted to sparse holes.\n"),
286 int main(int argc
, char **argv
)
293 loff_t length
= -2LL;
296 static const struct option longopts
[] = {
297 { "help", 0, 0, 'h' },
298 { "version", 0, 0, 'V' },
299 { "keep-size", 0, 0, 'n' },
300 { "punch-hole", 0, 0, 'p' },
301 { "collapse-range", 0, 0, 'c' },
302 { "dig-holes", 0, 0, 'd' },
303 { "insert-range", 0, 0, 'i' },
304 { "zero-range", 0, 0, 'z' },
305 { "offset", 1, 0, 'o' },
306 { "length", 1, 0, 'l' },
307 { "posix", 0, 0, 'x' },
308 { "verbose", 0, 0, 'v' },
312 static const ul_excl_t excl
[] = { /* rows and cols in in ASCII order */
313 { 'c', 'd', 'p', 'z' },
315 { 'x', 'c', 'd', 'i', 'n', 'p', 'z'},
318 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
320 setlocale(LC_ALL
, "");
321 bindtextdomain(PACKAGE
, LOCALEDIR
);
323 atexit(close_stdout
);
325 while ((c
= getopt_long(argc
, argv
, "hvVncpdizxl:o:", longopts
, NULL
))
328 err_exclusive_options(c
, longopts
, excl
, excl_st
);
335 mode
|= FALLOC_FL_COLLAPSE_RANGE
;
341 mode
|= FALLOC_FL_INSERT_RANGE
;
344 length
= cvtnum(optarg
);
347 mode
|= FALLOC_FL_KEEP_SIZE
;
350 offset
= cvtnum(optarg
);
353 mode
|= FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
;
356 mode
|= FALLOC_FL_ZERO_RANGE
;
359 #ifdef HAVE_POSIX_FALLOCATE
363 errx(EXIT_FAILURE
, _("posix_fallocate support is not compiled"))
369 printf(UTIL_LINUX_VERSION
);
372 errtryhelp(EXIT_FAILURE
);
377 errx(EXIT_FAILURE
, _("no filename specified"));
379 filename
= argv
[optind
++];
382 errx(EXIT_FAILURE
, _("unexpected number of arguments"));
385 /* for --dig-holes the default is analyze all file */
389 errx(EXIT_FAILURE
, _("invalid length value specified"));
391 /* it's safer to require the range specification (--length --offset) */
393 errx(EXIT_FAILURE
, _("no length argument specified"));
395 errx(EXIT_FAILURE
, _("invalid length value specified"));
398 errx(EXIT_FAILURE
, _("invalid offset value specified"));
400 /* O_CREAT makes sense only for the default fallocate(2) behavior
401 * when mode is no specified and new space is allocated */
402 fd
= open(filename
, O_RDWR
| (!dig
&& !mode
? O_CREAT
: 0),
403 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
405 err(EXIT_FAILURE
, _("cannot open %s"), filename
);
408 dig_holes(fd
, offset
, length
);
409 #ifdef HAVE_POSIX_FALLOCATE
411 xposix_fallocate(fd
, offset
, length
);
414 xfallocate(fd
, mode
, offset
, length
);
416 if (close_fd(fd
) != 0)
417 err(EXIT_FAILURE
, _("write failed: %s"), filename
);