uclibc-ng: bump version from 1.0.6 to 1.0.8
[openwrt.git] / toolchain / uClibc / patches-0.9.33.2 / 022-libc-add-posix_fallocate.patch
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()
4
5 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
6 ---
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
11
12 --- a/include/fcntl.h
13 +++ b/include/fcntl.h
14 @@ -210,9 +210,7 @@ extern int posix_fadvise64 (int __fd, __
15  
16  #endif
17  
18 -#if 0 /* && defined __UCLIBC_HAS_ADVANCED_REALTIME__ */
19 -
20 -/* FIXME -- uClibc should probably implement these... */
21 +#if defined __UCLIBC_HAS_ADVANCED_REALTIME__
22  
23  /* Reserve storage for the data of the file associated with FD.
24  
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
39 @@ -494,6 +494,14 @@
40  # define __ASSUME_PRIVATE_FUTEX        1
41  #endif
42  
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
49 +#endif
50 +
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
54 --- /dev/null
55 +++ b/libc/sysdeps/linux/common/posix_fallocate.c
56 @@ -0,0 +1,43 @@
57 +/* vi: set sw=4 ts=4: */
58 +/*
59 + * posix_fallocate() for uClibc
60 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
61 + *
62 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
63 + *
64 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
65 + */
66 +
67 +#include <sys/syscall.h>
68 +#include <fcntl.h>
69 +#include <bits/kernel-features.h>
70 +#include <stdint.h>
71 +
72 +#if defined __NR_fallocate
73 +int posix_fallocate(int fd, __off_t offset, __off_t len)
74 +{
75 +       int ret;
76 +
77 +# if __WORDSIZE == 32
78 +       uint32_t off_low = offset;
79 +       uint32_t len_low = len;
80 +       /* may assert that these >>31 are 0 */
81 +       uint32_t zero = 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));
89 +# else
90 +# error your machine is neither 32 bit or 64 bit ... it must be magical
91 +#endif
92 +    if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
93 +      return INTERNAL_SYSCALL_ERRNO (ret, err);
94 +    return 0;
95 +}
96 +# if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
97 +strong_alias(posix_fallocate,posix_fallocate64)
98 +# endif
99 +#endif
100 --- /dev/null
101 +++ b/libc/sysdeps/linux/common/posix_fallocate64.c
102 @@ -0,0 +1,39 @@
103 +/* vi: set sw=4 ts=4: */
104 +/*
105 + * posix_fallocate() for uClibc
106 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
107 + *
108 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
109 + *
110 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
111 + */
112 +
113 +#include <sys/syscall.h>
114 +#include <fcntl.h>
115 +#include <bits/kernel-features.h>
116 +#include <stdint.h>
117 +
118 +#if defined __NR_fallocate
119 +
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)
124 +{
125 +       int ret;
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);
136 +    return 0;
137 +}
138 +# else
139 +# error your machine is neither 32 bit or 64 bit ... it must be magical
140 +# endif
141 +#endif
142 --- a/test/.gitignore
143 +++ b/test/.gitignore
144 @@ -302,6 +302,8 @@ unistd/getcwd
145  unistd/getopt
146  unistd/getopt_long
147  unistd/tstgetopt
148 +unistd/tst-posix_fallocate
149 +unistd/tst-posix_fallocate64
150  unistd/tst-preadwrite
151  unistd/tst-preadwrite64
152  unistd/vfork
153 --- a/test/unistd/Makefile.in
154 +++ b/test/unistd/Makefile.in
155 @@ -2,7 +2,10 @@
156  # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
157  
158  ifeq ($(UCLIBC_HAS_LFS),)
159 -TESTS_DISABLED := tst-preadwrite64
160 +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
161 +endif
162 +ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
163 +TESTS_DISABLED := tst-posix_fallocate
164  endif
165  OPTS_getopt      := -abcXXX -9
166  OPTS_getopt_long := --add XXX --delete YYY --verbose
167 --- /dev/null
168 +++ b/test/unistd/tst-posix_fallocate.c
169 @@ -0,0 +1,127 @@
170 +#include <fcntl.h>
171 +#include <sys/stat.h>
172 +
173 +#ifndef TST_POSIX_FALLOCATE64
174 +# define stat64 stat
175 +# define fstat64 fstat
176 +# else
177 +# ifndef O_LARGEFILE
178 +#  error no O_LARGEFILE but you want to test with LFS enabled
179 +# endif
180 +#endif
181 +
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>
187 +
188 +static int fd;
189 +static void
190 +do_prepare (void)
191 +{
192 +  fd = create_temp_file ("tst-posix_fallocate.", NULL);
193 +  if (fd == -1)
194 +    {
195 +      printf ("cannot create temporary file: %m\n");
196 +      exit (1);
197 +    }
198 +}
199 +
200 +
201 +static int
202 +do_test (void)
203 +{
204 +  struct stat64 st;
205 +
206 +  if (fstat64 (fd, &st) != 0)
207 +    {
208 +      puts ("1st fstat failed");
209 +      return 1;
210 +    }
211 +
212 +  if (st.st_size != 0)
213 +    {
214 +      puts ("file not created with size 0");
215 +      return 1;
216 +    }
217 +
218 +  if (posix_fallocate (fd, 512, 768) != 0)
219 +    {
220 +      puts ("1st posix_fallocate call failed");
221 +      return 1;
222 +    }
223 +
224 +  if (fstat64 (fd, &st) != 0)
225 +    {
226 +      puts ("2nd fstat failed");
227 +      return 1;
228 +    }
229 +
230 +  if (st.st_size != 512 + 768)
231 +    {
232 +      printf ("file size after 1st posix_fallocate call is %llu, expected %u\n",
233 +             (unsigned long long int) st.st_size, 512u + 768u);
234 +      return 1;
235 +    }
236 +
237 +  if (posix_fallocate (fd, 0, 1024) != 0)
238 +    {
239 +      puts ("2nd posix_fallocate call failed");
240 +      return 1;
241 +    }
242 +
243 +  if (fstat64 (fd, &st) != 0)
244 +    {
245 +      puts ("3rd fstat failed");
246 +      return 1;
247 +    }
248 +
249 +  if (st.st_size != 512 + 768)
250 +    {
251 +      puts ("file size changed in 2nd posix_fallocate");
252 +      return 1;
253 +    }
254 +
255 +  if (posix_fallocate (fd, 2048, 64) != 0)
256 +    {
257 +      puts ("3rd posix_fallocate call failed");
258 +      return 1;
259 +    }
260 +
261 +  if (fstat64 (fd, &st) != 0)
262 +    {
263 +      puts ("4th fstat failed");
264 +      return 1;
265 +    }
266 +
267 +  if (st.st_size != 2048 + 64)
268 +    {
269 +      printf ("file size after 3rd posix_fallocate call is %llu, expected %u\n",
270 +             (unsigned long long int) st.st_size, 2048u + 64u);
271 +      return 1;
272 +    }
273 +#ifdef TST_POSIX_FALLOCATE64
274 +  if (posix_fallocate64 (fd, 4097ULL, 4294967295ULL + 2ULL) != 0)
275 +    {
276 +      puts ("4th posix_fallocate call failed");
277 +      return 1;
278 +    }
279 +
280 +  if (fstat64 (fd, &st) != 0)
281 +    {
282 +      puts ("5th fstat failed");
283 +      return 1;
284 +    }
285 +
286 +  if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
287 +    {
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);
290 +      return 1;
291 +    }
292 +#endif
293 +  close (fd);
294 +
295 +  return 0;
296 +}
297 --- /dev/null
298 +++ b/test/unistd/tst-posix_fallocate64.c
299 @@ -0,0 +1,2 @@
300 +#define TST_POSIX_FALLOCATE64
301 +#include "tst-posix_fallocate.c"