]>
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) || \ |
1fd4f609 | 43 | !defined(FALLOC_FL_COLLAPSE_RANGE) || !defined(FALLOC_FL_ZERO_RANGE)) |
bc5ddf0c | 44 | # include <linux/falloc.h> /* non-libc fallback for FALLOC_FL_* flags */ |
f75b8e5c KZ |
45 | #endif |
46 | ||
b7f3f147 | 47 | |
f75b8e5c | 48 | #ifndef FALLOC_FL_KEEP_SIZE |
1fd4f609 | 49 | # define FALLOC_FL_KEEP_SIZE 0x1 |
f75b8e5c KZ |
50 | #endif |
51 | ||
52 | #ifndef FALLOC_FL_PUNCH_HOLE | |
1fd4f609 | 53 | # define FALLOC_FL_PUNCH_HOLE 0x2 |
fac8b4bd | 54 | #endif |
d46a5499 | 55 | |
83cc932d | 56 | #ifndef FALLOC_FL_COLLAPSE_RANGE |
1fd4f609 LC |
57 | # define FALLOC_FL_COLLAPSE_RANGE 0x8 |
58 | #endif | |
59 | ||
60 | #ifndef FALLOC_FL_ZERO_RANGE | |
61 | # define FALLOC_FL_ZERO_RANGE 0x10 | |
83cc932d DP |
62 | #endif |
63 | ||
d46a5499 | 64 | #include "nls.h" |
8abcf290 | 65 | #include "strutils.h" |
eb76ca98 | 66 | #include "c.h" |
efb8854f | 67 | #include "closestream.h" |
24b2a479 | 68 | #include "xalloc.h" |
b7f3f147 | 69 | #include "optutils.h" |
d46a5499 | 70 | |
782c290c KZ |
71 | static int verbose; |
72 | static char *filename; | |
73 | ||
d46a5499 KZ |
74 | static void __attribute__((__noreturn__)) usage(FILE *out) |
75 | { | |
584aed6b | 76 | fputs(USAGE_HEADER, out); |
130bf041 KZ |
77 | fprintf(out, |
78 | _(" %s [options] <filename>\n"), program_invocation_short_name); | |
584aed6b | 79 | fputs(USAGE_OPTIONS, out); |
14c9b680 PB |
80 | |
81 | fputs(_(" -c, --collapse-range remove a range from the file\n"), out); | |
82 | fputs(_(" -d, --dig-holes detect zeroes and replace with holes\n"), out); | |
83 | fputs(_(" -l, --length <num> length for range operations, in bytes\n"), out); | |
84 | fputs(_(" -n, --keep-size maintain the apparent size of the file\n"), out); | |
85 | fputs(_(" -o, --offset <num> offset for range operations, in bytes\n"), out); | |
86 | fputs(_(" -p, --punch-hole replace a range with a hole (implies -n)\n"), out); | |
87 | fputs(_(" -z, --zero-range zero and ensure allocation of a range\n"), out); | |
83cc932d | 88 | fputs(_(" -v, --verbose verbose mode\n"), out); |
782c290c | 89 | |
584aed6b SK |
90 | fputs(USAGE_SEPARATOR, out); |
91 | fputs(USAGE_HELP, out); | |
92 | fputs(USAGE_VERSION, out); | |
14c9b680 | 93 | |
584aed6b | 94 | fprintf(out, USAGE_MAN_TAIL("fallocate(1)")); |
d46a5499 KZ |
95 | |
96 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
97 | } | |
98 | ||
d46a5499 KZ |
99 | static loff_t cvtnum(char *s) |
100 | { | |
3b6b039a | 101 | uintmax_t x; |
6264af59 | 102 | |
3b6b039a | 103 | if (strtosize(s, &x)) |
d46a5499 | 104 | return -1LL; |
d46a5499 | 105 | |
3b6b039a | 106 | return x; |
d46a5499 KZ |
107 | } |
108 | ||
24b2a479 | 109 | static void xfallocate(int fd, int mode, off_t offset, off_t length) |
bcd9315d RC |
110 | { |
111 | int error; | |
bcd9315d RC |
112 | #ifdef HAVE_FALLOCATE |
113 | error = fallocate(fd, mode, offset, length); | |
114 | #else | |
115 | error = syscall(SYS_fallocate, fd, mode, offset, length); | |
116 | #endif | |
117 | /* | |
118 | * EOPNOTSUPP: The FALLOC_FL_KEEP_SIZE is unsupported | |
119 | * ENOSYS: The filesystem does not support sys_fallocate | |
120 | */ | |
121 | if (error < 0) { | |
24b2a479 RC |
122 | if ((mode & FALLOC_FL_KEEP_SIZE) && errno == EOPNOTSUPP) |
123 | errx(EXIT_FAILURE, _("keep size mode (-n option) unsupported")); | |
124 | err(EXIT_FAILURE, _("fallocate failed")); | |
125 | } | |
126 | } | |
127 | ||
db5f00bc KZ |
128 | |
129 | static int skip_hole(int fd, off_t *off) | |
130 | { | |
131 | off_t newoff; | |
132 | ||
133 | errno = 0; | |
134 | newoff = lseek(fd, *off, SEEK_DATA); | |
135 | ||
136 | /* ENXIO means that there is no more data -- probably sparse hole at | |
137 | * the end of the file */ | |
138 | if (newoff < 0 && errno == ENXIO) | |
139 | return 1; | |
140 | ||
141 | if (newoff > *off) { | |
142 | *off = newoff; | |
143 | return 0; /* success */ | |
144 | } | |
145 | return -1; /* no hole */ | |
146 | } | |
147 | ||
4b01c5a1 KZ |
148 | /* The real buffer size has to be bufsize + sizeof(uintptr_t) */ |
149 | static int is_nul(void *buf, size_t bufsize) | |
c4172cc3 KZ |
150 | { |
151 | typedef uintptr_t word; | |
152 | void const *vp; | |
153 | char const *cbuf = buf, *cp; | |
154 | word const *wp = buf; | |
155 | ||
4b01c5a1 KZ |
156 | /* set sentinel */ |
157 | memset((char *) buf + bufsize, '\1', sizeof(word)); | |
158 | ||
c4172cc3 KZ |
159 | /* Find first nonzero *word*, or the word with the sentinel. */ |
160 | while (*wp++ == 0) | |
161 | continue; | |
162 | ||
163 | /* Find the first nonzero *byte*, or the sentinel. */ | |
164 | vp = wp - 1; | |
165 | cp = vp; | |
166 | ||
167 | while (*cp++ == 0) | |
168 | continue; | |
169 | ||
170 | return cbuf + bufsize < cp; | |
171 | } | |
172 | ||
d6cecc3f | 173 | static void dig_holes(int fd, off_t off, off_t len) |
24b2a479 | 174 | { |
d6cecc3f KZ |
175 | off_t end = len ? off + len : 0; |
176 | off_t hole_start = 0, hole_sz = 0; | |
177 | uintmax_t ct = 0; | |
0407c1be | 178 | size_t bufsz; |
c4172cc3 | 179 | char *buf; |
d6cecc3f | 180 | struct stat st; |
0407c1be SK |
181 | #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE) |
182 | off_t cache_start = off; | |
c12eff4c KZ |
183 | /* |
184 | * We don't want to call POSIX_FADV_DONTNEED to discard cached | |
185 | * data in PAGE_SIZE steps. IMHO it's overkill (too many syscalls). | |
186 | * | |
187 | * Let's assume that 1MiB (on system with 4K page size) is just | |
188 | * a good compromise. | |
189 | * -- kzak Feb-2014 | |
190 | */ | |
0407c1be SK |
191 | const size_t cachesz = getpagesize() * 256; |
192 | #endif | |
193 | ||
194 | if (fstat(fd, &st) != 0) | |
195 | err(EXIT_FAILURE, _("stat failed %s"), filename); | |
196 | ||
197 | bufsz = st.st_blksize; | |
c12eff4c | 198 | |
d6cecc3f KZ |
199 | if (lseek(fd, off, SEEK_SET) < 0) |
200 | err(EXIT_FAILURE, _("seek on %s failed"), filename); | |
201 | ||
4b01c5a1 KZ |
202 | /* buffer + extra space for is_nul() sentinel */ |
203 | buf = xmalloc(bufsz + sizeof(uintptr_t)); | |
c12eff4c KZ |
204 | #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE) |
205 | posix_fadvise(fd, off, 0, POSIX_FADV_SEQUENTIAL); | |
d6cecc3f | 206 | #endif |
24b2a479 | 207 | |
d6cecc3f KZ |
208 | while (end == 0 || off < end) { |
209 | ssize_t rsz; | |
24b2a479 | 210 | |
d6cecc3f KZ |
211 | rsz = pread(fd, buf, bufsz, off); |
212 | if (rsz < 0 && errno) | |
213 | err(EXIT_FAILURE, _("%s: read failed"), filename); | |
c4172cc3 | 214 | if (end && rsz > 0 && off > end - rsz) |
d6cecc3f KZ |
215 | rsz = end - off; |
216 | if (rsz <= 0) | |
217 | break; | |
24b2a479 | 218 | |
c4172cc3 | 219 | if (is_nul(buf, rsz)) { |
db5f00bc | 220 | if (!hole_sz) { /* new hole detected */ |
c4172cc3 KZ |
221 | int rc = skip_hole(fd, &off); |
222 | if (rc == 0) | |
223 | continue; /* hole skipped */ | |
224 | else if (rc == 1) | |
225 | break; /* end of file */ | |
d6cecc3f | 226 | hole_start = off; |
db5f00bc | 227 | } |
d6cecc3f KZ |
228 | hole_sz += rsz; |
229 | } else if (hole_sz) { | |
230 | xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, | |
231 | hole_start, hole_sz); | |
232 | ct += hole_sz; | |
233 | hole_sz = hole_start = 0; | |
234 | } | |
c12eff4c KZ |
235 | |
236 | #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE) | |
237 | /* discard cached data */ | |
238 | if (off - cache_start > (off_t) cachesz) { | |
239 | size_t clen = off - cache_start; | |
240 | ||
241 | clen = (clen / cachesz) * cachesz; | |
242 | posix_fadvise(fd, cache_start, clen, POSIX_FADV_DONTNEED); | |
243 | cache_start = cache_start + clen; | |
244 | } | |
245 | #endif | |
d6cecc3f KZ |
246 | off += rsz; |
247 | } | |
782c290c | 248 | |
d6cecc3f | 249 | if (hole_sz) { |
24b2a479 | 250 | xfallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, |
d6cecc3f KZ |
251 | hole_start, hole_sz); |
252 | ct += hole_sz; | |
bcd9315d | 253 | } |
24b2a479 | 254 | |
24b2a479 | 255 | free(buf); |
d6cecc3f KZ |
256 | |
257 | if (verbose) { | |
258 | char *str = size_to_human_string(SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, ct); | |
259 | fprintf(stdout, _("%s: %s (%ju bytes) converted to sparse holes.\n"), | |
260 | filename, str, ct); | |
261 | free(str); | |
262 | } | |
bcd9315d RC |
263 | } |
264 | ||
d46a5499 KZ |
265 | int main(int argc, char **argv) |
266 | { | |
d46a5499 | 267 | int c; |
d46a5499 KZ |
268 | int fd; |
269 | int mode = 0; | |
d6cecc3f | 270 | int dig = 0; |
d46a5499 KZ |
271 | loff_t length = -2LL; |
272 | loff_t offset = 0; | |
273 | ||
6c7d5ae9 | 274 | static const struct option longopts[] = { |
83cc932d DP |
275 | { "help", 0, 0, 'h' }, |
276 | { "version", 0, 0, 'V' }, | |
277 | { "keep-size", 0, 0, 'n' }, | |
278 | { "punch-hole", 0, 0, 'p' }, | |
279 | { "collapse-range", 0, 0, 'c' }, | |
280 | { "dig-holes", 0, 0, 'd' }, | |
1fd4f609 | 281 | { "zero-range", 0, 0, 'z' }, |
83cc932d DP |
282 | { "offset", 1, 0, 'o' }, |
283 | { "length", 1, 0, 'l' }, | |
284 | { "verbose", 0, 0, 'v' }, | |
285 | { NULL, 0, 0, 0 } | |
d46a5499 KZ |
286 | }; |
287 | ||
b7f3f147 KZ |
288 | static const ul_excl_t excl[] = { /* rows and cols in in ASCII order */ |
289 | { 'c', 'd', 'p', 'z' }, | |
290 | { 'c', 'n' }, | |
291 | { 0 } | |
292 | }; | |
293 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
294 | ||
d46a5499 KZ |
295 | setlocale(LC_ALL, ""); |
296 | bindtextdomain(PACKAGE, LOCALEDIR); | |
297 | textdomain(PACKAGE); | |
efb8854f | 298 | atexit(close_stdout); |
d46a5499 | 299 | |
1fd4f609 | 300 | while ((c = getopt_long(argc, argv, "hvVncpdzl:o:", longopts, NULL)) |
83cc932d | 301 | != -1) { |
b7f3f147 KZ |
302 | |
303 | err_exclusive_options(c, longopts, excl, excl_st); | |
304 | ||
d46a5499 KZ |
305 | switch(c) { |
306 | case 'h': | |
307 | usage(stdout); | |
308 | break; | |
83cc932d DP |
309 | case 'c': |
310 | mode |= FALLOC_FL_COLLAPSE_RANGE; | |
311 | break; | |
24b2a479 | 312 | case 'd': |
d6cecc3f | 313 | dig = 1; |
24b2a479 | 314 | break; |
d46a5499 KZ |
315 | case 'l': |
316 | length = cvtnum(optarg); | |
317 | break; | |
b7f3f147 KZ |
318 | case 'n': |
319 | mode |= FALLOC_FL_KEEP_SIZE; | |
320 | break; | |
d46a5499 KZ |
321 | case 'o': |
322 | offset = cvtnum(optarg); | |
323 | break; | |
b7f3f147 KZ |
324 | case 'p': |
325 | mode |= FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; | |
326 | break; | |
327 | case 'z': | |
328 | mode |= FALLOC_FL_ZERO_RANGE; | |
329 | break; | |
782c290c KZ |
330 | case 'v': |
331 | verbose++; | |
332 | break; | |
b7f3f147 KZ |
333 | case 'V': |
334 | printf(UTIL_LINUX_VERSION); | |
335 | return EXIT_SUCCESS; | |
d46a5499 KZ |
336 | default: |
337 | usage(stderr); | |
338 | break; | |
339 | } | |
340 | } | |
38a5440c BV |
341 | |
342 | if (optind == argc) | |
343 | errx(EXIT_FAILURE, _("no filename specified.")); | |
344 | ||
345 | filename = argv[optind++]; | |
346 | ||
347 | if (optind != argc) | |
348 | errx(EXIT_FAILURE, _("unexpected number of arguments")); | |
349 | ||
d6cecc3f | 350 | if (dig) { |
dac1cb53 | 351 | /* for --dig-holes the default is analyze all file */ |
d6cecc3f KZ |
352 | if (length == -2LL) |
353 | length = 0; | |
354 | if (length < 0) | |
355 | errx(EXIT_FAILURE, _("invalid length value specified")); | |
356 | } else { | |
dac1cb53 | 357 | /* it's safer to require the range specification (--length --offset) */ |
d6cecc3f KZ |
358 | if (length == -2LL) |
359 | errx(EXIT_FAILURE, _("no length argument specified")); | |
360 | if (length <= 0) | |
361 | errx(EXIT_FAILURE, _("invalid length value specified")); | |
362 | } | |
d46a5499 KZ |
363 | if (offset < 0) |
364 | errx(EXIT_FAILURE, _("invalid offset value specified")); | |
fd1ee3b9 | 365 | |
575718a0 KZ |
366 | /* O_CREAT makes sense only for the default fallocate(2) behavior |
367 | * when mode is no specified and new space is allocated */ | |
368 | fd = open(filename, O_RDWR | (!dig && !mode ? O_CREAT : 0), 0644); | |
d46a5499 | 369 | if (fd < 0) |
782c290c | 370 | err(EXIT_FAILURE, _("cannot open %s"), filename); |
d46a5499 | 371 | |
d6cecc3f KZ |
372 | if (dig) |
373 | dig_holes(fd, offset, length); | |
374 | else | |
24b2a479 | 375 | xfallocate(fd, mode, offset, length); |
d46a5499 | 376 | |
5f52af50 | 377 | if (close_fd(fd) != 0) |
782c290c | 378 | err(EXIT_FAILURE, _("write failed: %s"), filename); |
24b2a479 | 379 | |
d46a5499 KZ |
380 | return EXIT_SUCCESS; |
381 | } |