1 From: "Anthony G. Basile" <blueness@gentoo.org>
2 Date: Sun, 7 Sep 2014 15:33:46 -0400
3 Subject: [PATCH] libc: add fallocate() and fallocate64()
5 We add the Linux-specific function fallocate() which allows the user to
6 directly manipulate allocate space for a file. fallocate() can operate
7 in different modes, but the default mode is equivalent to posix_fallocate()
8 which is specified in POSIX.1.
10 Recent releases of e2fsprogs 1.42.11 and above expect fallocate64() to be
13 Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
14 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
16 create mode 100644 libc/sysdeps/linux/common/fallocate.c
17 create mode 100644 libc/sysdeps/linux/common/fallocate64.c
18 create mode 100644 test/unistd/tst-fallocate.c
19 create mode 100644 test/unistd/tst-fallocate64.c
21 --- a/extra/Configs/Config.in
22 +++ b/extra/Configs/Config.in
23 @@ -952,8 +952,8 @@ config UCLIBC_LINUX_SPECIFIC
27 - capget(), capset(), eventfd(), fstatfs(),
28 - inotify_*(), ioperm(), iopl(),
29 + capget(), capset(), eventfd(), fallocate(),
30 + fstatfs(), inotify_*(), ioperm(), iopl(),
31 madvise(), modify_ldt(), pipe2(), personality(),
32 prctl()/arch_prctl(), pivot_root(), modify_ldt(),
33 ppoll(), readahead(), reboot(), remap_file_pages(),
36 @@ -237,6 +237,38 @@ extern int __fcntl_nocancel (int fd, int
37 libc_hidden_proto(__fcntl_nocancel)
40 +#if (defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU) || defined _LIBC
41 +/* Reserve storage for the data of a file associated with FD. This function
42 + is Linux-specific. For the portable version, use posix_fallocate().
43 + Unlike the latter, fallocate can operate in different modes. The default
44 + mode = 0 is equivalent to posix_fallocate().
46 + Note: These declarations are used in posix_fallocate.c and
47 + posix_fallocate64.c, so we expose them internally.
50 +/* Flags for fallocate's mode. */
51 +# define FALLOC_FL_KEEP_SIZE 1 /* Don't extend size of file
52 + even if offset + len is
53 + greater than file size. */
54 +# define FALLOC_FL_PUNCH_HOLE 2 /* Create a hole in the file. */
56 +# ifndef __USE_FILE_OFFSET64
57 +extern int fallocate (int __fd, int __mode, __off_t __offset, __off_t __len);
60 +extern int __REDIRECT (fallocate, (int __fd, int __mode, __off64_t __offset,
64 +# define fallocate fallocate64
67 +# ifdef __USE_LARGEFILE64
68 +extern int fallocate64 (int __fd, int __mode, __off64_t __offset, __off64_t __len);
75 --- a/libc/sysdeps/linux/common/Makefile.in
76 +++ b/libc/sysdeps/linux/common/Makefile.in
77 @@ -61,6 +61,10 @@ CSRC-$(UCLIBC_LINUX_SPECIFIC) += \
79 CSRC-$(if $(findstring yy,$(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_LFS)),y) += \
81 +# posix_fallocate() needs __libc_fallocate() from fallocate.c
82 +# posix_fallocate64() needs __libc_fallocate64() from fallocate64.c
83 +CSRC-$(if $(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_ADVANCED_REALTIME),y,) += \
84 + fallocate.c $(filter fallocate64.c,$(CSRC-y))
85 # NPTL needs these internally: madvise.c
86 CSRC-$(findstring y,$(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_THREADS_NATIVE)) += madvise.c
87 ifeq ($(UCLIBC_HAS_THREADS_NATIVE),y)
89 +++ b/libc/sysdeps/linux/common/fallocate.c
91 +/* vi: set sw=4 ts=4: */
93 + * fallocate() for uClibc - Based off of posix_fallocate() by Erik Andersen
94 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
96 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
98 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
101 +#include <sys/syscall.h>
103 +#include <bits/kernel-features.h>
106 +#if defined __NR_fallocate
107 +extern __typeof(fallocate) __libc_fallocate attribute_hidden;
108 +int attribute_hidden __libc_fallocate(int fd, int mode, __off_t offset, __off_t len)
112 +# if __WORDSIZE == 32
113 + uint32_t off_low = offset;
114 + uint32_t len_low = len;
115 + /* may assert that these >>31 are 0 */
117 + INTERNAL_SYSCALL_DECL(err);
118 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, mode,
119 + __LONG_LONG_PAIR (zero, off_low),
120 + __LONG_LONG_PAIR (zero, len_low)));
121 +# elif __WORDSIZE == 64
122 + INTERNAL_SYSCALL_DECL(err);
123 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, mode, offset, len));
125 +# error your machine is neither 32 bit or 64 bit ... it must be magical
127 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
128 + return INTERNAL_SYSCALL_ERRNO (ret, err);
132 +# if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU
133 +strong_alias(__libc_fallocate,fallocate)
134 +# if __WORDSIZE == 64
135 +strong_alias(__libc_fallocate,fallocate64)
140 +++ b/libc/sysdeps/linux/common/fallocate64.c
142 +/* vi: set sw=4 ts=4: */
144 + * fallocate() for uClibc - based off posix_fallocate() by Erik Andersen
145 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
147 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
149 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
152 +#include <sys/syscall.h>
155 +#include <bits/kernel-features.h>
158 +#if defined __NR_fallocate
160 +# if __WORDSIZE == 64
161 +/* Can use normal fallocate() */
162 +# elif __WORDSIZE == 32
163 +extern __typeof(fallocate64) __libc_fallocate64 attribute_hidden;
164 +int attribute_hidden __libc_fallocate64(int fd, int mode, __off64_t offset,
168 + INTERNAL_SYSCALL_DECL(err);
169 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, mode,
170 + OFF64_HI_LO (offset), OFF64_HI_LO (len)));
171 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
172 + return INTERNAL_SYSCALL_ERRNO (ret, err);
176 +# if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU
177 +strong_alias(__libc_fallocate64,fallocate64)
181 +# error your machine is neither 32 bit or 64 bit ... it must be magical
184 --- a/libc/sysdeps/linux/common/posix_fallocate.c
185 +++ b/libc/sysdeps/linux/common/posix_fallocate.c
189 #if defined __NR_fallocate
190 +extern __typeof(fallocate) __libc_fallocate attribute_hidden;
191 int posix_fallocate(int fd, __off_t offset, __off_t len)
195 -# if __WORDSIZE == 32
196 - uint32_t off_low = offset;
197 - uint32_t len_low = len;
198 - /* may assert that these >>31 are 0 */
200 - INTERNAL_SYSCALL_DECL(err);
201 - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
202 - __LONG_LONG_PAIR (zero, off_low),
203 - __LONG_LONG_PAIR (zero, len_low)));
204 -# elif __WORDSIZE == 64
205 - INTERNAL_SYSCALL_DECL(err);
206 - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, 0, offset, len));
208 -# error your machine is neither 32 bit or 64 bit ... it must be magical
210 - if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
211 - return INTERNAL_SYSCALL_ERRNO (ret, err);
213 + return __libc_fallocate(fd, 0, offset, len);
215 # if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
216 strong_alias(posix_fallocate,posix_fallocate64)
217 --- a/libc/sysdeps/linux/common/posix_fallocate64.c
218 +++ b/libc/sysdeps/linux/common/posix_fallocate64.c
220 # if __WORDSIZE == 64
221 /* Can use normal posix_fallocate() */
222 # elif __WORDSIZE == 32
223 +extern __typeof(fallocate64) __libc_fallocate64 attribute_hidden;
224 int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
227 - uint32_t off_low = offset & 0xffffffff;
228 - uint32_t off_high = offset >> 32;
229 - uint32_t len_low = len & 0xffffffff;
230 - uint32_t len_high = len >> 32;
231 - INTERNAL_SYSCALL_DECL(err);
232 - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
233 - __LONG_LONG_PAIR (off_high, off_low),
234 - __LONG_LONG_PAIR (len_high, len_low)));
235 - if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
236 - return INTERNAL_SYSCALL_ERRNO (ret, err);
238 + return __libc_fallocate64(fd, 0, offset, len);
241 -# error your machine is neither 32 bit or 64 bit ... it must be magical
242 +# error your machine is neither 32 bit or 64 bit ... it must be magical
245 --- a/test/.gitignore
246 +++ b/test/.gitignore
247 @@ -302,6 +302,8 @@ unistd/getcwd
251 +unistd/tst-fallocate
252 +unistd/tst-fallocate64
253 unistd/tst-posix_fallocate
254 unistd/tst-posix_fallocate64
255 unistd/tst-preadwrite
257 +++ b/test/unistd/tst-fallocate.c
260 +#include <sys/stat.h>
262 +#ifndef TST_FALLOCATE64
263 +# define stat64 stat
264 +# define fstat64 fstat
266 +# ifndef O_LARGEFILE
267 +# error no O_LARGEFILE but you want to test with LFS enabled
271 +static void do_prepare(void);
272 +static int do_test(void);
273 +#define PREPARE(argc, argv) do_prepare ()
274 +#define TEST_FUNCTION do_test ()
275 +#include <test-skeleton.c>
281 + fd = create_temp_file ("tst-fallocate.", NULL);
284 + printf ("cannot create temporary file: %m\n");
295 + char garbage[4096];
298 + if (fstat64 (fd, &st) != 0)
300 + puts ("1st fstat failed");
304 + if (st.st_size != 0)
306 + puts ("file not created with size 0");
310 + /* This is the default mode which is identical to posix_fallocate().
311 + Note: we need a few extra blocks for FALLOC_FL_PUNCH_HOLE below.
312 + While block sizes vary, we'll assume eight 4K blocks for good measure. */
313 + if (fallocate (fd, 0, 8 * 4096, 128) != 0)
315 + puts ("1st fallocate call failed");
319 + if (fstat64 (fd, &st) != 0)
321 + puts ("2nd fstat failed");
325 + if (st.st_size != 8 * 4096 + 128)
327 + printf ("file size after 1st fallocate call is %llu, expected %u\n",
328 + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
332 + /* Without FALLOC_FL_KEEP_SIZE, this would increaste the size of the file. */
333 + if (fallocate (fd, FALLOC_FL_KEEP_SIZE, 0, 16 * 4096) != 0)
335 + puts ("2nd fallocate call failed");
339 + if (fstat64 (fd, &st) != 0)
341 + puts ("3rd fstat failed");
345 + if (st.st_size != 8 * 4096 + 128)
347 + printf ("file size changed in 2nd fallocate call to %llu, expected %u\n",
348 + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
352 + /* Let's fill up the first eight 4k blocks with 'x' to force some allocations. */
354 + memset(garbage, 'x', 4096);
355 + for(c=0; c < 8; c++)
356 + if(write(fd, garbage, 4096) == -1)
358 + puts ("write failed");
362 + if (fstat64 (fd, &st) != 0)
364 + puts ("4th fstat failed");
368 + blksb4 = st.st_blocks;
370 + /* Let's punch a hole in the entire file, turning it effectively into a sparse file. */
371 + if (fallocate (fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 8 * 4096 + 128) != 0)
373 + puts ("3rd fallocate call failed");
377 + if (fstat64 (fd, &st) != 0)
379 + puts ("5th fstat failed");
383 + if (st.st_size != 8 * 4096 + 128)
385 + printf ("file size after 3rd fallocate call is %llu, expected %u\n",
386 + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
390 + /* The number of allocated blocks should decrease. I hope this works on
391 + all filesystems! */
392 + if (st.st_blocks >= blksb4)
394 + printf ("number of blocks after 3rd fallocate call is %lu, expected less than %lu\n",
395 + (unsigned long int) st.st_blocks, blksb4);
399 +#ifdef TST_FALLOCATE64
400 + /* We'll just do a mode = 0 test for fallocate64() */
401 + if (fallocate64 (fd, 0, 4097ULL, 4294967295ULL + 2ULL) != 0)
403 + puts ("1st fallocate64 call failed");
407 + if (fstat64 (fd, &st) != 0)
409 + puts ("6th fstat failed");
413 + if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
415 + printf ("file size after 1st fallocate64 call is %llu, expected %llu\n",
416 + (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
426 +++ b/test/unistd/tst-fallocate64.c
428 +#define TST_FALLOCATE64
429 +#include "tst-fallocate.c"
430 --- a/test/unistd/Makefile.in
431 +++ b/test/unistd/Makefile.in
433 # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
435 ifeq ($(UCLIBC_HAS_LFS),)
436 -TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
437 +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64 tst-fallocate64
439 ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
440 -TESTS_DISABLED := tst-posix_fallocate
441 +TESTS_DISABLED := tst-posix_fallocate tst-fallocate64
443 +ifeq ($(UCLIBC_LINUX_SPECIFIC),)
444 +TESTS_DISABLED += tst-fallocate
446 OPTS_getopt := -abcXXX -9
447 OPTS_getopt_long := --add XXX --delete YYY --verbose