022-libc-add-posix_fallocate.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
  5. ---
  6. create mode 100644 libc/sysdeps/linux/common/posix_fallocate.c
  7. create mode 100644 libc/sysdeps/linux/common/posix_fallocate64.c
  8. create mode 100644 test/unistd/tst-posix_fallocate.c
  9. create mode 100644 test/unistd/tst-posix_fallocate64.c
  10. --- a/include/fcntl.h
  11. +++ b/include/fcntl.h
  12. @@ -210,9 +210,7 @@ extern int posix_fadvise64 (int __fd, __
  13. #endif
  14. -#if 0 /* && defined __UCLIBC_HAS_ADVANCED_REALTIME__ */
  15. -
  16. -/* FIXME -- uClibc should probably implement these... */
  17. +#if defined __UCLIBC_HAS_ADVANCED_REALTIME__
  18. /* Reserve storage for the data of the file associated with FD.
  19. --- a/libc/sysdeps/linux/common/Makefile.in
  20. +++ b/libc/sysdeps/linux/common/Makefile.in
  21. @@ -81,7 +81,8 @@ CSRC-$(UCLIBC_HAS_REALTIME) += clock_get
  22. sched_get_priority_max.c sched_get_priority_min.c sched_getscheduler.c \
  23. sched_rr_get_interval.c sched_setparam.c sched_setscheduler.c sigqueue.c
  24. # 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
  25. -CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_fadvise64.c posix_fadvise.c posix_madvise.c
  26. +CSRC-$(UCLIBC_HAS_ADVANCED_REALTIME) += posix_fadvise64.c posix_fadvise.c posix_madvise.c \
  27. + posix_fallocate.c posix_fallocate64.c
  28. CSRC-$(UCLIBC_SUSV4_LEGACY) += utime.c
  29. CSRC-$(UCLIBC_HAS_EPOLL) += epoll.c
  30. CSRC-$(UCLIBC_HAS_XATTR) += xattr.c
  31. --- a/libc/sysdeps/linux/common/bits/kernel-features.h
  32. +++ b/libc/sysdeps/linux/common/bits/kernel-features.h
  33. @@ -494,6 +494,14 @@
  34. # define __ASSUME_PRIVATE_FUTEX 1
  35. #endif
  36. +/* Support for fallocate was added in 2.6.23,
  37. + on s390 only after 2.6.23-rc1, on alpha only after 2.6.33-rc1. */
  38. +#if __LINUX_KERNEL_VERSION >= 0x020617 \
  39. + && (!defined __s390__ || __LINUX_KERNEL_VERSION >= 0x020618) \
  40. + && (!defined __alpha__ || __LINUX_KERNEL_VERSION >= 0x020621)
  41. +# define __ASSUME_FALLOCATE 1
  42. +#endif
  43. +
  44. /* getcpu is a syscall for x86-64 since 3.1. */
  45. #if defined __x86_64__ && __LINUX_KERNEL_VERSION >= 0x030100
  46. # define __ASSUME_GETCPU_SYSCALL 1
  47. --- /dev/null
  48. +++ b/libc/sysdeps/linux/common/posix_fallocate.c
  49. @@ -0,0 +1,43 @@
  50. +/* vi: set sw=4 ts=4: */
  51. +/*
  52. + * posix_fallocate() for uClibc
  53. + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
  54. + *
  55. + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  56. + *
  57. + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  58. + */
  59. +
  60. +#include <sys/syscall.h>
  61. +#include <fcntl.h>
  62. +#include <bits/kernel-features.h>
  63. +#include <stdint.h>
  64. +
  65. +#if defined __NR_fallocate
  66. +int posix_fallocate(int fd, __off_t offset, __off_t len)
  67. +{
  68. + int ret;
  69. +
  70. +# if __WORDSIZE == 32
  71. + uint32_t off_low = offset;
  72. + uint32_t len_low = len;
  73. + /* may assert that these >>31 are 0 */
  74. + uint32_t zero = 0;
  75. + INTERNAL_SYSCALL_DECL(err);
  76. + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
  77. + __LONG_LONG_PAIR (zero, off_low),
  78. + __LONG_LONG_PAIR (zero, len_low)));
  79. +# elif __WORDSIZE == 64
  80. + INTERNAL_SYSCALL_DECL(err);
  81. + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, 0, offset, len));
  82. +# else
  83. +# error your machine is neither 32 bit or 64 bit ... it must be magical
  84. +#endif
  85. + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
  86. + return INTERNAL_SYSCALL_ERRNO (ret, err);
  87. + return 0;
  88. +}
  89. +# if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
  90. +strong_alias(posix_fallocate,posix_fallocate64)
  91. +# endif
  92. +#endif
  93. --- /dev/null
  94. +++ b/libc/sysdeps/linux/common/posix_fallocate64.c
  95. @@ -0,0 +1,39 @@
  96. +/* vi: set sw=4 ts=4: */
  97. +/*
  98. + * posix_fallocate() for uClibc
  99. + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
  100. + *
  101. + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  102. + *
  103. + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  104. + */
  105. +
  106. +#include <sys/syscall.h>
  107. +#include <fcntl.h>
  108. +#include <bits/kernel-features.h>
  109. +#include <stdint.h>
  110. +
  111. +#if defined __NR_fallocate
  112. +
  113. +# if __WORDSIZE == 64
  114. +/* Can use normal posix_fallocate() */
  115. +# elif __WORDSIZE == 32
  116. +int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
  117. +{
  118. + int ret;
  119. + uint32_t off_low = offset & 0xffffffff;
  120. + uint32_t off_high = offset >> 32;
  121. + uint32_t len_low = len & 0xffffffff;
  122. + uint32_t len_high = len >> 32;
  123. + INTERNAL_SYSCALL_DECL(err);
  124. + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
  125. + __LONG_LONG_PAIR (off_high, off_low),
  126. + __LONG_LONG_PAIR (len_high, len_low)));
  127. + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
  128. + return INTERNAL_SYSCALL_ERRNO (ret, err);
  129. + return 0;
  130. +}
  131. +# else
  132. +# error your machine is neither 32 bit or 64 bit ... it must be magical
  133. +# endif
  134. +#endif
  135. --- a/test/.gitignore
  136. +++ b/test/.gitignore
  137. @@ -302,6 +302,8 @@ unistd/getcwd
  138. unistd/getopt
  139. unistd/getopt_long
  140. unistd/tstgetopt
  141. +unistd/tst-posix_fallocate
  142. +unistd/tst-posix_fallocate64
  143. unistd/tst-preadwrite
  144. unistd/tst-preadwrite64
  145. unistd/vfork
  146. --- a/test/unistd/Makefile.in
  147. +++ b/test/unistd/Makefile.in
  148. @@ -2,7 +2,10 @@
  149. # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  150. ifeq ($(UCLIBC_HAS_LFS),)
  151. -TESTS_DISABLED := tst-preadwrite64
  152. +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
  153. +endif
  154. +ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
  155. +TESTS_DISABLED := tst-posix_fallocate
  156. endif
  157. OPTS_getopt := -abcXXX -9
  158. OPTS_getopt_long := --add XXX --delete YYY --verbose
  159. --- /dev/null
  160. +++ b/test/unistd/tst-posix_fallocate.c
  161. @@ -0,0 +1,127 @@
  162. +#include <fcntl.h>
  163. +#include <sys/stat.h>
  164. +
  165. +#ifndef TST_POSIX_FALLOCATE64
  166. +# define stat64 stat
  167. +# define fstat64 fstat
  168. +# else
  169. +# ifndef O_LARGEFILE
  170. +# error no O_LARGEFILE but you want to test with LFS enabled
  171. +# endif
  172. +#endif
  173. +
  174. +static void do_prepare (void);
  175. +#define PREPARE(argc, argv) do_prepare ()
  176. +static int do_test (void);
  177. +#define TEST_FUNCTION do_test ()
  178. +#include <test-skeleton.c>
  179. +
  180. +static int fd;
  181. +static void
  182. +do_prepare (void)
  183. +{
  184. + fd = create_temp_file ("tst-posix_fallocate.", NULL);
  185. + if (fd == -1)
  186. + {
  187. + printf ("cannot create temporary file: %m\n");
  188. + exit (1);
  189. + }
  190. +}
  191. +
  192. +
  193. +static int
  194. +do_test (void)
  195. +{
  196. + struct stat64 st;
  197. +
  198. + if (fstat64 (fd, &st) != 0)
  199. + {
  200. + puts ("1st fstat failed");
  201. + return 1;
  202. + }
  203. +
  204. + if (st.st_size != 0)
  205. + {
  206. + puts ("file not created with size 0");
  207. + return 1;
  208. + }
  209. +
  210. + if (posix_fallocate (fd, 512, 768) != 0)
  211. + {
  212. + puts ("1st posix_fallocate call failed");
  213. + return 1;
  214. + }
  215. +
  216. + if (fstat64 (fd, &st) != 0)
  217. + {
  218. + puts ("2nd fstat failed");
  219. + return 1;
  220. + }
  221. +
  222. + if (st.st_size != 512 + 768)
  223. + {
  224. + printf ("file size after 1st posix_fallocate call is %llu, expected %u\n",
  225. + (unsigned long long int) st.st_size, 512u + 768u);
  226. + return 1;
  227. + }
  228. +
  229. + if (posix_fallocate (fd, 0, 1024) != 0)
  230. + {
  231. + puts ("2nd posix_fallocate call failed");
  232. + return 1;
  233. + }
  234. +
  235. + if (fstat64 (fd, &st) != 0)
  236. + {
  237. + puts ("3rd fstat failed");
  238. + return 1;
  239. + }
  240. +
  241. + if (st.st_size != 512 + 768)
  242. + {
  243. + puts ("file size changed in 2nd posix_fallocate");
  244. + return 1;
  245. + }
  246. +
  247. + if (posix_fallocate (fd, 2048, 64) != 0)
  248. + {
  249. + puts ("3rd posix_fallocate call failed");
  250. + return 1;
  251. + }
  252. +
  253. + if (fstat64 (fd, &st) != 0)
  254. + {
  255. + puts ("4th fstat failed");
  256. + return 1;
  257. + }
  258. +
  259. + if (st.st_size != 2048 + 64)
  260. + {
  261. + printf ("file size after 3rd posix_fallocate call is %llu, expected %u\n",
  262. + (unsigned long long int) st.st_size, 2048u + 64u);
  263. + return 1;
  264. + }
  265. +#ifdef TST_POSIX_FALLOCATE64
  266. + if (posix_fallocate64 (fd, 4097ULL, 4294967295ULL + 2ULL) != 0)
  267. + {
  268. + puts ("4th posix_fallocate call failed");
  269. + return 1;
  270. + }
  271. +
  272. + if (fstat64 (fd, &st) != 0)
  273. + {
  274. + puts ("5th fstat failed");
  275. + return 1;
  276. + }
  277. +
  278. + if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
  279. + {
  280. + printf ("file size after 4th posix_fallocate call is %llu, expected %llu\n",
  281. + (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
  282. + return 1;
  283. + }
  284. +#endif
  285. + close (fd);
  286. +
  287. + return 0;
  288. +}
  289. --- /dev/null
  290. +++ b/test/unistd/tst-posix_fallocate64.c
  291. @@ -0,0 +1,2 @@
  292. +#define TST_POSIX_FALLOCATE64
  293. +#include "tst-posix_fallocate.c"