default.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #define _FASTWRITER_DEFAULT_C
  2. #define _GNU_SOURCE
  3. #define _XOPEN_SOURCE 600
  4. #define _POSIX_C_SOURCE 200112L
  5. #define _LARGEFILE64_SOURCE
  6. #include "config.h"
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <limits.h>
  12. #include <errno.h>
  13. #include <pthread.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <sys/time.h>
  17. #include <fcntl.h>
  18. #ifdef HAVE_LINUX_FALLOC_H
  19. # include <linux/falloc.h>
  20. #endif /* HAVE_LINUX_FALLOC_H */
  21. #include "fastwriter.h"
  22. #include "private.h"
  23. #include "sysinfo.h"
  24. #include "default.h"
  25. #define SYNC_MODE
  26. #define HAVE_FALLOCATE
  27. #define EXT4_WRITEBLOCK 4194304
  28. #define EXT4_PREALLOCATE 1073741824
  29. typedef struct {
  30. int fd;
  31. int sync_mode;
  32. size_t prior_size; /**< original size of file */
  33. size_t preallocated; /**< preallocated bytes */
  34. size_t wr_block; /**< minimal block of data to write */
  35. size_t pa_block; /**< preallocation setp */
  36. } fastwriter_default_t;
  37. int fastwriter_default_open(fastwriter_t *fw, const char *name, fastwriter_flags_t flags) {
  38. int err;
  39. char fs[16];
  40. int open_flags = (O_CREAT|O_WRONLY|O_NOATIME|O_LARGEFILE);
  41. int open_mode = (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  42. fastwriter_default_t *ctx;
  43. err = get_file_fs(name, sizeof(fs) - 1, fs);
  44. if (err) return err;
  45. ctx = (fastwriter_default_t*)malloc(sizeof(fastwriter_default_t));
  46. if (!ctx) return ENOMEM;
  47. memset(ctx, 0, sizeof(fastwriter_default_t));
  48. fw->ctx = ctx;
  49. #ifdef SYNC_MODE
  50. open_flags |= O_DIRECT;
  51. ctx->sync_mode = 1;
  52. #endif /* SYNC_MODE */
  53. ctx->prior_size = 0;
  54. if (!strcmp(fs, "raw")) {
  55. ctx->wr_block = EXT4_WRITEBLOCK;
  56. ctx->pa_block = 0;
  57. ctx->prior_size = (size_t)-1;
  58. } else if (!strcmp(fs, "ext4")) {
  59. ctx->wr_block = EXT4_WRITEBLOCK;
  60. ctx->pa_block = EXT4_PREALLOCATE;
  61. } else if (!strcmp(fs, "btrfs")) {
  62. ctx->wr_block = EXT4_WRITEBLOCK;
  63. ctx->pa_block = EXT4_PREALLOCATE;
  64. } else if (!strcmp(fs, "xfs")) {
  65. ctx->wr_block = EXT4_WRITEBLOCK;
  66. ctx->pa_block = EXT4_PREALLOCATE;
  67. } else {
  68. ctx->wr_block = EXT4_WRITEBLOCK;
  69. ctx->pa_block = 0;
  70. }
  71. if (flags&FASTWRITER_FLAGS_OVERWRITE)
  72. open_flags |= O_TRUNC;
  73. ctx->fd = open(name, open_flags, open_mode);
  74. if (ctx->fd < 0) return errno;
  75. if (((open_flags&FASTWRITER_FLAGS_OVERWRITE)==0)&&(strcmp(fs, "raw"))) {
  76. ctx->prior_size = lseek(ctx->fd, 0, SEEK_END);
  77. # ifdef SYNC_MODE
  78. if (ctx->prior_size%FASTWRITER_SYNCIO_ALIGN) {
  79. close(ctx->fd);
  80. ctx->fd = open(name, open_flags&~O_DIRECT, open_mode);
  81. if (ctx->fd < 0) return errno;
  82. ctx->prior_size = lseek(ctx->fd, 0, SEEK_END);
  83. ctx->sync_mode = 0;
  84. }
  85. # endif /* SYNC_MODE */
  86. }
  87. ctx->preallocated = 0;
  88. return 0;
  89. }
  90. void fastwriter_default_close(fastwriter_t *fw) {
  91. if (fw->ctx) {
  92. fastwriter_default_t *ctx = (fastwriter_default_t*)fw->ctx;
  93. if (ctx->fd >= 0) {
  94. #if defined(SYNC_MODE)||!defined(HAVE_LINUX_FALLOC_H)
  95. if (ctx->prior_size != (size_t)-1) {
  96. ftruncate(ctx->fd, ctx->prior_size + fw->written);
  97. }
  98. #endif
  99. close(ctx->fd);
  100. }
  101. free(ctx);
  102. fw->ctx = NULL;
  103. }
  104. }
  105. int fastwriter_default_write(fastwriter_t *fw, fastwriter_write_flags_t flags, size_t size, void *data, size_t *written) {
  106. size_t sum = 0;
  107. size_t delta = 0;
  108. ssize_t res;
  109. fastwriter_default_t *ctx = (fastwriter_default_t*)fw->ctx;
  110. if ((flags&FASTWRITER_WRITE_FLAG_FORCE)==0) {
  111. if (size < ctx->wr_block) {
  112. *written = 0;
  113. return 0;
  114. }
  115. size -= size % ctx->wr_block;
  116. }
  117. if ((ctx->pa_block)&&((fw->written + size) > ctx->preallocated)) {
  118. #ifdef HAVE_LINUX_FALLOC_H
  119. if (fallocate(ctx->fd, FALLOC_FL_KEEP_SIZE, ctx->preallocated, ctx->pa_block)) {
  120. #else /* HAVE_LINUX_FALLOC_H */
  121. if (posix_fallocate(ctx->fd, ctx->preallocated, ctx->pa_block)) {
  122. #endif /* HAVE_LINUX_FALLOC_H */
  123. ctx->pa_block = 0;
  124. } else {
  125. ctx->preallocated += ctx->pa_block;
  126. }
  127. }
  128. #ifdef SYNC_MODE
  129. // we expect this to happen only at last iteration (buffer is multiply of the required align)
  130. if ((ctx->sync_mode)&&(size%FASTWRITER_SYNCIO_ALIGN)) {
  131. delta = FASTWRITER_SYNCIO_ALIGN - size%FASTWRITER_SYNCIO_ALIGN;
  132. }
  133. #endif /* SYNC_MODE */
  134. do {
  135. res = write(ctx->fd, data + sum, size + delta - sum);
  136. // printf("%i %i %p %zu %i\n", res, ctx->fd, data, size, delta);
  137. if (res < 0) {
  138. *written = sum;
  139. return errno;
  140. }
  141. sum += res;
  142. } while (sum < size);
  143. #ifdef SYNC_MODE
  144. posix_fadvise(ctx->fd, fw->written, size, POSIX_FADV_DONTNEED);
  145. #endif /* SYNC_MODE */
  146. *written = size;
  147. return 0;
  148. }