2 * fallocate - utility to use the fallocate system call
4 * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
5 * Written by Eric Sandeen <sandeen@redhat.com>
7 * cvtnum routine taken from xfsprogs,
8 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it would be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define _LARGEFILE_SOURCE
25 #define _LARGEFILE64_SOURCE
28 #include <sys/syscall.h>
29 #include <sys/types.h>
36 // #include <linux/falloc.h>
37 #define FALLOC_FL_KEEP_SIZE 0x01
41 printf("Usage: fallocate [-nt] [-o offset] -l length filename\n");
45 #define EXABYTES(x) ((long long)(x) << 60)
46 #define PETABYTES(x) ((long long)(x) << 50)
47 #define TERABYTES(x) ((long long)(x) << 40)
48 #define GIGABYTES(x) ((long long)(x) << 30)
49 #define MEGABYTES(x) ((long long)(x) << 20)
50 #define KILOBYTES(x) ((long long)(x) << 10)
59 i = strtoll(s, &sp, 0);
60 if (i == 0 && sp == s)
86 int main(int argc, char **argv)
97 while ((opt = getopt(argc, argv, "nl:ot")) != -1) {
100 /* do not change filesize */
101 falloc_mode = FALLOC_FL_KEEP_SIZE;
104 length = cvtnum(optarg);
107 offset = cvtnum(optarg);
117 if (length == -2LL) {
118 printf("Error: no length argument specified\n");
123 printf("Error: invalid length value specified\n");
128 printf("Error: invalid offset value specified\n");
132 if (tflag && (falloc_mode & FALLOC_FL_KEEP_SIZE)) {
133 printf("-n and -t options incompatible\n");
137 if (tflag && offset) {
138 printf("-n and -o options incompatible\n");
142 if (optind == argc) {
143 printf("Error: no filename specified\n");
147 fname = argv[optind++];
149 /* Should we create the file if it doesn't already exist? */
150 fd = open(fname, O_WRONLY|O_LARGEFILE);
152 perror("Error opening file");
157 error = ftruncate(fd, length);
159 error = syscall(SYS_fallocate, fd, falloc_mode, offset, length);
162 perror("fallocate failed");