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.

204 lines
5.3 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 "fuse.h"
  23. namespace l
  24. {
  25. static
  26. bool
  27. out_of_space(const ssize_t error_)
  28. {
  29. return ((error_ == -ENOSPC) ||
  30. (error_ == -EDQUOT));
  31. }
  32. static
  33. int
  34. move_and_pwrite(const char *buf_,
  35. const size_t count_,
  36. const off_t offset_,
  37. FileInfo *fi_,
  38. int err_)
  39. {
  40. int err;
  41. ssize_t rv;
  42. Config::Read cfg;
  43. if(cfg->moveonenospc.enabled == false)
  44. return err_;
  45. rv = fs::movefile_as_root(cfg->moveonenospc.policy,
  46. cfg->branches,
  47. fi_->fusepath,
  48. fi_->fd);
  49. if(rv < 0)
  50. return err_;
  51. err = fs::dup2(rv,fi_->fd);
  52. fs::close(rv);
  53. if(err < 0)
  54. return err_;
  55. return fs::pwrite(fi_->fd,buf_,count_,offset_);
  56. }
  57. static
  58. int
  59. move_and_pwriten(char const *buf_,
  60. size_t const count_,
  61. off_t const offset_,
  62. FileInfo *fi_,
  63. ssize_t const err_,
  64. ssize_t const written_)
  65. {
  66. int err;
  67. ssize_t rv;
  68. Config::Read cfg;
  69. if(cfg->moveonenospc.enabled == false)
  70. return err_;
  71. rv = fs::movefile_as_root(cfg->moveonenospc.policy,
  72. cfg->branches,
  73. fi_->fusepath,
  74. fi_->fd);
  75. if(rv < 0)
  76. return err_;
  77. err = fs::dup2(rv,fi_->fd);
  78. fs::close(rv);
  79. if(err < 0)
  80. return err_;
  81. rv = fs::pwriten(fi_->fd,
  82. buf_ + written_,
  83. count_ - written_,
  84. offset_ + written_,
  85. &err);
  86. if(err < 0)
  87. return err;
  88. return (written_ + rv);
  89. }
  90. // When in direct_io mode write's return value should match that of
  91. // the operation.
  92. // 0 on EOF
  93. // N bytes written (short writes included)
  94. // -errno on error
  95. // See libfuse/include/fuse.h for more details
  96. static
  97. int
  98. write_direct_io(const char *buf_,
  99. const size_t count_,
  100. const off_t offset_,
  101. FileInfo *fi_)
  102. {
  103. ssize_t rv;
  104. rv = fs::pwrite(fi_->fd,buf_,count_,offset_);
  105. if(l::out_of_space(rv))
  106. rv = l::move_and_pwrite(buf_,count_,offset_,fi_,rv);
  107. return rv;
  108. }
  109. // When not in direct_io mode write's return value is more complex.
  110. // 0 or less than `count` on EOF
  111. // `count` on non-errors
  112. // -errno on error
  113. // See libfuse/include/fuse.h for more details
  114. static
  115. int
  116. write_cached(const char *buf_,
  117. const size_t count_,
  118. const off_t offset_,
  119. FileInfo *fi_)
  120. {
  121. int err;
  122. ssize_t rv;
  123. rv = fs::pwriten(fi_->fd,buf_,count_,offset_,&err);
  124. if(err == 0)
  125. return rv;
  126. if(err && !l::out_of_space(err))
  127. return err;
  128. rv = l::move_and_pwriten(buf_,count_,offset_,fi_,err,rv);
  129. return rv;
  130. }
  131. static
  132. int
  133. write(const fuse_file_info_t *ffi_,
  134. const char *buf_,
  135. const size_t count_,
  136. const off_t offset_)
  137. {
  138. FileInfo *fi;
  139. fi = reinterpret_cast<FileInfo*>(ffi_->fh);
  140. // Concurrent writes can only happen if:
  141. // 1) writeback-cache is enabled and using page caching
  142. // 2) parallel_direct_writes is enabled and file has `direct_io=true`
  143. // Will look into selectively locking in the future
  144. // A reader/writer lock would probably be the best option given
  145. // the expense of the write itself in comparison. Alternatively,
  146. // could change the move file behavior to use a known target file
  147. // and have threads use O_EXCL and back off and wait for the
  148. // transfer to complete before retrying.
  149. std::lock_guard<std::mutex> guard(fi->mutex);
  150. if(fi->direct_io)
  151. return l::write_direct_io(buf_,count_,offset_,fi);
  152. return l::write_cached(buf_,count_,offset_,fi);
  153. }
  154. }
  155. namespace FUSE
  156. {
  157. int
  158. write(const fuse_file_info_t *ffi_,
  159. const char *buf_,
  160. size_t count_,
  161. off_t offset_)
  162. {
  163. return l::write(ffi_,buf_,count_,offset_);
  164. }
  165. int
  166. write_null(const fuse_file_info_t *ffi_,
  167. const char *buf_,
  168. size_t count_,
  169. off_t offset_)
  170. {
  171. return count_;
  172. }
  173. }