]>
Commit | Line | Data |
---|---|---|
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 |
76 | static int verbose; |
77 | static char *filename; | |
78 | ||
86be6a32 | 79 | static 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 | |
f45f3ec3 | 106 | printf(USAGE_MAN_TAIL("fallocate(1)")); |
d46a5499 | 107 | |
86be6a32 | 108 | exit(EXIT_SUCCESS); |
d46a5499 KZ |
109 | } |
110 | ||
d46a5499 KZ |
111 | static loff_t cvtnum(char *s) |
112 | { | |
3b6b039a | 113 | uintmax_t x; |
6264af59 | 114 | |
3b6b039a | 115 | if (strtosize(s, &x)) |
d46a5499 | 116 | return -1LL; |
d46a5499 | 117 | |
3b6b039a | 118 | return x; |
d46a5499 KZ |
119 | } |
120 | ||
24b2a479 | 121 | static void xfallocate(int fd, int mode, off_t offset, off_t length) |
bcd9315d RC |
122 | { |
123 | int error; | |
bcd9315d RC |
124 | #ifdef HAVE_FALLOCATE |
125 | error = fallocate(fd, mode, offset, length); | |
126 | #else | |
127 | error = syscall(SYS_fallocate, fd, mode, offset, length); | |
128 | #endif | |
129 | /* | |
130 | * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported | |
131 | * ENOSYS: The filesystem does not support sys_fallocate | |
132 | */ | |
133 | if (error < 0) { | |
24b2a479 | 134 | if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP) |
049bfa06 | 135 | errx(EXIT_FAILURE, _("fallocate failed: keep size mode is unsupported")); |
24b2a479 RC |
136 | err(EXIT_FAILURE, _("fallocate failed")); |
137 | } | |
138 | } | |
139 | ||
833f9a7a DC |
140 | #ifdef HAVE_POSIX_FALLOCATE |
141 | static void xposix_fallocate(int fd, off_t offset, off_t length) | |
142 | { | |
143 | int error = posix_fallocate(fd, offset, length); | |
144 | if (error < 0) { | |
145 | err(EXIT_FAILURE, _("fallocate failed")); | |
146 | } | |
147 | } | |
148 | #endif | |
db5f00bc KZ |
149 | |
150 | static int skip_hole(int fd, off_t *off) | |
151 | { | |
152 | off_t newoff; | |
153 | ||
154 | errno = 0; | |
155 | newoff = lseek(fd, *off, SEEK_DATA); | |
156 | ||
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) | |
160 | return 1; | |
161 | ||
162 | if (newoff > *off) { | |
163 | *off = newoff; | |
164 | return 0; /* success */ | |
165 | } | |
166 | return -1; /* no hole */ | |
167 | } | |
168 | ||
4b01c5a1 KZ |
169 | /* The real buffer size has to be bufsize + sizeof(uintptr_t) */ |
170 | static int is_nul(void *buf, size_t bufsize) | |
c4172cc3 KZ |
171 | { |
172 | typedef uintptr_t word; | |
173 | void const *vp; | |
174 | char const *cbuf = buf, *cp; | |
175 | word const *wp = buf; | |
176 | ||
4b01c5a1 KZ |
177 | /* set sentinel */ |
178 | memset((char *) buf + bufsize, '\1', sizeof(word)); | |
179 | ||
c4172cc3 KZ |
180 | /* Find first nonzero *word*, or the word with the sentinel. */ |
181 | while (*wp++ == 0) | |
182 | continue; | |
183 | ||
184 | /* Find the first nonzero *byte*, or the sentinel. */ | |
185 | vp = wp - 1; | |
186 | cp = vp; | |
187 | ||
188 | while (*cp++ == 0) | |
189 | continue; | |
190 | ||
7ff635bf | 191 | return cbuf + bufsize < cp; |
c4172cc3 KZ |
192 | } |
193 | ||
d6cecc3f | 194 | static void dig_holes(int fd, off_t off, off_t len) |
24b2a479 | 195 | { |
d6cecc3f KZ |
196 | off_t end = len ? off + len : 0; |
197 | off_t hole_start = 0, hole_sz = 0; | |
198 | uintmax_t ct = 0; | |
0407c1be | 199 | size_t bufsz; |
c4172cc3 | 200 | char *buf; |
d6cecc3f | 201 | struct stat st; |
0407c1be SK |
202 | #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE) |
203 | off_t cache_start = off; | |
c12eff4c KZ |
204 | /* |
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). | |
207 | * | |
208 | * Let's assume that 1MiB (on system with 4K page size) is just | |
209 | * a good compromise. | |
210 | * -- kzak Feb-2014 | |
211 | */ | |
0407c1be SK |
212 | const size_t cachesz = getpagesize() * 256; |
213 | #endif | |
214 | ||
215 | if (fstat(fd, &st) != 0) | |
fc14ceba | 216 | err(EXIT_FAILURE, _("stat of %s failed"), filename); |
0407c1be SK |
217 | |
218 | bufsz = st.st_blksize; | |
c12eff4c | 219 | |
d6cecc3f KZ |
220 | if (lseek(fd, off, SEEK_SET) < 0) |
221 | err(EXIT_FAILURE, _("seek on %s failed"), filename); | |
222 | ||
4b01c5a1 KZ |
223 | /* buffer + extra space for is_nul() sentinel */ |
224 | buf = xmalloc(bufsz + sizeof(uintptr_t)); | |
c12eff4c KZ |
225 | #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE) |
226 | posix_fadvise(fd, off, 0, POSIX_FADV_SEQUENTIAL); | |
d6cecc3f | 227 | #endif |
24b2a479 | 228 | |
d6cecc3f KZ |
229 | while (end == 0 || off < end) { |
230 | ssize_t rsz; | |
24b2a479 | 231 | |
d6cecc3f KZ |
232 | rsz = pread(fd, buf, bufsz, off); |
233 | if (rsz < 0 && errno) | |
234 | err(EXIT_FAILURE, _("%s: read failed"), filename); | |
c4172cc3 | 235 | if (end && rsz > 0 && off > end - rsz) |
d6cecc3f KZ |
236 | rsz = end - off; |
237 | if (rsz <= 0) | |
238 | break; | |
24b2a479 | 239 | |
c4172cc3 | 240 | if (is_nul(buf, rsz)) { |
db5f00bc | 241 | if (!hole_sz) { /* new hole detected */ |
c4172cc3 KZ |
242 | int rc = skip_hole(fd, &off); |
243 | if (rc == 0) | |
244 | continue; /* hole skipped */ | |
245 | else if (rc == 1) | |
246 | break; /* end of file */ | |
d6cecc3f | 247 | hole_start = off; |
db5f00bc | 248 | } |
d6cecc3f KZ |
249 | hole_sz += rsz; |
250 | } else if (hole_sz) { | |
251 | xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, | |
252 | hole_start, hole_sz); | |
253 | ct += hole_sz; | |
254 | hole_sz = hole_start = 0; | |
255 | } | |
c12eff4c KZ |
256 | |
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; | |
261 | ||
262 | clen = (clen / cachesz) * cachesz; | |
263 | posix_fadvise(fd, cache_start, clen, POSIX_FADV_DONTNEED); | |
264 | cache_start = cache_start + clen; | |
265 | } | |
266 | #endif | |
d6cecc3f KZ |
267 | off += rsz; |
268 | } | |
782c290c | 269 | |
d6cecc3f | 270 | if (hole_sz) { |
24b2a479 | 271 | xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, |
d6cecc3f KZ |
272 | hole_start, hole_sz); |
273 | ct += hole_sz; | |
bcd9315d | 274 | } |
24b2a479 | 275 | |
24b2a479 | 276 | free(buf); |
d6cecc3f KZ |
277 | |
278 | if (verbose) { | |
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"), | |
281 | filename, str, ct); | |
282 | free(str); | |
283 | } | |
bcd9315d RC |
284 | } |
285 | ||
d46a5499 KZ |
286 | int main(int argc, char **argv) |
287 | { | |
d46a5499 | 288 | int c; |
d46a5499 KZ |
289 | int fd; |
290 | int mode = 0; | |
d6cecc3f | 291 | int dig = 0; |
833f9a7a | 292 | int posix = 0; |
d46a5499 KZ |
293 | loff_t length = -2LL; |
294 | loff_t offset = 0; | |
295 | ||
6c7d5ae9 | 296 | static const struct option longopts[] = { |
87918040 SK |
297 | { "help", no_argument, NULL, 'h' }, |
298 | { "version", no_argument, NULL, 'V' }, | |
299 | { "keep-size", no_argument, NULL, 'n' }, | |
300 | { "punch-hole", no_argument, NULL, 'p' }, | |
301 | { "collapse-range", no_argument, NULL, 'c' }, | |
302 | { "dig-holes", no_argument, NULL, 'd' }, | |
303 | { "insert-range", no_argument, NULL, 'i' }, | |
304 | { "zero-range", no_argument, NULL, 'z' }, | |
305 | { "offset", required_argument, NULL, 'o' }, | |
306 | { "length", required_argument, NULL, 'l' }, | |
307 | { "posix", no_argument, NULL, 'x' }, | |
308 | { "verbose", no_argument, NULL, 'v' }, | |
309 | { NULL, 0, NULL, 0 } | |
d46a5499 KZ |
310 | }; |
311 | ||
a7349ee3 | 312 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
b7f3f147 KZ |
313 | { 'c', 'd', 'p', 'z' }, |
314 | { 'c', 'n' }, | |
833f9a7a | 315 | { 'x', 'c', 'd', 'i', 'n', 'p', 'z'}, |
b7f3f147 KZ |
316 | { 0 } |
317 | }; | |
318 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
319 | ||
d46a5499 KZ |
320 | setlocale(LC_ALL, ""); |
321 | bindtextdomain(PACKAGE, LOCALEDIR); | |
322 | textdomain(PACKAGE); | |
efb8854f | 323 | atexit(close_stdout); |
d46a5499 | 324 | |
833f9a7a | 325 | while ((c = getopt_long(argc, argv, "hvVncpdizxl:o:", longopts, NULL)) |
83cc932d | 326 | != -1) { |
b7f3f147 KZ |
327 | |
328 | err_exclusive_options(c, longopts, excl, excl_st); | |
329 | ||
d46a5499 KZ |
330 | switch(c) { |
331 | case 'h': | |
86be6a32 | 332 | usage(); |
d46a5499 | 333 | break; |
83cc932d DP |
334 | case 'c': |
335 | mode |= FALLOC_FL_COLLAPSE_RANGE; | |
336 | break; | |
24b2a479 | 337 | case 'd': |
d6cecc3f | 338 | dig = 1; |
24b2a479 | 339 | break; |
b4390656 FF |
340 | case 'i': |
341 | mode |= FALLOC_FL_INSERT_RANGE; | |
342 | break; | |
d46a5499 KZ |
343 | case 'l': |
344 | length = cvtnum(optarg); | |
345 | break; | |
b7f3f147 KZ |
346 | case 'n': |
347 | mode |= FALLOC_FL_KEEP_SIZE; | |
348 | break; | |
d46a5499 KZ |
349 | case 'o': |
350 | offset = cvtnum(optarg); | |
351 | break; | |
b7f3f147 KZ |
352 | case 'p': |
353 | mode |= FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; | |
354 | break; | |
355 | case 'z': | |
356 | mode |= FALLOC_FL_ZERO_RANGE; | |
357 | break; | |
833f9a7a DC |
358 | case 'x': |
359 | #ifdef HAVE_POSIX_FALLOCATE | |
360 | posix = 1; | |
361 | break; | |
362 | #else | |
363 | errx(EXIT_FAILURE, _("posix_fallocate support is not compiled")) | |
364 | #endif | |
782c290c KZ |
365 | case 'v': |
366 | verbose++; | |
367 | break; | |
b7f3f147 KZ |
368 | case 'V': |
369 | printf(UTIL_LINUX_VERSION); | |
370 | return EXIT_SUCCESS; | |
d46a5499 | 371 | default: |
677ec86c | 372 | errtryhelp(EXIT_FAILURE); |
d46a5499 KZ |
373 | } |
374 | } | |
38a5440c BV |
375 | |
376 | if (optind == argc) | |
1d231190 | 377 | errx(EXIT_FAILURE, _("no filename specified")); |
38a5440c BV |
378 | |
379 | filename = argv[optind++]; | |
380 | ||
381 | if (optind != argc) | |
382 | errx(EXIT_FAILURE, _("unexpected number of arguments")); | |
383 | ||
d6cecc3f | 384 | if (dig) { |
dac1cb53 | 385 | /* for --dig-holes the default is analyze all file */ |
d6cecc3f KZ |
386 | if (length == -2LL) |
387 | length = 0; | |
388 | if (length < 0) | |
389 | errx(EXIT_FAILURE, _("invalid length value specified")); | |
390 | } else { | |
dac1cb53 | 391 | /* it's safer to require the range specification (--length --offset) */ |
d6cecc3f KZ |
392 | if (length == -2LL) |
393 | errx(EXIT_FAILURE, _("no length argument specified")); | |
394 | if (length <= 0) | |
395 | errx(EXIT_FAILURE, _("invalid length value specified")); | |
396 | } | |
d46a5499 KZ |
397 | if (offset < 0) |
398 | errx(EXIT_FAILURE, _("invalid offset value specified")); | |
fd1ee3b9 | 399 | |
575718a0 KZ |
400 | /* O_CREAT makes sense only for the default fallocate(2) behavior |
401 | * when mode is no specified and new space is allocated */ | |
185aa9e5 KZ |
402 | fd = open(filename, O_RDWR | (!dig && !mode ? O_CREAT : 0), |
403 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
d46a5499 | 404 | if (fd < 0) |
782c290c | 405 | err(EXIT_FAILURE, _("cannot open %s"), filename); |
d46a5499 | 406 | |
d6cecc3f KZ |
407 | if (dig) |
408 | dig_holes(fd, offset, length); | |
833f9a7a DC |
409 | #ifdef HAVE_POSIX_FALLOCATE |
410 | else if (posix) | |
411 | xposix_fallocate(fd, offset, length); | |
412 | #endif | |
d6cecc3f | 413 | else |
24b2a479 | 414 | xfallocate(fd, mode, offset, length); |
d46a5499 | 415 | |
5f52af50 | 416 | if (close_fd(fd) != 0) |
782c290c | 417 | err(EXIT_FAILURE, _("write failed: %s"), filename); |
24b2a479 | 418 | |
d46a5499 KZ |
419 | return EXIT_SUCCESS; |
420 | } |