You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
4.3 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2022, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "fuse_msgbuf.hpp"
  16. #include "fuse.h"
  17. #include "fuse_kernel.h"
  18. #include <unistd.h>
  19. #include <atomic>
  20. #include <cstdint>
  21. #include <cstdlib>
  22. #include <mutex>
  23. #include <vector>
  24. static std::uint32_t g_PAGESIZE = 0;
  25. static std::uint32_t g_BUFSIZE = 0;
  26. static std::atomic<std::uint_fast64_t> g_MSGBUF_ALLOC_COUNT;
  27. static std::mutex g_MUTEX;
  28. static std::vector<fuse_msgbuf_t*> g_MSGBUF_STACK;
  29. uint64_t
  30. msgbuf_get_bufsize()
  31. {
  32. return g_BUFSIZE;
  33. }
  34. void
  35. msgbuf_set_bufsize(const uint32_t size_in_pages_)
  36. {
  37. g_BUFSIZE = ((size_in_pages_ + 1) * g_PAGESIZE);
  38. }
  39. void
  40. msgbuf_page_align(fuse_msgbuf_t *msgbuf_)
  41. {
  42. msgbuf_->mem = (char*)msgbuf_;
  43. msgbuf_->mem += g_PAGESIZE;
  44. msgbuf_->size = (g_BUFSIZE - g_PAGESIZE);
  45. }
  46. void
  47. msgbuf_write_align(fuse_msgbuf_t *msgbuf_)
  48. {
  49. msgbuf_->mem = (char*)msgbuf_;
  50. msgbuf_->mem += g_PAGESIZE;
  51. msgbuf_->mem -= sizeof(struct fuse_in_header);
  52. msgbuf_->mem -= sizeof(struct fuse_write_in);
  53. msgbuf_->size = (g_BUFSIZE - g_PAGESIZE);
  54. }
  55. static
  56. __attribute__((constructor))
  57. void
  58. msgbuf_constructor()
  59. {
  60. g_PAGESIZE = sysconf(_SC_PAGESIZE);
  61. // FUSE_MAX_MAX_PAGES for payload + 1 for message header
  62. msgbuf_set_bufsize(FUSE_MAX_MAX_PAGES + 1);
  63. }
  64. static
  65. __attribute__((destructor))
  66. void
  67. msgbuf_destroy()
  68. {
  69. }
  70. static
  71. void*
  72. page_aligned_malloc(const uint64_t size_)
  73. {
  74. int rv;
  75. void *buf = NULL;
  76. rv = posix_memalign(&buf,g_PAGESIZE,size_);
  77. if(rv != 0)
  78. return NULL;
  79. return buf;
  80. }
  81. typedef void (*msgbuf_setup_func_t)(fuse_msgbuf_t*);
  82. static
  83. fuse_msgbuf_t*
  84. _msgbuf_alloc(msgbuf_setup_func_t setup_func_)
  85. {
  86. fuse_msgbuf_t *msgbuf;
  87. g_MUTEX.lock();
  88. if(g_MSGBUF_STACK.empty())
  89. {
  90. g_MUTEX.unlock();
  91. msgbuf = (fuse_msgbuf_t*)page_aligned_malloc(g_BUFSIZE);
  92. if(msgbuf == NULL)
  93. return NULL;
  94. g_MSGBUF_ALLOC_COUNT++;
  95. }
  96. else
  97. {
  98. msgbuf = g_MSGBUF_STACK.back();
  99. g_MSGBUF_STACK.pop_back();
  100. g_MUTEX.unlock();
  101. }
  102. setup_func_(msgbuf);
  103. return msgbuf;
  104. }
  105. // Offset the memory so write request payload will be placed on page
  106. // boundry so O_DIRECT can work. No impact on other message types
  107. // except for `read` which will require using `msgbuf_page_align`.
  108. fuse_msgbuf_t*
  109. msgbuf_alloc()
  110. {
  111. return _msgbuf_alloc(msgbuf_write_align);
  112. }
  113. fuse_msgbuf_t*
  114. msgbuf_alloc_page_aligned()
  115. {
  116. return _msgbuf_alloc(msgbuf_page_align);
  117. }
  118. static
  119. void
  120. msgbuf_destroy(fuse_msgbuf_t *msgbuf_)
  121. {
  122. // free(msgbuf_->mem);
  123. free(msgbuf_);
  124. }
  125. void
  126. msgbuf_free(fuse_msgbuf_t *msgbuf_)
  127. {
  128. std::lock_guard<std::mutex> lck(g_MUTEX);
  129. if(msgbuf_->size != (g_BUFSIZE - g_PAGESIZE))
  130. {
  131. msgbuf_destroy(msgbuf_);
  132. g_MSGBUF_ALLOC_COUNT--;
  133. return;
  134. }
  135. g_MSGBUF_STACK.emplace_back(msgbuf_);
  136. }
  137. uint64_t
  138. msgbuf_alloc_count()
  139. {
  140. return g_MSGBUF_ALLOC_COUNT;
  141. }
  142. uint64_t
  143. msgbuf_avail_count()
  144. {
  145. std::lock_guard<std::mutex> lck(g_MUTEX);
  146. return g_MSGBUF_STACK.size();
  147. }
  148. void
  149. msgbuf_gc_10percent()
  150. {
  151. std::vector<fuse_msgbuf_t*> togc;
  152. {
  153. std::size_t size;
  154. std::size_t ten_percent;
  155. std::lock_guard<std::mutex> lck(g_MUTEX);
  156. size = g_MSGBUF_STACK.size();
  157. ten_percent = (size / 10);
  158. for(std::size_t i = 0; i < ten_percent; i++)
  159. {
  160. togc.push_back(g_MSGBUF_STACK.back());
  161. g_MSGBUF_STACK.pop_back();
  162. }
  163. }
  164. for(auto msgbuf : togc)
  165. {
  166. msgbuf_destroy(msgbuf);
  167. g_MSGBUF_ALLOC_COUNT--;
  168. }
  169. }
  170. void
  171. msgbuf_gc()
  172. {
  173. std::vector<fuse_msgbuf_t*> oldstack;
  174. {
  175. std::lock_guard<std::mutex> lck(g_MUTEX);
  176. oldstack.swap(g_MSGBUF_STACK);
  177. }
  178. for(auto msgbuf: oldstack)
  179. {
  180. msgbuf_destroy(msgbuf);
  181. g_MSGBUF_ALLOC_COUNT--;
  182. }
  183. }