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.

215 lines
5.5 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "config.hpp"
  15. #include "errno.hpp"
  16. #include "fileinfo.hpp"
  17. #include "fs_close.hpp"
  18. #include "fs_dup2.hpp"
  19. #include "fs_movefile.hpp"
  20. #include "fs_pwrite.hpp"
  21. #include "fs_pwriten.hpp"
  22. #include "ugid.hpp"
  23. #include "fuse.h"
  24. #include <string>
  25. #include <vector>
  26. using std::string;
  27. using std::vector;
  28. typedef int (*WriteFunc)(const int,const void*,const size_t,const off_t);
  29. namespace l
  30. {
  31. static
  32. bool
  33. out_of_space(const ssize_t error_)
  34. {
  35. return ((error_ == -ENOSPC) ||
  36. (error_ == -EDQUOT));
  37. }
  38. static
  39. int
  40. move_and_pwrite(const char *buf_,
  41. const size_t count_,
  42. const off_t offset_,
  43. FileInfo *fi_,
  44. int err_)
  45. {
  46. int err;
  47. ssize_t rv;
  48. Config::Read cfg;
  49. if(cfg->moveonenospc.enabled == false)
  50. return err_;
  51. rv = fs::movefile_as_root(cfg->moveonenospc.policy,
  52. cfg->branches,
  53. fi_->fusepath,
  54. fi_->fd);
  55. if(rv < 0)
  56. return err_;
  57. err = fs::dup2(rv,fi_->fd);
  58. fs::close(rv);
  59. if(err < 0)
  60. return err;
  61. return fs::pwrite(fi_->fd,buf_,count_,offset_);
  62. }
  63. static
  64. int
  65. move_and_pwriten(char const *buf_,
  66. size_t const count_,
  67. off_t const offset_,
  68. FileInfo *fi_,
  69. ssize_t const err_,
  70. ssize_t const written_)
  71. {
  72. int err;
  73. ssize_t rv;
  74. Config::Read cfg;
  75. if(cfg->moveonenospc.enabled == false)
  76. return err_;
  77. rv = fs::movefile_as_root(cfg->moveonenospc.policy,
  78. cfg->branches,
  79. fi_->fusepath,
  80. fi_->fd);
  81. if(rv < 0)
  82. return err_;
  83. err = fs::dup2(rv,fi_->fd);
  84. fs::close(rv);
  85. if(err < 0)
  86. return err;
  87. rv = fs::pwriten(fi_->fd,
  88. buf_ + written_,
  89. count_ - written_,
  90. offset_ + written_,
  91. &err);
  92. if(err < 0)
  93. return err;
  94. return (rv + written_);
  95. }
  96. // When in direct_io mode write's return value should match that of
  97. // the operation.
  98. // 0 on EOF
  99. // N bytes written (short writes included)
  100. // -errno on error
  101. // See libfuse/include/fuse.h for more details
  102. static
  103. int
  104. write_direct_io(const char *buf_,
  105. const size_t count_,
  106. const off_t offset_,
  107. FileInfo *fi_)
  108. {
  109. ssize_t rv;
  110. rv = fs::pwrite(fi_->fd,buf_,count_,offset_);
  111. if(l::out_of_space(rv))
  112. rv = l::move_and_pwrite(buf_,count_,offset_,fi_,rv);
  113. return rv;
  114. }
  115. // When not in direct_io mode write's return value is more complex.
  116. // 0 or less than `count` on EOF
  117. // `count` on non-errors
  118. // -errno on error
  119. // See libfuse/include/fuse.h for more details
  120. static
  121. int
  122. write_cached(const char *buf_,
  123. const size_t count_,
  124. const off_t offset_,
  125. FileInfo *fi_)
  126. {
  127. int err;
  128. ssize_t rv;
  129. rv = fs::pwriten(fi_->fd,buf_,count_,offset_,&err);
  130. if(rv == (ssize_t)count_)
  131. return count_;
  132. if(rv == 0)
  133. return 0;
  134. if(err && !l::out_of_space(err))
  135. return err;
  136. rv = l::move_and_pwriten(buf_,count_,offset_,fi_,err,rv);
  137. return rv;
  138. }
  139. static
  140. int
  141. write(const fuse_file_info_t *ffi_,
  142. const char *buf_,
  143. const size_t count_,
  144. const off_t offset_)
  145. {
  146. FileInfo *fi;
  147. fi = reinterpret_cast<FileInfo*>(ffi_->fh);
  148. // Concurrent writes can only happen if:
  149. // 1) writeback-cache is enabled and using page caching
  150. // 2) parallel_direct_writes is enabled and file has `direct_io=true`
  151. // Will look into selectively locking in the future
  152. // A reader/writer lock would probably be the best option given
  153. // the expense of the write itself in comparison. Alternatively,
  154. // could change the move file behavior to use a known target file
  155. // and have threads use O_EXCL and back off and wait for the
  156. // transfer to complete before retrying.
  157. std::lock_guard<std::mutex> guard(fi->mutex);
  158. if(fi->direct_io)
  159. return l::write_direct_io(buf_,count_,offset_,fi);
  160. return l::write_cached(buf_,count_,offset_,fi);
  161. }
  162. }
  163. namespace FUSE
  164. {
  165. int
  166. write(const fuse_file_info_t *ffi_,
  167. const char *buf_,
  168. size_t count_,
  169. off_t offset_)
  170. {
  171. return l::write(ffi_,buf_,count_,offset_);
  172. }
  173. int
  174. write_null(const fuse_file_info_t *ffi_,
  175. const char *buf_,
  176. size_t count_,
  177. off_t offset_)
  178. {
  179. return count_;
  180. }
  181. }