tst-fallocate.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <fcntl.h>
  2. #include <sys/stat.h>
  3. #ifndef TST_FALLOCATE64
  4. # define stat64 stat
  5. # define fstat64 fstat
  6. # else
  7. # ifndef O_LARGEFILE
  8. # error no O_LARGEFILE but you want to test with LFS enabled
  9. # endif
  10. #endif
  11. static void do_prepare(void);
  12. static int do_test(void);
  13. #define PREPARE(argc, argv) do_prepare ()
  14. #define TEST_FUNCTION do_test ()
  15. #include <test-skeleton.c>
  16. static int fd;
  17. static void
  18. do_prepare (void)
  19. {
  20. fd = create_temp_file ("tst-fallocate.", NULL);
  21. if (fd == -1)
  22. {
  23. printf ("cannot create temporary file: %m\n");
  24. exit (1);
  25. }
  26. }
  27. static int
  28. do_test (void)
  29. {
  30. struct stat64 st;
  31. int c;
  32. char garbage[4096];
  33. blkcnt_t blksb4;
  34. if (fstat64 (fd, &st) != 0)
  35. {
  36. puts ("1st fstat failed");
  37. return 1;
  38. }
  39. if (st.st_size != 0)
  40. {
  41. puts ("file not created with size 0");
  42. return 1;
  43. }
  44. /* This is the default mode which is identical to posix_fallocate().
  45. Note: we need a few extra blocks for FALLOC_FL_PUNCH_HOLE below.
  46. While block sizes vary, we'll assume eight 4K blocks for good measure. */
  47. if (fallocate (fd, 0, 8 * 4096, 128) != 0)
  48. {
  49. puts ("1st fallocate call failed");
  50. return 1;
  51. }
  52. if (fstat64 (fd, &st) != 0)
  53. {
  54. puts ("2nd fstat failed");
  55. return 1;
  56. }
  57. if (st.st_size != 8 * 4096 + 128)
  58. {
  59. printf ("file size after 1st fallocate call is %llu, expected %u\n",
  60. (unsigned long long int) st.st_size, 8u * 4096u + 128u);
  61. return 1;
  62. }
  63. /* Without FALLOC_FL_KEEP_SIZE, this would increaste the size of the file. */
  64. if (fallocate (fd, FALLOC_FL_KEEP_SIZE, 0, 16 * 4096) != 0)
  65. {
  66. puts ("2nd fallocate call failed");
  67. return 1;
  68. }
  69. if (fstat64 (fd, &st) != 0)
  70. {
  71. puts ("3rd fstat failed");
  72. return 1;
  73. }
  74. if (st.st_size != 8 * 4096 + 128)
  75. {
  76. printf ("file size changed in 2nd fallocate call to %llu, expected %u\n",
  77. (unsigned long long int) st.st_size, 8u * 4096u + 128u);
  78. return 1;
  79. }
  80. /* Let's fill up the first eight 4k blocks with 'x' to force some allocations. */
  81. memset(garbage, 'x', 4096);
  82. for(c=0; c < 8; c++)
  83. if(write(fd, garbage, 4096) == -1)
  84. {
  85. puts ("write failed");
  86. return 1;
  87. }
  88. if (fstat64 (fd, &st) != 0)
  89. {
  90. puts ("4th fstat failed");
  91. return 1;
  92. }
  93. blksb4 = st.st_blocks;
  94. /* Let's punch a hole in the entire file, turning it effectively into a sparse file. */
  95. if (fallocate (fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 8 * 4096 + 128) != 0)
  96. {
  97. puts ("3rd fallocate call failed");
  98. return 1;
  99. }
  100. if (fstat64 (fd, &st) != 0)
  101. {
  102. puts ("5th fstat failed");
  103. return 1;
  104. }
  105. if (st.st_size != 8 * 4096 + 128)
  106. {
  107. printf ("file size after 3rd fallocate call is %llu, expected %u\n",
  108. (unsigned long long int) st.st_size, 8u * 4096u + 128u);
  109. return 1;
  110. }
  111. /* The number of allocated blocks should decrease. I hope this works on
  112. all filesystems! */
  113. if (st.st_blocks >= blksb4)
  114. {
  115. printf ("number of blocks after 3rd fallocate call is %lu, expected less than %lu\n",
  116. (unsigned long int) st.st_blocks, blksb4);
  117. return 1;
  118. }
  119. #ifdef TST_FALLOCATE64
  120. /* We'll just do a mode = 0 test for fallocate64() */
  121. if (fallocate64 (fd, 0, 4097ULL, 4294967295ULL + 2ULL) != 0)
  122. {
  123. puts ("1st fallocate64 call failed");
  124. return 1;
  125. }
  126. if (fstat64 (fd, &st) != 0)
  127. {
  128. puts ("6th fstat failed");
  129. return 1;
  130. }
  131. if (st.st_size != 4097ULL + 4294967295ULL + 2ULL)
  132. {
  133. printf ("file size after 1st fallocate64 call is %llu, expected %llu\n",
  134. (unsigned long long int) st.st_size, 4097ULL + 4294967295ULL + 2ULL);
  135. return 1;
  136. }
  137. #endif
  138. close (fd);
  139. return 0;
  140. }