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.

278 lines
7.0 KiB

7 months ago
  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. passthrough(const fuse_context *fc_,
  153. fuse_file_info_t *ffi_)
  154. {
  155. int backing_id;
  156. FileInfo *fi;
  157. const ugid::SetRootGuard ugid;
  158. fi = reinterpret_cast<FileInfo*>(ffi_->fh);
  159. backing_id = fuse_passthrough_open(fc_,fi->fd);
  160. if(backing_id <= 0)
  161. return 0;
  162. ffi_->passthrough = true;
  163. ffi_->keep_cache = false;
  164. fi->backing_id = backing_id;
  165. ffi_->backing_id = backing_id;
  166. return 0;
  167. }
  168. static
  169. int
  170. create(const Policy::Search &searchFunc_,
  171. const Policy::Create &createFunc_,
  172. const Branches &branches_,
  173. const char *fusepath_,
  174. fuse_file_info_t *ffi_,
  175. const mode_t mode_,
  176. const mode_t umask_)
  177. {
  178. int rv;
  179. std::string fullpath;
  180. std::string fusedirpath;
  181. StrVec createpaths;
  182. StrVec existingpaths;
  183. fusedirpath = fs::path::dirname(fusepath_);
  184. rv = searchFunc_(branches_,fusedirpath,&existingpaths);
  185. if(rv == -1)
  186. return -errno;
  187. rv = createFunc_(branches_,fusedirpath,&createpaths);
  188. if(rv == -1)
  189. return -errno;
  190. rv = fs::clonepath_as_root(existingpaths[0],createpaths[0],fusedirpath);
  191. if(rv == -1)
  192. return -errno;
  193. return l::create_core(createpaths[0],
  194. fusepath_,
  195. ffi_,
  196. mode_,
  197. umask_);
  198. }
  199. }
  200. namespace FUSE
  201. {
  202. int
  203. create(const char *fusepath_,
  204. mode_t mode_,
  205. fuse_file_info_t *ffi_)
  206. {
  207. int rv;
  208. Config::Read cfg;
  209. const fuse_context *fc = fuse_get_context();
  210. const ugid::Set ugid(fc->uid,fc->gid);
  211. l::config_to_ffi_flags(cfg,fc->pid,ffi_);
  212. if(cfg->writeback_cache)
  213. l::tweak_flags_writeback_cache(&ffi_->flags);
  214. ffi_->noflush = !l::calculate_flush(cfg->flushonclose,
  215. ffi_->flags);
  216. rv = l::create(cfg->func.getattr.policy,
  217. cfg->func.create.policy,
  218. cfg->branches,
  219. fusepath_,
  220. ffi_,
  221. mode_,
  222. fc->umask);
  223. if(rv == -EROFS)
  224. {
  225. Config::Write()->branches.find_and_set_mode_ro();
  226. rv = l::create(cfg->func.getattr.policy,
  227. cfg->func.create.policy,
  228. cfg->branches,
  229. fusepath_,
  230. ffi_,
  231. mode_,
  232. fc->umask);
  233. }
  234. if((rv != 0) || (cfg->passthrough == false))
  235. return rv;
  236. return l::passthrough(fc,ffi_);
  237. }
  238. }