uclibc-ng: bump version from 1.0.6 to 1.0.8
[openwrt/openwrt.git] / toolchain / uClibc / patches-0.9.33.2 / 023-libc-add-fallocate-and-fallocate64.patch
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()
4
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.
9
10 Recent releases of e2fsprogs 1.42.11 and above expect fallocate64() to be
11 available.
12
13 Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
14 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
15 ---
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
20
21 --- a/extra/Configs/Config.in
22 +++ b/extra/Configs/Config.in
23 @@ -952,8 +952,8 @@ config UCLIBC_LINUX_SPECIFIC
24 default y
25 help
26 accept4(), bdflush(),
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(),
34 --- a/include/fcntl.h
35 +++ b/include/fcntl.h
36 @@ -237,6 +237,38 @@ extern int __fcntl_nocancel (int fd, int
37 libc_hidden_proto(__fcntl_nocancel)
38 #endif
39
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().
45 +
46 + Note: These declarations are used in posix_fallocate.c and
47 + posix_fallocate64.c, so we expose them internally.
48 + */
49 +
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. */
55 +
56 +# ifndef __USE_FILE_OFFSET64
57 +extern int fallocate (int __fd, int __mode, __off_t __offset, __off_t __len);
58 +# else
59 +# ifdef __REDIRECT
60 +extern int __REDIRECT (fallocate, (int __fd, int __mode, __off64_t __offset,
61 + __off64_t __len),
62 + fallocate64);
63 +# else
64 +# define fallocate fallocate64
65 +# endif
66 +# endif
67 +# ifdef __USE_LARGEFILE64
68 +extern int fallocate64 (int __fd, int __mode, __off64_t __offset, __off64_t __len);
69 +# endif
70 +#endif
71 +
72 __END_DECLS
73
74 #endif /* fcntl.h */
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) += \
78 vmsplice.c
79 CSRC-$(if $(findstring yy,$(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_LFS)),y) += \
80 sendfile64.c
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)
88 --- /dev/null
89 +++ b/libc/sysdeps/linux/common/fallocate.c
90 @@ -0,0 +1,48 @@
91 +/* vi: set sw=4 ts=4: */
92 +/*
93 + * fallocate() for uClibc - Based off of posix_fallocate() by Erik Andersen
94 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
95 + *
96 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
97 + *
98 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
99 + */
100 +
101 +#include <sys/syscall.h>
102 +#include <fcntl.h>
103 +#include <bits/kernel-features.h>
104 +#include <stdint.h>
105 +
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)
109 +{
110 + int ret;
111 +
112 +# if __WORDSIZE == 32
113 + uint32_t off_low = offset;
114 + uint32_t len_low = len;
115 + /* may assert that these >>31 are 0 */
116 + uint32_t zero = 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));
124 +# else
125 +# error your machine is neither 32 bit or 64 bit ... it must be magical
126 +# endif
127 + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
128 + return INTERNAL_SYSCALL_ERRNO (ret, err);
129 + return 0;
130 +}
131 +
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)
136 +# endif
137 +# endif
138 +#endif
139 --- /dev/null
140 +++ b/libc/sysdeps/linux/common/fallocate64.c
141 @@ -0,0 +1,42 @@
142 +/* vi: set sw=4 ts=4: */
143 +/*
144 + * fallocate() for uClibc - based off posix_fallocate() by Erik Andersen
145 + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
146 + *
147 + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
148 + *
149 + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
150 + */
151 +
152 +#include <sys/syscall.h>
153 +
154 +#include <fcntl.h>
155 +#include <bits/kernel-features.h>
156 +#include <stdint.h>
157 +
158 +#if defined __NR_fallocate
159 +
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,
165 + __off64_t len)
166 +{
167 + int ret;
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);
173 + return 0;
174 +}
175 +
176 +# if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU
177 +strong_alias(__libc_fallocate64,fallocate64)
178 +# endif
179 +
180 +# else
181 +# error your machine is neither 32 bit or 64 bit ... it must be magical
182 +# endif
183 +#endif
184 --- a/libc/sysdeps/linux/common/posix_fallocate.c
185 +++ b/libc/sysdeps/linux/common/posix_fallocate.c
186 @@ -14,28 +14,10 @@
187 #include <stdint.h>
188
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)
192 {
193 - int ret;
194 -
195 -# if __WORDSIZE == 32
196 - uint32_t off_low = offset;
197 - uint32_t len_low = len;
198 - /* may assert that these >>31 are 0 */
199 - uint32_t zero = 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));
207 -# else
208 -# error your machine is neither 32 bit or 64 bit ... it must be magical
209 -#endif
210 - if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
211 - return INTERNAL_SYSCALL_ERRNO (ret, err);
212 - return 0;
213 + return __libc_fallocate(fd, 0, offset, len);
214 }
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
219 @@ -18,22 +18,12 @@
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)
225 {
226 - int ret;
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);
237 - return 0;
238 + return __libc_fallocate64(fd, 0, offset, len);
239 }
240 # else
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
243 # endif
244 #endif
245 --- a/test/.gitignore
246 +++ b/test/.gitignore
247 @@ -302,6 +302,8 @@ unistd/getcwd
248 unistd/getopt
249 unistd/getopt_long
250 unistd/tstgetopt
251 +unistd/tst-fallocate
252 +unistd/tst-fallocate64
253 unistd/tst-posix_fallocate
254 unistd/tst-posix_fallocate64
255 unistd/tst-preadwrite
256 --- /dev/null
257 +++ b/test/unistd/tst-fallocate.c
258 @@ -0,0 +1,166 @@
259 +#include <fcntl.h>
260 +#include <sys/stat.h>
261 +
262 +#ifndef TST_FALLOCATE64
263 +# define stat64 stat
264 +# define fstat64 fstat
265 +# else
266 +# ifndef O_LARGEFILE
267 +# error no O_LARGEFILE but you want to test with LFS enabled
268 +# endif
269 +#endif
270 +
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>
276 +
277 +static int fd;
278 +static void
279 +do_prepare (void)
280 +{
281 + fd = create_temp_file ("tst-fallocate.", NULL);
282 + if (fd == -1)
283 + {
284 + printf ("cannot create temporary file: %m\n");
285 + exit (1);
286 + }
287 +}
288 +
289 +
290 +static int
291 +do_test (void)
292 +{
293 + struct stat64 st;
294 + int c;
295 + char garbage[4096];
296 + blkcnt_t blksb4;
297 +
298 + if (fstat64 (fd, &st) != 0)
299 + {
300 + puts ("1st fstat failed");
301 + return 1;
302 + }
303 +
304 + if (st.st_size != 0)
305 + {
306 + puts ("file not created with size 0");
307 + return 1;
308 + }
309 +
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)
314 + {
315 + puts ("1st fallocate call failed");
316 + return 1;
317 + }
318 +
319 + if (fstat64 (fd, &st) != 0)
320 + {
321 + puts ("2nd fstat failed");
322 + return 1;
323 + }
324 +
325 + if (st.st_size != 8 * 4096 + 128)
326 + {
327 + printf ("file size after 1st fallocate call is %llu, expected %u\n",
328 + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
329 + return 1;
330 + }
331 +
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)
334 + {
335 + puts ("2nd fallocate call failed");
336 + return 1;
337 + }
338 +
339 + if (fstat64 (fd, &st) != 0)
340 + {
341 + puts ("3rd fstat failed");
342 + return 1;
343 + }
344 +
345 + if (st.st_size != 8 * 4096 + 128)
346 + {
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);
349 + return 1;
350 + }
351 +
352 + /* Let's fill up the first eight 4k blocks with 'x' to force some allocations. */
353 +
354 + memset(garbage, 'x', 4096);
355 + for(c=0; c < 8; c++)
356 + if(write(fd, garbage, 4096) == -1)
357 + {
358 + puts ("write failed");
359 + return 1;
360 + }
361 +
362 + if (fstat64 (fd, &st) != 0)
363 + {
364 + puts ("4th fstat failed");
365 + return 1;
366 + }
367 +
368 + blksb4 = st.st_blocks;
369 +
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)
372 + {
373 + puts ("3rd fallocate call failed");
374 + return 1;
375 + }
376 +
377 + if (fstat64 (fd, &st) != 0)
378 + {
379 + puts ("5th fstat failed");
380 + return 1;
381 + }
382 +
383 + if (st.st_size != 8 * 4096 + 128)
384 + {
385 + printf ("file size after 3rd fallocate call is %llu, expected %u\n",
386 + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
387 + return 1;
388 + }
389 +
390 + /* The number of allocated blocks should decrease. I hope this works on
391 + all filesystems! */
392 + if (st.st_blocks >= blksb4)
393 + {
394 + printf ("number of blocks after 3rd fallocate call is %lu, expected less than %lu\n",
395 + (unsigned long int) st.st_blocks, blksb4);
396 + return 1;
397 + }
398 +
399 +#ifdef TST_FALLOCATE64
400 + /* We'll just do a mode = 0 test for fallocate64() */
401 + if (fallocate64 (fd, 0, 4097ULL, 4294967295ULL + 2ULL) != 0)
402 + {
403 + puts ("1st fallocate64 call failed");
404 + return 1;
405 + }
406 +
407 + if (fstat64 (fd, &st) != 0)
408 + {
409 + puts ("6th fstat failed");
410 + return 1;
411 + }
412 +
413 + if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
414 + {
415 + printf ("file size after 1st fallocate64 call is %llu, expected %llu\n",
416 + (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
417 + return 1;
418 + }
419 +#endif
420 + close (fd);
421 +
422 + return 0;
423 +}
424 +
425 --- /dev/null
426 +++ b/test/unistd/tst-fallocate64.c
427 @@ -0,0 +1,2 @@
428 +#define TST_FALLOCATE64
429 +#include "tst-fallocate.c"
430 --- a/test/unistd/Makefile.in
431 +++ b/test/unistd/Makefile.in
432 @@ -2,10 +2,13 @@
433 # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
434
435 ifeq ($(UCLIBC_HAS_LFS),)
436 -TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
437 +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64 tst-fallocate64
438 endif
439 ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
440 -TESTS_DISABLED := tst-posix_fallocate
441 +TESTS_DISABLED := tst-posix_fallocate tst-fallocate64
442 +endif
443 +ifeq ($(UCLIBC_LINUX_SPECIFIC),)
444 +TESTS_DISABLED += tst-fallocate
445 endif
446 OPTS_getopt := -abcXXX -9
447 OPTS_getopt_long := --add XXX --delete YYY --verbose