1 From 8fc83b7f3fd7425aa4e96c870a7d46df1d81c16c Mon Sep 17 00:00:00 2001
2 From: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
3 Date: Tue, 17 Apr 2012 09:30:15 +0200
4 Subject: [PATCH] libc: add posix_fallocate()
6 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
9 libc/sysdeps/linux/common/Makefile.in | 3 +-
10 libc/sysdeps/linux/common/bits/kernel-features.h | 8 ++
11 libc/sysdeps/linux/common/posix_fallocate.c | 43 ++++++++
12 libc/sysdeps/linux/common/posix_fallocate64.c | 39 +++++++
14 test/unistd/Makefile.in | 5 +-
15 test/unistd/tst-posix_fallocate.c | 127 ++++++++++++++++++++++
16 test/unistd/tst-posix_fallocate64.c | 2 +
17 9 files changed, 228 insertions(+), 5 deletions(-)
18 create mode 100644 libc/sysdeps/linux/common/posix_fallocate.c
19 create mode 100644 libc/sysdeps/linux/common/posix_fallocate64.c
20 create mode 100644 test/unistd/tst-posix_fallocate.c
21 create mode 100644 test/unistd/tst-posix_fallocate64.c
23 diff --git a/include/fcntl.h b/include/fcntl.h
24 index 26ad1fe..c4a47af 100644
27 @@ -210,9 +210,7 @@ extern int posix_fadvise64 (int __fd, __off64_t __offset, __off64_t __len,
31 -#if 0 /* && defined __UCLIBC_HAS_ADVANCED_REALTIME__ */
33 -/* FIXME -- uClibc should probably implement these... */
34 +#if defined __UCLIBC_HAS_ADVANCED_REALTIME__
36 /* Reserve storage for the data of the file associated with FD.
38 diff --git a/libc/sysdeps/linux/common/Makefile.in b/libc/sysdeps/linux/common/Makefile.in
39 index e9baa47..e4ac4ff 100644
40 --- a/libc/sysdeps/linux/common/Makefile.in
41 +++ b/libc/sysdeps/linux/common/Makefile.in
42 @@ -82,7 +82,8 @@ CSRC-$(UCLIBC_HAS_REALTIME) += clock_getres.c clock_gettime.c clock_settime.c \
43 sched_get_priority_max.c sched_get_priority_min.c sched_getscheduler.c \
44 sched_rr_get_interval.c sched_setparam.c sched_setscheduler.c sigqueue.c
45 # 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
46 -CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_fadvise64.c posix_fadvise.c posix_madvise.c
47 +CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_fadvise64.c posix_fadvise.c posix_madvise.c \
48 + posix_fallocate.c posix_fallocate64.c
49 CSRC-$(UCLIBC_SUSV4_LEGACY) += utime.c
50 CSRC-$(UCLIBC_HAS_EPOLL) += epoll.c
51 CSRC-$(UCLIBC_HAS_XATTR) += xattr.c
52 diff --git a/libc/sysdeps/linux/common/bits/kernel-features.h b/libc/sysdeps/linux/common/bits/kernel-features.h
53 index 5665e24..13c7a63 100644
54 --- a/libc/sysdeps/linux/common/bits/kernel-features.h
55 +++ b/libc/sysdeps/linux/common/bits/kernel-features.h
57 # define __ASSUME_PRIVATE_FUTEX 1
60 +/* Support for fallocate was added in 2.6.23,
61 + on s390 only after 2.6.23-rc1, on alpha only after 2.6.33-rc1. */
62 +#if __LINUX_KERNEL_VERSION >= 0x020617 \
63 + && (!defined __s390__ || __LINUX_KERNEL_VERSION >= 0x020618) \
64 + && (!defined __alpha__ || __LINUX_KERNEL_VERSION >= 0x020621)
65 +# define __ASSUME_FALLOCATE 1
68 /* getcpu is a syscall for x86-64 since 3.1. */
69 #if defined __x86_64__ && __LINUX_KERNEL_VERSION >= 0x030100
70 # define __ASSUME_GETCPU_SYSCALL 1
71 diff --git a/libc/sysdeps/linux/common/posix_fallocate.c b/libc/sysdeps/linux/common/posix_fallocate.c
73 index 0000000..9aaa6ce
75 +++ b/libc/sysdeps/linux/common/posix_fallocate.c
77 +/* vi: set sw=4 ts=4: */
79 + * posix_fallocate() for uClibc
80 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
82 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
84 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
87 +#include <sys/syscall.h>
89 +#include <bits/kernel-features.h>
92 +#if defined __NR_fallocate
93 +int posix_fallocate(int fd, __off_t offset, __off_t len)
97 +# if __WORDSIZE == 32
98 + uint32_t off_low = offset;
99 + uint32_t len_low = len;
100 + /* may assert that these >>31 are 0 */
102 + INTERNAL_SYSCALL_DECL(err);
103 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
104 + __LONG_LONG_PAIR (zero, off_low),
105 + __LONG_LONG_PAIR (zero, len_low)));
106 +# elif __WORDSIZE == 64
107 + INTERNAL_SYSCALL_DECL(err);
108 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, 0, offset, len));
110 +# error your machine is neither 32 bit or 64 bit ... it must be magical
112 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
113 + return INTERNAL_SYSCALL_ERRNO (ret, err);
116 +# if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
117 +strong_alias(posix_fallocate,posix_fallocate64)
120 diff --git a/libc/sysdeps/linux/common/posix_fallocate64.c b/libc/sysdeps/linux/common/posix_fallocate64.c
122 index 0000000..818d868
124 +++ b/libc/sysdeps/linux/common/posix_fallocate64.c
126 +/* vi: set sw=4 ts=4: */
128 + * posix_fallocate() for uClibc
129 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
131 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
133 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
136 +#include <sys/syscall.h>
138 +#include <bits/kernel-features.h>
141 +#if defined __NR_fallocate
143 +# if __WORDSIZE == 64
144 +/* Can use normal posix_fallocate() */
145 +# elif __WORDSIZE == 32
146 +int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
149 + uint32_t off_low = offset & 0xffffffff;
150 + uint32_t off_high = offset >> 32;
151 + uint32_t len_low = len & 0xffffffff;
152 + uint32_t len_high = len >> 32;
153 + INTERNAL_SYSCALL_DECL(err);
154 + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
155 + __LONG_LONG_PAIR (off_high, off_low),
156 + __LONG_LONG_PAIR (len_high, len_low)));
157 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
158 + return INTERNAL_SYSCALL_ERRNO (ret, err);
162 +# error your machine is neither 32 bit or 64 bit ... it must be magical
165 diff --git a/test/.gitignore b/test/.gitignore
166 index 7234c48..ef152e9 100644
167 --- a/test/.gitignore
168 +++ b/test/.gitignore
169 @@ -305,6 +305,8 @@ unistd/getcwd
173 +unistd/tst-posix_fallocate
174 +unistd/tst-posix_fallocate64
175 unistd/tst-preadwrite
176 unistd/tst-preadwrite64
178 diff --git a/test/unistd/Makefile.in b/test/unistd/Makefile.in
179 index c542f98..24b9a37 100644
180 --- a/test/unistd/Makefile.in
181 +++ b/test/unistd/Makefile.in
183 # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
185 ifeq ($(UCLIBC_HAS_LFS),)
186 -TESTS_DISABLED := tst-preadwrite64
187 +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
189 +ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
190 +TESTS_DISABLED := tst-posix_fallocate
192 OPTS_getopt := -abcXXX -9
193 OPTS_getopt_long := --add XXX --delete YYY --verbose
194 diff --git a/test/unistd/tst-posix_fallocate.c b/test/unistd/tst-posix_fallocate.c
196 index 0000000..d41c604
198 +++ b/test/unistd/tst-posix_fallocate.c
201 +#include <sys/stat.h>
203 +#ifndef TST_POSIX_FALLOCATE64
204 +# define stat64 stat
205 +# define fstat64 fstat
207 +# ifndef O_LARGEFILE
208 +# error no O_LARGEFILE but you want to test with LFS enabled
212 +static void do_prepare (void);
213 +#define PREPARE(argc, argv) do_prepare ()
214 +static int do_test (void);
215 +#define TEST_FUNCTION do_test ()
216 +#include <test-skeleton.c>
222 + fd = create_temp_file ("tst-posix_fallocate.", NULL);
225 + printf ("cannot create temporary file: %m\n");
236 + if (fstat64 (fd, &st) != 0)
238 + puts ("1st fstat failed");
242 + if (st.st_size != 0)
244 + puts ("file not created with size 0");
248 + if (posix_fallocate (fd, 512, 768) != 0)
250 + puts ("1st posix_fallocate call failed");
254 + if (fstat64 (fd, &st) != 0)
256 + puts ("2nd fstat failed");
260 + if (st.st_size != 512 + 768)
262 + printf ("file size after 1st posix_fallocate call is %llu, expected %u\n",
263 + (unsigned long long int) st.st_size, 512u + 768u);
267 + if (posix_fallocate (fd, 0, 1024) != 0)
269 + puts ("2nd posix_fallocate call failed");
273 + if (fstat64 (fd, &st) != 0)
275 + puts ("3rd fstat failed");
279 + if (st.st_size != 512 + 768)
281 + puts ("file size changed in 2nd posix_fallocate");
285 + if (posix_fallocate (fd, 2048, 64) != 0)
287 + puts ("3rd posix_fallocate call failed");
291 + if (fstat64 (fd, &st) != 0)
293 + puts ("4th fstat failed");
297 + if (st.st_size != 2048 + 64)
299 + printf ("file size after 3rd posix_fallocate call is %llu, expected %u\n",
300 + (unsigned long long int) st.st_size, 2048u + 64u);
303 +#ifdef TST_POSIX_FALLOCATE64
304 + if (posix_fallocate64 (fd, 4097ULL, 4294967295ULL + 2ULL) != 0)
306 + puts ("4th posix_fallocate call failed");
310 + if (fstat64 (fd, &st) != 0)
312 + puts ("5th fstat failed");
316 + if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
318 + printf ("file size after 4th posix_fallocate call is %llu, expected %llu\n",
319 + (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
327 diff --git a/test/unistd/tst-posix_fallocate64.c b/test/unistd/tst-posix_fallocate64.c
329 index 0000000..b1ee0ff
331 +++ b/test/unistd/tst-posix_fallocate64.c
333 +#define TST_POSIX_FALLOCATE64
334 +#include "tst-posix_fallocate.c"