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.

252 lines
6.4 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_acl.hpp"
  18. #include "fs_clonepath.hpp"
  19. #include "fs_open.hpp"
  20. #include "fs_path.hpp"
  21. #include "procfs_get_name.hpp"
  22. #include "ugid.hpp"
  23. #include "fuse.h"
  24. #include <string>
  25. #include <vector>
  26. namespace l
  27. {
  28. /*
  29. The kernel expects being able to issue read requests when running
  30. with writeback caching enabled so we must change O_WRONLY to
  31. O_RDWR.
  32. With writeback caching enabled the kernel handles O_APPEND. Could
  33. be an issue if the underlying file changes out of band but that is
  34. true of any caching.
  35. */
  36. static
  37. void
  38. tweak_flags_writeback_cache(int *flags_)
  39. {
  40. if((*flags_ & O_ACCMODE) == O_WRONLY)
  41. *flags_ = ((*flags_ & ~O_ACCMODE) | O_RDWR);
  42. if(*flags_ & O_APPEND)
  43. *flags_ &= ~O_APPEND;
  44. }
  45. static
  46. bool
  47. rdonly(const int flags_)
  48. {
  49. return ((flags_ & O_ACCMODE) == O_RDONLY);
  50. }
  51. static
  52. bool
  53. calculate_flush(FlushOnClose const flushonclose_,
  54. int const flags_)
  55. {
  56. switch(flushonclose_)
  57. {
  58. case FlushOnCloseEnum::NEVER:
  59. return false;
  60. case FlushOnCloseEnum::OPENED_FOR_WRITE:
  61. return !l::rdonly(flags_);
  62. case FlushOnCloseEnum::ALWAYS:
  63. return true;
  64. }
  65. return true;
  66. }
  67. static
  68. void
  69. config_to_ffi_flags(Config::Read &cfg_,
  70. const int tid_,
  71. fuse_file_info_t *ffi_)
  72. {
  73. switch(cfg_->cache_files)
  74. {
  75. case CacheFiles::ENUM::LIBFUSE:
  76. ffi_->direct_io = cfg_->direct_io;
  77. ffi_->keep_cache = cfg_->kernel_cache;
  78. ffi_->auto_cache = cfg_->auto_cache;
  79. break;
  80. case CacheFiles::ENUM::OFF:
  81. ffi_->direct_io = 1;
  82. ffi_->keep_cache = 0;
  83. ffi_->auto_cache = 0;
  84. break;
  85. case CacheFiles::ENUM::PARTIAL:
  86. ffi_->direct_io = 0;
  87. ffi_->keep_cache = 0;
  88. ffi_->auto_cache = 0;
  89. break;
  90. case CacheFiles::ENUM::FULL:
  91. ffi_->direct_io = 0;
  92. ffi_->keep_cache = 1;
  93. ffi_->auto_cache = 0;
  94. break;
  95. case CacheFiles::ENUM::AUTO_FULL:
  96. ffi_->direct_io = 0;
  97. ffi_->keep_cache = 0;
  98. ffi_->auto_cache = 1;
  99. break;
  100. case CacheFiles::ENUM::PER_PROCESS:
  101. std::string proc_name;
  102. proc_name = procfs::get_name(tid_);
  103. if(cfg_->cache_files_process_names.count(proc_name) == 0)
  104. {
  105. ffi_->direct_io = 1;
  106. ffi_->keep_cache = 0;
  107. ffi_->auto_cache = 0;
  108. }
  109. else
  110. {
  111. ffi_->direct_io = 0;
  112. ffi_->keep_cache = 0;
  113. ffi_->auto_cache = 0;
  114. }
  115. break;
  116. }
  117. if(cfg_->parallel_direct_writes == true)
  118. ffi_->parallel_direct_writes = ffi_->direct_io;
  119. }
  120. static
  121. int
  122. create_core(const std::string &fullpath_,
  123. mode_t mode_,
  124. const mode_t umask_,
  125. const int flags_)
  126. {
  127. if(!fs::acl::dir_has_defaults(fullpath_))
  128. mode_ &= ~umask_;
  129. return fs::open(fullpath_,flags_,mode_);
  130. }
  131. static
  132. int
  133. create_core(const std::string &createpath_,
  134. const char *fusepath_,
  135. fuse_file_info_t *ffi_,
  136. const mode_t mode_,
  137. const mode_t umask_)
  138. {
  139. int rv;
  140. FileInfo *fi;
  141. std::string fullpath;
  142. fullpath = fs::path::make(createpath_,fusepath_);
  143. rv = l::create_core(fullpath,mode_,umask_,ffi_->flags);
  144. if(rv == -1)
  145. return -errno;
  146. fi = new FileInfo(rv,fusepath_,ffi_->direct_io);
  147. ffi_->fh = reinterpret_cast<uint64_t>(fi);
  148. return 0;
  149. }
  150. static
  151. int
  152. create(const Policy::Search &searchFunc_,
  153. const Policy::Create &createFunc_,
  154. const Branches &branches_,
  155. const char *fusepath_,
  156. fuse_file_info_t *ffi_,
  157. const mode_t mode_,
  158. const mode_t umask_)
  159. {
  160. int rv;
  161. std::string fullpath;
  162. std::string fusedirpath;
  163. StrVec createpaths;
  164. StrVec existingpaths;
  165. fusedirpath = fs::path::dirname(fusepath_);
  166. rv = searchFunc_(branches_,fusedirpath,&existingpaths);
  167. if(rv == -1)
  168. return -errno;
  169. rv = createFunc_(branches_,fusedirpath,&createpaths);
  170. if(rv == -1)
  171. return -errno;
  172. rv = fs::clonepath_as_root(existingpaths[0],createpaths[0],fusedirpath);
  173. if(rv == -1)
  174. return -errno;
  175. return l::create_core(createpaths[0],
  176. fusepath_,
  177. ffi_,
  178. mode_,
  179. umask_);
  180. }
  181. }
  182. namespace FUSE
  183. {
  184. int
  185. create(const char *fusepath_,
  186. mode_t mode_,
  187. fuse_file_info_t *ffi_)
  188. {
  189. int rv;
  190. Config::Read cfg;
  191. const fuse_context *fc = fuse_get_context();
  192. const ugid::Set ugid(fc->uid,fc->gid);
  193. l::config_to_ffi_flags(cfg,fc->pid,ffi_);
  194. if(cfg->writeback_cache)
  195. l::tweak_flags_writeback_cache(&ffi_->flags);
  196. ffi_->noflush = !l::calculate_flush(cfg->flushonclose,
  197. ffi_->flags);
  198. rv = l::create(cfg->func.getattr.policy,
  199. cfg->func.create.policy,
  200. cfg->branches,
  201. fusepath_,
  202. ffi_,
  203. mode_,
  204. fc->umask);
  205. if(rv == -EROFS)
  206. {
  207. Config::Write()->branches.find_and_set_mode_ro();
  208. rv = l::create(cfg->func.getattr.policy,
  209. cfg->func.create.policy,
  210. cfg->branches,
  211. fusepath_,
  212. ffi_,
  213. mode_,
  214. fc->umask);
  215. }
  216. return rv;
  217. }
  218. }