023-libc-add-fallocate-and-fallocate64.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. We add the Linux-specific function fallocate() which allows the user to
  5. directly manipulate allocate space for a file. fallocate() can operate
  6. in different modes, but the default mode is equivalent to posix_fallocate()
  7. which is specified in POSIX.1.
  8. Recent releases of e2fsprogs 1.42.11 and above expect fallocate64() to be
  9. available.
  10. Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
  11. Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
  12. ---
  13. create mode 100644 libc/sysdeps/linux/common/fallocate.c
  14. create mode 100644 libc/sysdeps/linux/common/fallocate64.c
  15. create mode 100644 test/unistd/tst-fallocate.c
  16. create mode 100644 test/unistd/tst-fallocate64.c
  17. --- a/extra/Configs/Config.in
  18. +++ b/extra/Configs/Config.in
  19. @@ -952,8 +952,8 @@ config UCLIBC_LINUX_SPECIFIC
  20. default y
  21. help
  22. accept4(), bdflush(),
  23. - capget(), capset(), eventfd(), fstatfs(),
  24. - inotify_*(), ioperm(), iopl(),
  25. + capget(), capset(), eventfd(), fallocate(),
  26. + fstatfs(), inotify_*(), ioperm(), iopl(),
  27. madvise(), modify_ldt(), pipe2(), personality(),
  28. prctl()/arch_prctl(), pivot_root(), modify_ldt(),
  29. ppoll(), readahead(), reboot(), remap_file_pages(),
  30. --- a/include/fcntl.h
  31. +++ b/include/fcntl.h
  32. @@ -237,6 +237,38 @@ extern int __fcntl_nocancel (int fd, int
  33. libc_hidden_proto(__fcntl_nocancel)
  34. #endif
  35. +#if (defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU) || defined _LIBC
  36. +/* Reserve storage for the data of a file associated with FD. This function
  37. + is Linux-specific. For the portable version, use posix_fallocate().
  38. + Unlike the latter, fallocate can operate in different modes. The default
  39. + mode = 0 is equivalent to posix_fallocate().
  40. +
  41. + Note: These declarations are used in posix_fallocate.c and
  42. + posix_fallocate64.c, so we expose them internally.
  43. + */
  44. +
  45. +/* Flags for fallocate's mode. */
  46. +# define FALLOC_FL_KEEP_SIZE 1 /* Don't extend size of file
  47. + even if offset + len is
  48. + greater than file size. */
  49. +# define FALLOC_FL_PUNCH_HOLE 2 /* Create a hole in the file. */
  50. +
  51. +# ifndef __USE_FILE_OFFSET64
  52. +extern int fallocate (int __fd, int __mode, __off_t __offset, __off_t __len);
  53. +# else
  54. +# ifdef __REDIRECT
  55. +extern int __REDIRECT (fallocate, (int __fd, int __mode, __off64_t __offset,
  56. + __off64_t __len),
  57. + fallocate64);
  58. +# else
  59. +# define fallocate fallocate64
  60. +# endif
  61. +# endif
  62. +# ifdef __USE_LARGEFILE64
  63. +extern int fallocate64 (int __fd, int __mode, __off64_t __offset, __off64_t __len);
  64. +# endif
  65. +#endif
  66. +
  67. __END_DECLS
  68. #endif /* fcntl.h */
  69. --- a/libc/sysdeps/linux/common/Makefile.in
  70. +++ b/libc/sysdeps/linux/common/Makefile.in
  71. @@ -61,6 +61,10 @@ CSRC-$(UCLIBC_LINUX_SPECIFIC) += \
  72. vmsplice.c
  73. CSRC-$(if $(findstring yy,$(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_LFS)),y) += \
  74. sendfile64.c
  75. +# posix_fallocate() needs __libc_fallocate() from fallocate.c
  76. +# posix_fallocate64() needs __libc_fallocate64() from fallocate64.c
  77. +CSRC-$(if $(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_ADVANCED_REALTIME),y,) += \
  78. + fallocate.c $(filter fallocate64.c,$(CSRC-y))
  79. # NPTL needs these internally: madvise.c
  80. CSRC-$(findstring y,$(UCLIBC_LINUX_SPECIFIC)$(UCLIBC_HAS_THREADS_NATIVE)) += madvise.c
  81. ifeq ($(UCLIBC_HAS_THREADS_NATIVE),y)
  82. --- /dev/null
  83. +++ b/libc/sysdeps/linux/common/fallocate.c
  84. @@ -0,0 +1,48 @@
  85. +/* vi: set sw=4 ts=4: */
  86. +/*
  87. + * fallocate() for uClibc - Based off of posix_fallocate() by Erik Andersen
  88. + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
  89. + *
  90. + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  91. + *
  92. + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  93. + */
  94. +
  95. +#include <sys/syscall.h>
  96. +#include <fcntl.h>
  97. +#include <bits/kernel-features.h>
  98. +#include <stdint.h>
  99. +
  100. +#if defined __NR_fallocate
  101. +extern __typeof(fallocate) __libc_fallocate attribute_hidden;
  102. +int attribute_hidden __libc_fallocate(int fd, int mode, __off_t offset, __off_t len)
  103. +{
  104. + int ret;
  105. +
  106. +# if __WORDSIZE == 32
  107. + uint32_t off_low = offset;
  108. + uint32_t len_low = len;
  109. + /* may assert that these >>31 are 0 */
  110. + uint32_t zero = 0;
  111. + INTERNAL_SYSCALL_DECL(err);
  112. + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, mode,
  113. + __LONG_LONG_PAIR (zero, off_low),
  114. + __LONG_LONG_PAIR (zero, len_low)));
  115. +# elif __WORDSIZE == 64
  116. + INTERNAL_SYSCALL_DECL(err);
  117. + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, mode, offset, len));
  118. +# else
  119. +# error your machine is neither 32 bit or 64 bit ... it must be magical
  120. +# endif
  121. + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
  122. + return INTERNAL_SYSCALL_ERRNO (ret, err);
  123. + return 0;
  124. +}
  125. +
  126. +# if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU
  127. +strong_alias(__libc_fallocate,fallocate)
  128. +# if __WORDSIZE == 64
  129. +strong_alias(__libc_fallocate,fallocate64)
  130. +# endif
  131. +# endif
  132. +#endif
  133. --- /dev/null
  134. +++ b/libc/sysdeps/linux/common/fallocate64.c
  135. @@ -0,0 +1,42 @@
  136. +/* vi: set sw=4 ts=4: */
  137. +/*
  138. + * fallocate() for uClibc - based off posix_fallocate() by Erik Andersen
  139. + * http://www.opengroup.org/onlinepubs/9699919799/functions/posix_fallocate.html
  140. + *
  141. + * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  142. + *
  143. + * Licensed under the LGPL v2.1 or later, see the file COPYING.LIB in this tarball.
  144. + */
  145. +
  146. +#include <sys/syscall.h>
  147. +
  148. +#include <fcntl.h>
  149. +#include <bits/kernel-features.h>
  150. +#include <stdint.h>
  151. +
  152. +#if defined __NR_fallocate
  153. +
  154. +# if __WORDSIZE == 64
  155. +/* Can use normal fallocate() */
  156. +# elif __WORDSIZE == 32
  157. +extern __typeof(fallocate64) __libc_fallocate64 attribute_hidden;
  158. +int attribute_hidden __libc_fallocate64(int fd, int mode, __off64_t offset,
  159. + __off64_t len)
  160. +{
  161. + int ret;
  162. + INTERNAL_SYSCALL_DECL(err);
  163. + ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, mode,
  164. + OFF64_HI_LO (offset), OFF64_HI_LO (len)));
  165. + if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
  166. + return INTERNAL_SYSCALL_ERRNO (ret, err);
  167. + return 0;
  168. +}
  169. +
  170. +# if defined __UCLIBC_LINUX_SPECIFIC__ && defined __USE_GNU
  171. +strong_alias(__libc_fallocate64,fallocate64)
  172. +# endif
  173. +
  174. +# else
  175. +# error your machine is neither 32 bit or 64 bit ... it must be magical
  176. +# endif
  177. +#endif
  178. --- a/libc/sysdeps/linux/common/posix_fallocate.c
  179. +++ b/libc/sysdeps/linux/common/posix_fallocate.c
  180. @@ -14,28 +14,10 @@
  181. #include <stdint.h>
  182. #if defined __NR_fallocate
  183. +extern __typeof(fallocate) __libc_fallocate attribute_hidden;
  184. int posix_fallocate(int fd, __off_t offset, __off_t len)
  185. {
  186. - int ret;
  187. -
  188. -# if __WORDSIZE == 32
  189. - uint32_t off_low = offset;
  190. - uint32_t len_low = len;
  191. - /* may assert that these >>31 are 0 */
  192. - uint32_t zero = 0;
  193. - INTERNAL_SYSCALL_DECL(err);
  194. - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
  195. - __LONG_LONG_PAIR (zero, off_low),
  196. - __LONG_LONG_PAIR (zero, len_low)));
  197. -# elif __WORDSIZE == 64
  198. - INTERNAL_SYSCALL_DECL(err);
  199. - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 4, fd, 0, offset, len));
  200. -# else
  201. -# error your machine is neither 32 bit or 64 bit ... it must be magical
  202. -#endif
  203. - if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
  204. - return INTERNAL_SYSCALL_ERRNO (ret, err);
  205. - return 0;
  206. + return __libc_fallocate(fd, 0, offset, len);
  207. }
  208. # if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
  209. strong_alias(posix_fallocate,posix_fallocate64)
  210. --- a/libc/sysdeps/linux/common/posix_fallocate64.c
  211. +++ b/libc/sysdeps/linux/common/posix_fallocate64.c
  212. @@ -18,22 +18,12 @@
  213. # if __WORDSIZE == 64
  214. /* Can use normal posix_fallocate() */
  215. # elif __WORDSIZE == 32
  216. +extern __typeof(fallocate64) __libc_fallocate64 attribute_hidden;
  217. int posix_fallocate64(int fd, __off64_t offset, __off64_t len)
  218. {
  219. - int ret;
  220. - uint32_t off_low = offset & 0xffffffff;
  221. - uint32_t off_high = offset >> 32;
  222. - uint32_t len_low = len & 0xffffffff;
  223. - uint32_t len_high = len >> 32;
  224. - INTERNAL_SYSCALL_DECL(err);
  225. - ret = (int) (INTERNAL_SYSCALL(fallocate, err, 6, fd, 0,
  226. - __LONG_LONG_PAIR (off_high, off_low),
  227. - __LONG_LONG_PAIR (len_high, len_low)));
  228. - if (unlikely(INTERNAL_SYSCALL_ERROR_P (ret, err)))
  229. - return INTERNAL_SYSCALL_ERRNO (ret, err);
  230. - return 0;
  231. + return __libc_fallocate64(fd, 0, offset, len);
  232. }
  233. # else
  234. -# error your machine is neither 32 bit or 64 bit ... it must be magical
  235. +# error your machine is neither 32 bit or 64 bit ... it must be magical
  236. # endif
  237. #endif
  238. --- a/test/.gitignore
  239. +++ b/test/.gitignore
  240. @@ -302,6 +302,8 @@ unistd/getcwd
  241. unistd/getopt
  242. unistd/getopt_long
  243. unistd/tstgetopt
  244. +unistd/tst-fallocate
  245. +unistd/tst-fallocate64
  246. unistd/tst-posix_fallocate
  247. unistd/tst-posix_fallocate64
  248. unistd/tst-preadwrite
  249. --- /dev/null
  250. +++ b/test/unistd/tst-fallocate.c
  251. @@ -0,0 +1,166 @@
  252. +#include <fcntl.h>
  253. +#include <sys/stat.h>
  254. +
  255. +#ifndef TST_FALLOCATE64
  256. +# define stat64 stat
  257. +# define fstat64 fstat
  258. +# else
  259. +# ifndef O_LARGEFILE
  260. +# error no O_LARGEFILE but you want to test with LFS enabled
  261. +# endif
  262. +#endif
  263. +
  264. +static void do_prepare(void);
  265. +static int do_test(void);
  266. +#define PREPARE(argc, argv) do_prepare ()
  267. +#define TEST_FUNCTION do_test ()
  268. +#include <test-skeleton.c>
  269. +
  270. +static int fd;
  271. +static void
  272. +do_prepare (void)
  273. +{
  274. + fd = create_temp_file ("tst-fallocate.", NULL);
  275. + if (fd == -1)
  276. + {
  277. + printf ("cannot create temporary file: %m\n");
  278. + exit (1);
  279. + }
  280. +}
  281. +
  282. +
  283. +static int
  284. +do_test (void)
  285. +{
  286. + struct stat64 st;
  287. + int c;
  288. + char garbage[4096];
  289. + blkcnt_t blksb4;
  290. +
  291. + if (fstat64 (fd, &st) != 0)
  292. + {
  293. + puts ("1st fstat failed");
  294. + return 1;
  295. + }
  296. +
  297. + if (st.st_size != 0)
  298. + {
  299. + puts ("file not created with size 0");
  300. + return 1;
  301. + }
  302. +
  303. + /* This is the default mode which is identical to posix_fallocate().
  304. + Note: we need a few extra blocks for FALLOC_FL_PUNCH_HOLE below.
  305. + While block sizes vary, we'll assume eight 4K blocks for good measure. */
  306. + if (fallocate (fd, 0, 8 * 4096, 128) != 0)
  307. + {
  308. + puts ("1st fallocate call failed");
  309. + return 1;
  310. + }
  311. +
  312. + if (fstat64 (fd, &st) != 0)
  313. + {
  314. + puts ("2nd fstat failed");
  315. + return 1;
  316. + }
  317. +
  318. + if (st.st_size != 8 * 4096 + 128)
  319. + {
  320. + printf ("file size after 1st fallocate call is %llu, expected %u\n",
  321. + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
  322. + return 1;
  323. + }
  324. +
  325. + /* Without FALLOC_FL_KEEP_SIZE, this would increaste the size of the file. */
  326. + if (fallocate (fd, FALLOC_FL_KEEP_SIZE, 0, 16 * 4096) != 0)
  327. + {
  328. + puts ("2nd fallocate call failed");
  329. + return 1;
  330. + }
  331. +
  332. + if (fstat64 (fd, &st) != 0)
  333. + {
  334. + puts ("3rd fstat failed");
  335. + return 1;
  336. + }
  337. +
  338. + if (st.st_size != 8 * 4096 + 128)
  339. + {
  340. + printf ("file size changed in 2nd fallocate call to %llu, expected %u\n",
  341. + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
  342. + return 1;
  343. + }
  344. +
  345. + /* Let's fill up the first eight 4k blocks with 'x' to force some allocations. */
  346. +
  347. + memset(garbage, 'x', 4096);
  348. + for(c=0; c < 8; c++)
  349. + if(write(fd, garbage, 4096) == -1)
  350. + {
  351. + puts ("write failed");
  352. + return 1;
  353. + }
  354. +
  355. + if (fstat64 (fd, &st) != 0)
  356. + {
  357. + puts ("4th fstat failed");
  358. + return 1;
  359. + }
  360. +
  361. + blksb4 = st.st_blocks;
  362. +
  363. + /* Let's punch a hole in the entire file, turning it effectively into a sparse file. */
  364. + if (fallocate (fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 8 * 4096 + 128) != 0)
  365. + {
  366. + puts ("3rd fallocate call failed");
  367. + return 1;
  368. + }
  369. +
  370. + if (fstat64 (fd, &st) != 0)
  371. + {
  372. + puts ("5th fstat failed");
  373. + return 1;
  374. + }
  375. +
  376. + if (st.st_size != 8 * 4096 + 128)
  377. + {
  378. + printf ("file size after 3rd fallocate call is %llu, expected %u\n",
  379. + (unsigned long long int) st.st_size, 8u * 4096u + 128u);
  380. + return 1;
  381. + }
  382. +
  383. + /* The number of allocated blocks should decrease. I hope this works on
  384. + all filesystems! */
  385. + if (st.st_blocks >= blksb4)
  386. + {
  387. + printf ("number of blocks after 3rd fallocate call is %lu, expected less than %lu\n",
  388. + (unsigned long int) st.st_blocks, blksb4);
  389. + return 1;
  390. + }
  391. +
  392. +#ifdef TST_FALLOCATE64
  393. + /* We'll just do a mode = 0 test for fallocate64() */
  394. + if (fallocate64 (fd, 0, 4097ULL, 4294967295ULL + 2ULL) != 0)
  395. + {
  396. + puts ("1st fallocate64 call failed");
  397. + return 1;
  398. + }
  399. +
  400. + if (fstat64 (fd, &st) != 0)
  401. + {
  402. + puts ("6th fstat failed");
  403. + return 1;
  404. + }
  405. +
  406. + if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
  407. + {
  408. + printf ("file size after 1st fallocate64 call is %llu, expected %llu\n",
  409. + (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
  410. + return 1;
  411. + }
  412. +#endif
  413. + close (fd);
  414. +
  415. + return 0;
  416. +}
  417. +
  418. --- /dev/null
  419. +++ b/test/unistd/tst-fallocate64.c
  420. @@ -0,0 +1,2 @@
  421. +#define TST_FALLOCATE64
  422. +#include "tst-fallocate.c"
  423. --- a/test/unistd/Makefile.in
  424. +++ b/test/unistd/Makefile.in
  425. @@ -2,10 +2,13 @@
  426. # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  427. ifeq ($(UCLIBC_HAS_LFS),)
  428. -TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64
  429. +TESTS_DISABLED := tst-preadwrite64 tst-posix_fallocate64 tst-fallocate64
  430. endif
  431. ifeq ($(UCLIBC_HAS_ADVANCED_REALTIME),)
  432. -TESTS_DISABLED := tst-posix_fallocate
  433. +TESTS_DISABLED := tst-posix_fallocate tst-fallocate64
  434. +endif
  435. +ifeq ($(UCLIBC_LINUX_SPECIFIC),)
  436. +TESTS_DISABLED += tst-fallocate
  437. endif
  438. OPTS_getopt := -abcXXX -9
  439. OPTS_getopt_long := --add XXX --delete YYY --verbose