1 From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
2 Date: Tue, 17 Apr 2012 09:30:15 +0200
3 Subject: [PATCH] libc: add posix_fallocate()
5 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
7 create mode 100644 libc/sysdeps/linux/common/posix_fallocate.c
8 create mode 100644 libc/sysdeps/linux/common/posix_fallocate64.c
9 create mode 100644 test/unistd/tst-posix_fallocate.c
10 create mode 100644 test/unistd/tst-posix_fallocate64.c
14 @@ -210,9 +210,7 @@ extern int posix_fadvise64 (int __fd, __
18 -#if 0 /* && defined __UCLIBC_HAS_ADVANCED_REALTIME__ */
20 -/* FIXME -- uClibc should probably implement these... */
21 +#if defined __UCLIBC_HAS_ADVANCED_REALTIME__
23 /* Reserve storage for the data of the file associated with FD.
25 --- a/libc/sysdeps/linux/common/Makefile.in
26 +++ b/libc/sysdeps/linux/common/Makefile.in
27 @@ -81,7 +81,8 @@ CSRC-$(UCLIBC_HAS_REALTIME) += clock_get
28 sched_get_priority_max.c sched_get_priority_min.c sched_getscheduler.c \
29 sched_rr_get_interval.c sched_setparam.c sched_setscheduler.c sigqueue.c
30 # clock_getcpuclockid|clock_nanosleep|mq_timedreceive|mq_timedsend|posix_fadvise|posix_fallocate|posix_madvise|posix_memalign|posix_mem_offset|posix_spawnattr_destroy|posix_spawnattr_init|posix_spawnattr_getflags|posix_spawnattr_setflags|posix_spawnattr_getpgroup|posix_spawnattr_setpgroup|posix_spawnattr_getschedparam|posix_spawnattr_setschedparam|posix_spawnattr_getschedpolicy|posix_spawnattr_setschedpolicy|posix_spawnattr_getsigdefault|posix_spawnattr_setsigdefault|posix_spawnattr_getsigmask|posix_spawnattr_setsigmask|posix_spawnattr_init|posix_spawnattr_setflags|posix_spawnattr_setpgroup|posix_spawnattr_setschedparam|posix_spawnattr_setschedpolicy|posix_spawnattr_setsigdefault|posix_spawnattr_setsigmask|posix_spawn_file_actions_addclose|posix_spawn_file_actions_addopen|posix_spawn_file_actions_adddup2|posix_spawn_file_actions_addopen|posix_spawn_file_actions_destroy|posix_spawn_file_actions_init|posix_spawn_file_actions_init|posix_spawn|posix_spawnp|posix_spawnp|posix_typed_mem_get_info|pthread_mutex_timedlock|sem_timedwait
31 -CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_fadvise64.c posix_fadvise.c posix_madvise.c
32 +CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_fadvise64.c posix_fadvise.c posix_madvise.c \
33 + posix_fallocate.c posix_fallocate64.c
34 CSRC-$(UCLIBC_SUSV4_LEGACY) += utime.c
35 CSRC-$(UCLIBC_HAS_EPOLL) += epoll.c
36 CSRC-$(UCLIBC_HAS_XATTR) += xattr.c
37 --- a/libc/sysdeps/linux/common/bits/kernel-features.h
38 +++ b/libc/sysdeps/linux/common/bits/kernel-features.h
40 # define __ASSUME_PRIVATE_FUTEX 1
43 +/* Support for fallocate was added in 2.6.23,
44 + on s390 only after 2.6.23-rc1, on alpha only after 2.6.33-rc1. */
45 +#if __LINUX_KERNEL_VERSION >= 0x020617 \
46 + && (!defined __s390__ || __LINUX_KERNEL_VERSION >= 0x020618) \
47 + && (!defined __alpha__ || __LINUX_KERNEL_VERSION >= 0x020621)
48 +# define __ASSUME_FALLOCATE 1
51 /* getcpu is a syscall for x86-64 since 3.1. */
52 #if defined __x86_64__ && __LINUX_KERNEL_VERSION >= 0x030100
53 # define __ASSUME_GETCPU_SYSCALL 1
55 +++ b/libc/sysdeps/linux/common/posix_fallocate.c
57 +/* vi: set sw=4 ts=4: */
59 + * posix_fallocate() for uClibc
60 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
62 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
64 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
67 +#include <sys/syscall.h>
69 +#include <bits/kernel-features.h>
72 +#if defined __NR_fallocate
73 +int posix_fallocate(int fd, __off_t offset, __off_t len)
77 +# if __WORDSIZE == 32
78 + uint32_t off_low = offset;
79 + uint32_t len_low = len;
80 + /* may assert that these >>31 are 0 */
82 + INTERNAL_SYSCALL_DECL(err);
83 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
84 + __LONG_LONG_PAIR (zero, off_low),
85 + __LONG_LONG_PAIR (zero, len_low)));
86 +# elif __WORDSIZE == 64
87 + INTERNAL_SYSCALL_DECL(err);
88 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, 0, offset, len));
90 +# error your machine is neither 32 bit or 64 bit ... it must be magical
92 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
93 + return INTERNAL_SYSCALL_ERRNO (ret, err);
96 +# if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
97 +strong_alias(posix_fallocate,posix_fallocate64)
101 +++ b/libc/sysdeps/linux/common/posix_fallocate64.c
103 +/* vi: set sw=4 ts=4: */
105 + * posix_fallocate() for uClibc
106 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
108 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
110 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
113 +#include <sys/syscall.h>
115 +#include <bits/kernel-features.h>
118 +#if defined __NR_fallocate
120 +# if __WORDSIZE == 64
121 +/* Can use normal posix_fallocate() */
122 +# elif __WORDSIZE == 32
123 +int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
126 + uint32_t off_low = offset & 0xffffffff;
127 + uint32_t off_high = offset >> 32;
128 + uint32_t len_low = len & 0xffffffff;
129 + uint32_t len_high = len >> 32;
130 + INTERNAL_SYSCALL_DECL(err);
131 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
132 + __LONG_LONG_PAIR (off_high, off_low),
133 + __LONG_LONG_PAIR (len_high, len_low)));
134 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
135 + return INTERNAL_SYSCALL_ERRNO (ret, err);
139 +# error your machine is neither 32 bit or 64 bit ... it must be magical
142 --- a/test/.gitignore
143 +++ b/test/.gitignore
144 @@ -302,6 +302,8 @@ unistd/getcwd
148 +unistd/tst-posix_fallocate
149 +unistd/tst-posix_fallocate64
150 unistd/tst-preadwrite
151 unistd/tst-preadwrite64
153 --- a/test/unistd/Makefile.in
154 +++ b/test/unistd/Makefile.in
156 # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
158 ifeq ($(UCLIBC_HAS_LFS),)
159 -TESTS_DISABLED := tst-preadwrite64
160 +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
162 +ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
163 +TESTS_DISABLED := tst-posix_fallocate
165 OPTS_getopt := -abcXXX -9
166 OPTS_getopt_long := --add XXX --delete YYY --verbose
168 +++ b/test/unistd/tst-posix_fallocate.c
171 +#include <sys/stat.h>
173 +#ifndef TST_POSIX_FALLOCATE64
174 +# define stat64 stat
175 +# define fstat64 fstat
177 +# ifndef O_LARGEFILE
178 +# error no O_LARGEFILE but you want to test with LFS enabled
182 +static void do_prepare (void);
183 +#define PREPARE(argc, argv) do_prepare ()
184 +static int do_test (void);
185 +#define TEST_FUNCTION do_test ()
186 +#include <test-skeleton.c>
192 + fd = create_temp_file ("tst-posix_fallocate.", NULL);
195 + printf ("cannot create temporary file: %m\n");
206 + if (fstat64 (fd, &st) != 0)
208 + puts ("1st fstat failed");
212 + if (st.st_size != 0)
214 + puts ("file not created with size 0");
218 + if (posix_fallocate (fd, 512, 768) != 0)
220 + puts ("1st posix_fallocate call failed");
224 + if (fstat64 (fd, &st) != 0)
226 + puts ("2nd fstat failed");
230 + if (st.st_size != 512 + 768)
232 + printf ("file size after 1st posix_fallocate call is %llu, expected %u\n",
233 + (unsigned long long int) st.st_size, 512u + 768u);
237 + if (posix_fallocate (fd, 0, 1024) != 0)
239 + puts ("2nd posix_fallocate call failed");
243 + if (fstat64 (fd, &st) != 0)
245 + puts ("3rd fstat failed");
249 + if (st.st_size != 512 + 768)
251 + puts ("file size changed in 2nd posix_fallocate");
255 + if (posix_fallocate (fd, 2048, 64) != 0)
257 + puts ("3rd posix_fallocate call failed");
261 + if (fstat64 (fd, &st) != 0)
263 + puts ("4th fstat failed");
267 + if (st.st_size != 2048 + 64)
269 + printf ("file size after 3rd posix_fallocate call is %llu, expected %u\n",
270 + (unsigned long long int) st.st_size, 2048u + 64u);
273 +#ifdef TST_POSIX_FALLOCATE64
274 + if (posix_fallocate64 (fd, 4097ULL, 4294967295ULL + 2ULL) != 0)
276 + puts ("4th posix_fallocate call failed");
280 + if (fstat64 (fd, &st) != 0)
282 + puts ("5th fstat failed");
286 + if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
288 + printf ("file size after 4th posix_fallocate call is %llu, expected %llu\n",
289 + (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
298 +++ b/test/unistd/tst-posix_fallocate64.c
300 +#define TST_POSIX_FALLOCATE64
301 +#include "tst-posix_fallocate.c"