]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/fallocate.c
Merge branch 'eject-sparc' of https://github.com/mator/util-linux
[thirdparty/util-linux.git] / sys-utils / fallocate.c
CommitLineData
d46a5499
KZ
1/*
2 * fallocate - utility to use the fallocate system call
3 *
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>
7 *
8 * cvtnum routine taken from xfsprogs,
9 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
10 *
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.
14 *
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.
19 *
7cebf0bb
SK
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.
d46a5499
KZ
23 */
24#include <sys/stat.h>
25#include <sys/types.h>
24b2a479 26#include <sys/mman.h>
d46a5499
KZ
27#include <ctype.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include <getopt.h>
6264af59 34#include <limits.h>
24b2a479 35#include <string.h>
d46a5499
KZ
36
37#ifndef HAVE_FALLOCATE
38# include <sys/syscall.h>
39#endif
40
bc5ddf0c 41#if defined(HAVE_LINUX_FALLOC_H) && \
83cc932d 42 (!defined(FALLOC_FL_KEEP_SIZE) || !defined(FALLOC_FL_PUNCH_HOLE) || \
b4390656
FF
43 !defined(FALLOC_FL_COLLAPSE_RANGE) || !defined(FALLOC_FL_ZERO_RANGE) || \
44 !defined(FALLOC_FL_INSERT_RANGE))
bc5ddf0c 45# include <linux/falloc.h> /* non-libc fallback for FALLOC_FL_* flags */
f75b8e5c
KZ
46#endif
47
b7f3f147 48
f75b8e5c 49#ifndef FALLOC_FL_KEEP_SIZE
1fd4f609 50# define FALLOC_FL_KEEP_SIZE 0x1
f75b8e5c
KZ
51#endif
52
53#ifndef FALLOC_FL_PUNCH_HOLE
1fd4f609 54# define FALLOC_FL_PUNCH_HOLE 0x2
fac8b4bd 55#endif
d46a5499 56
83cc932d 57#ifndef FALLOC_FL_COLLAPSE_RANGE
1fd4f609
LC
58# define FALLOC_FL_COLLAPSE_RANGE 0x8
59#endif
60
61#ifndef FALLOC_FL_ZERO_RANGE
62# define FALLOC_FL_ZERO_RANGE 0x10
83cc932d
DP
63#endif
64
b4390656
FF
65#ifndef FALLOC_FL_INSERT_RANGE
66# define FALLOC_FL_INSERT_RANGE 0x20
67#endif
68
d46a5499 69#include "nls.h"
8abcf290 70#include "strutils.h"
eb76ca98 71#include "c.h"
efb8854f 72#include "closestream.h"
24b2a479 73#include "xalloc.h"
b7f3f147 74#include "optutils.h"
d46a5499 75
782c290c
KZ
76static int verbose;
77static char *filename;
78
86be6a32 79static void __attribute__((__noreturn__)) usage(void)
d46a5499 80{
86be6a32 81 FILE *out = stdout;
584aed6b 82 fputs(USAGE_HEADER, out);
130bf041
KZ
83 fprintf(out,
84 _(" %s [options] <filename>\n"), program_invocation_short_name);
14c9b680 85
451dbcfa
BS
86 fputs(USAGE_SEPARATOR, out);
87 fputs(_("Preallocate space to, or deallocate space from a file.\n"), out);
88
89 fputs(USAGE_OPTIONS, out);
14c9b680
PB
90 fputs(_(" -c, --collapse-range remove a range from the file\n"), out);
91 fputs(_(" -d, --dig-holes detect zeroes and replace with holes\n"), out);
b4390656 92 fputs(_(" -i, --insert-range insert a hole at range, shifting existing data\n"), out);
14c9b680
PB
93 fputs(_(" -l, --length <num> length for range operations, in bytes\n"), out);
94 fputs(_(" -n, --keep-size maintain the apparent size of the file\n"), out);
95 fputs(_(" -o, --offset <num> offset for range operations, in bytes\n"), out);
96 fputs(_(" -p, --punch-hole replace a range with a hole (implies -n)\n"), out);
97 fputs(_(" -z, --zero-range zero and ensure allocation of a range\n"), out);
833f9a7a
DC
98#ifdef HAVE_POSIX_FALLOCATE
99 fputs(_(" -x, --posix use posix_fallocate(3) instead of fallocate(2)\n"), out);
100#endif
83cc932d 101 fputs(_(" -v, --verbose verbose mode\n"), out);
782c290c 102
584aed6b 103 fputs(USAGE_SEPARATOR, out);
f45f3ec3 104 printf(USAGE_HELP_OPTIONS(22));
14c9b680 105
f1970cc5
KZ
106 fputs(USAGE_ARGUMENTS, out);
107 printf(USAGE_ARG_SIZE(_("<num>")));
108
f45f3ec3 109 printf(USAGE_MAN_TAIL("fallocate(1)"));
d46a5499 110
86be6a32 111 exit(EXIT_SUCCESS);
d46a5499
KZ
112}
113
d46a5499
KZ
114static loff_t cvtnum(char *s)
115{
3b6b039a 116 uintmax_t x;
6264af59 117
3b6b039a 118 if (strtosize(s, &x))
d46a5499 119 return -1LL;
d46a5499 120
3b6b039a 121 return x;
d46a5499
KZ
122}
123
24b2a479 124static void xfallocate(int fd, int mode, off_t offset, off_t length)
bcd9315d
RC
125{
126 int error;
562adaed 127
bcd9315d
RC
128#ifdef HAVE_FALLOCATE
129 error = fallocate(fd, mode, offset, length);
130#else
131 error = syscall(SYS_fallocate, fd, mode, offset, length);
132#endif
133 /*
134 * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported
135 * ENOSYS: The filesystem does not support sys_fallocate
136 */
137 if (error < 0) {
24b2a479 138 if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP)
049bfa06 139 errx(EXIT_FAILURE, _("fallocate failed: keep size mode is unsupported"));
24b2a479
RC
140 err(EXIT_FAILURE, _("fallocate failed"));
141 }
142}
143
833f9a7a
DC
144#ifdef HAVE_POSIX_FALLOCATE
145static void xposix_fallocate(int fd, off_t offset, off_t length)
146{
147 int error = posix_fallocate(fd, offset, length);
148 if (error < 0) {
149 err(EXIT_FAILURE, _("fallocate failed"));
150 }
151}
152#endif
db5f00bc 153
4b01c5a1
KZ
154/* The real buffer size has to be bufsize + sizeof(uintptr_t) */
155static int is_nul(void *buf, size_t bufsize)
c4172cc3
KZ
156{
157 typedef uintptr_t word;
158 void const *vp;
159 char const *cbuf = buf, *cp;
160 word const *wp = buf;
161
4b01c5a1
KZ
162 /* set sentinel */
163 memset((char *) buf + bufsize, '\1', sizeof(word));
164
c4172cc3
KZ
165 /* Find first nonzero *word*, or the word with the sentinel. */
166 while (*wp++ == 0)
167 continue;
168
169 /* Find the first nonzero *byte*, or the sentinel. */
170 vp = wp - 1;
171 cp = vp;
172
173 while (*cp++ == 0)
174 continue;
175
7ff635bf 176 return cbuf + bufsize < cp;
c4172cc3
KZ
177}
178
173ef882 179static void dig_holes(int fd, off_t file_off, off_t len)
24b2a479 180{
173ef882 181 off_t file_end = len ? file_off + len : 0;
d6cecc3f
KZ
182 off_t hole_start = 0, hole_sz = 0;
183 uintmax_t ct = 0;
0407c1be 184 size_t bufsz;
c4172cc3 185 char *buf;
d6cecc3f 186 struct stat st;
0407c1be 187#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
173ef882 188 off_t cache_start = file_off;
c12eff4c
KZ
189 /*
190 * We don't want to call POSIX_FADV_DONTNEED to discard cached
191 * data in PAGE_SIZE steps. IMHO it's overkill (too many syscalls).
192 *
193 * Let's assume that 1MiB (on system with 4K page size) is just
194 * a good compromise.
195 * -- kzak Feb-2014
196 */
0407c1be
SK
197 const size_t cachesz = getpagesize() * 256;
198#endif
199
200 if (fstat(fd, &st) != 0)
fc14ceba 201 err(EXIT_FAILURE, _("stat of %s failed"), filename);
0407c1be
SK
202
203 bufsz = st.st_blksize;
c12eff4c 204
173ef882 205 if (lseek(fd, file_off, SEEK_SET) < 0)
d6cecc3f
KZ
206 err(EXIT_FAILURE, _("seek on %s failed"), filename);
207
4b01c5a1
KZ
208 /* buffer + extra space for is_nul() sentinel */
209 buf = xmalloc(bufsz + sizeof(uintptr_t));
173ef882
KZ
210 while (file_end == 0 || file_off < file_end) {
211 /*
09cb6b42 212 * Detect data area (skip holes)
173ef882
KZ
213 */
214 off_t end, off;
215
216 off = lseek(fd, file_off, SEEK_DATA);
217 if ((off == -1 && errno == ENXIO) ||
218 (file_end && off >= file_end))
219 break;
220
221 end = lseek(fd, off, SEEK_HOLE);
222 if (file_end && end > file_end)
223 end = file_end;
224
edf9c218
KZ
225 if (off < 0 || end < 0)
226 break;
227
c12eff4c 228#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
edf9c218 229 (void) posix_fadvise(fd, off, end, POSIX_FADV_SEQUENTIAL);
d6cecc3f 230#endif
173ef882
KZ
231 /*
232 * Dig holes in the area
233 */
234 while (off < end) {
235 ssize_t rsz = pread(fd, buf, bufsz, off);
236 if (rsz < 0 && errno)
237 err(EXIT_FAILURE, _("%s: read failed"), filename);
238 if (end && rsz > 0 && off > end - rsz)
239 rsz = end - off;
240 if (rsz <= 0)
241 break;
242
243 if (is_nul(buf, rsz)) {
244 if (!hole_sz) /* new hole detected */
245 hole_start = off;
246 hole_sz += rsz;
247 } else if (hole_sz) {
248 xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,
249 hole_start, hole_sz);
250 ct += hole_sz;
251 hole_sz = hole_start = 0;
252 }
24b2a479 253
173ef882
KZ
254#if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
255 /* discard cached data */
256 if (off - cache_start > (off_t) cachesz) {
257 size_t clen = off - cache_start;
24b2a479 258
173ef882 259 clen = (clen / cachesz) * cachesz;
edf9c218 260 (void) posix_fadvise(fd, cache_start, clen, POSIX_FADV_DONTNEED);
173ef882 261 cache_start = cache_start + clen;
db5f00bc 262 }
173ef882
KZ
263#endif
264 off += rsz;
265 }
266 if (hole_sz) {
d6cecc3f 267 xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE,
173ef882 268 hole_start, hole_sz);
d6cecc3f 269 ct += hole_sz;
d6cecc3f 270 }
173ef882 271 file_off = off;
bcd9315d 272 }
24b2a479 273
24b2a479 274 free(buf);
d6cecc3f
KZ
275
276 if (verbose) {
277 char *str = size_to_human_string(SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, ct);
278 fprintf(stdout, _("%s: %s (%ju bytes) converted to sparse holes.\n"),
279 filename, str, ct);
280 free(str);
281 }
bcd9315d
RC
282}
283
d46a5499
KZ
284int main(int argc, char **argv)
285{
d46a5499 286 int c;
d46a5499
KZ
287 int fd;
288 int mode = 0;
d6cecc3f 289 int dig = 0;
833f9a7a 290 int posix = 0;
d46a5499
KZ
291 loff_t length = -2LL;
292 loff_t offset = 0;
293
6c7d5ae9 294 static const struct option longopts[] = {
87918040
SK
295 { "help", no_argument, NULL, 'h' },
296 { "version", no_argument, NULL, 'V' },
297 { "keep-size", no_argument, NULL, 'n' },
298 { "punch-hole", no_argument, NULL, 'p' },
299 { "collapse-range", no_argument, NULL, 'c' },
300 { "dig-holes", no_argument, NULL, 'd' },
301 { "insert-range", no_argument, NULL, 'i' },
302 { "zero-range", no_argument, NULL, 'z' },
303 { "offset", required_argument, NULL, 'o' },
304 { "length", required_argument, NULL, 'l' },
305 { "posix", no_argument, NULL, 'x' },
306 { "verbose", no_argument, NULL, 'v' },
307 { NULL, 0, NULL, 0 }
d46a5499
KZ
308 };
309
a7349ee3 310 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
b7f3f147
KZ
311 { 'c', 'd', 'p', 'z' },
312 { 'c', 'n' },
833f9a7a 313 { 'x', 'c', 'd', 'i', 'n', 'p', 'z'},
b7f3f147
KZ
314 { 0 }
315 };
316 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
317
d46a5499
KZ
318 setlocale(LC_ALL, "");
319 bindtextdomain(PACKAGE, LOCALEDIR);
320 textdomain(PACKAGE);
2c308875 321 close_stdout_atexit();
d46a5499 322
833f9a7a 323 while ((c = getopt_long(argc, argv, "hvVncpdizxl:o:", longopts, NULL))
83cc932d 324 != -1) {
b7f3f147
KZ
325
326 err_exclusive_options(c, longopts, excl, excl_st);
327
d46a5499 328 switch(c) {
83cc932d
DP
329 case 'c':
330 mode |= FALLOC_FL_COLLAPSE_RANGE;
331 break;
24b2a479 332 case 'd':
d6cecc3f 333 dig = 1;
24b2a479 334 break;
b4390656
FF
335 case 'i':
336 mode |= FALLOC_FL_INSERT_RANGE;
337 break;
d46a5499
KZ
338 case 'l':
339 length = cvtnum(optarg);
340 break;
b7f3f147
KZ
341 case 'n':
342 mode |= FALLOC_FL_KEEP_SIZE;
343 break;
d46a5499
KZ
344 case 'o':
345 offset = cvtnum(optarg);
346 break;
b7f3f147
KZ
347 case 'p':
348 mode |= FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
349 break;
350 case 'z':
351 mode |= FALLOC_FL_ZERO_RANGE;
352 break;
833f9a7a
DC
353 case 'x':
354#ifdef HAVE_POSIX_FALLOCATE
355 posix = 1;
356 break;
357#else
f1a7cfcb 358 errx(EXIT_FAILURE, _("posix_fallocate support is not compiled"));
833f9a7a 359#endif
782c290c
KZ
360 case 'v':
361 verbose++;
362 break;
2c308875
KZ
363
364 case 'h':
365 usage();
b7f3f147 366 case 'V':
2c308875 367 print_version(EXIT_SUCCESS);
d46a5499 368 default:
677ec86c 369 errtryhelp(EXIT_FAILURE);
d46a5499
KZ
370 }
371 }
38a5440c
BV
372
373 if (optind == argc)
1d231190 374 errx(EXIT_FAILURE, _("no filename specified"));
38a5440c
BV
375
376 filename = argv[optind++];
377
378 if (optind != argc)
379 errx(EXIT_FAILURE, _("unexpected number of arguments"));
380
d6cecc3f 381 if (dig) {
dac1cb53 382 /* for --dig-holes the default is analyze all file */
d6cecc3f
KZ
383 if (length == -2LL)
384 length = 0;
385 if (length < 0)
386 errx(EXIT_FAILURE, _("invalid length value specified"));
387 } else {
dac1cb53 388 /* it's safer to require the range specification (--length --offset) */
d6cecc3f
KZ
389 if (length == -2LL)
390 errx(EXIT_FAILURE, _("no length argument specified"));
391 if (length <= 0)
392 errx(EXIT_FAILURE, _("invalid length value specified"));
393 }
d46a5499
KZ
394 if (offset < 0)
395 errx(EXIT_FAILURE, _("invalid offset value specified"));
fd1ee3b9 396
575718a0
KZ
397 /* O_CREAT makes sense only for the default fallocate(2) behavior
398 * when mode is no specified and new space is allocated */
185aa9e5
KZ
399 fd = open(filename, O_RDWR | (!dig && !mode ? O_CREAT : 0),
400 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
d46a5499 401 if (fd < 0)
782c290c 402 err(EXIT_FAILURE, _("cannot open %s"), filename);
d46a5499 403
d6cecc3f
KZ
404 if (dig)
405 dig_holes(fd, offset, length);
833f9a7a
DC
406#ifdef HAVE_POSIX_FALLOCATE
407 else if (posix)
408 xposix_fallocate(fd, offset, length);
409#endif
d6cecc3f 410 else
24b2a479 411 xfallocate(fd, mode, offset, length);
d46a5499 412
5f52af50 413 if (close_fd(fd) != 0)
782c290c 414 err(EXIT_FAILURE, _("write failed: %s"), filename);
24b2a479 415
d46a5499
KZ
416 return EXIT_SUCCESS;
417}