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.

208 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_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. void
  47. config_to_ffi_flags(Config::Read &cfg_,
  48. const int tid_,
  49. fuse_file_info_t *ffi_)
  50. {
  51. switch(cfg_->cache_files)
  52. {
  53. case CacheFiles::ENUM::LIBFUSE:
  54. ffi_->direct_io = cfg_->direct_io;
  55. ffi_->keep_cache = cfg_->kernel_cache;
  56. ffi_->auto_cache = cfg_->auto_cache;
  57. break;
  58. case CacheFiles::ENUM::OFF:
  59. ffi_->direct_io = 1;
  60. ffi_->keep_cache = 0;
  61. ffi_->auto_cache = 0;
  62. break;
  63. case CacheFiles::ENUM::PARTIAL:
  64. ffi_->direct_io = 0;
  65. ffi_->keep_cache = 0;
  66. ffi_->auto_cache = 0;
  67. break;
  68. case CacheFiles::ENUM::FULL:
  69. ffi_->direct_io = 0;
  70. ffi_->keep_cache = 1;
  71. ffi_->auto_cache = 0;
  72. break;
  73. case CacheFiles::ENUM::AUTO_FULL:
  74. ffi_->direct_io = 0;
  75. ffi_->keep_cache = 0;
  76. ffi_->auto_cache = 1;
  77. break;
  78. case CacheFiles::ENUM::PER_PROCESS:
  79. std::string proc_name;
  80. proc_name = procfs::get_name(tid_);
  81. if(cfg_->cache_files_process_names.count(proc_name) == 0)
  82. {
  83. ffi_->direct_io = 1;
  84. ffi_->keep_cache = 0;
  85. ffi_->auto_cache = 0;
  86. }
  87. else
  88. {
  89. ffi_->direct_io = 0;
  90. ffi_->keep_cache = 0;
  91. ffi_->auto_cache = 0;
  92. }
  93. break;
  94. }
  95. }
  96. static
  97. int
  98. create_core(const std::string &fullpath_,
  99. mode_t mode_,
  100. const mode_t umask_,
  101. const int flags_)
  102. {
  103. if(!fs::acl::dir_has_defaults(fullpath_))
  104. mode_ &= ~umask_;
  105. return fs::open(fullpath_,flags_,mode_);
  106. }
  107. static
  108. int
  109. create_core(const std::string &createpath_,
  110. const char *fusepath_,
  111. const mode_t mode_,
  112. const mode_t umask_,
  113. const int flags_,
  114. uint64_t *fh_)
  115. {
  116. int rv;
  117. std::string fullpath;
  118. fullpath = fs::path::make(createpath_,fusepath_);
  119. rv = l::create_core(fullpath,mode_,umask_,flags_);
  120. if(rv == -1)
  121. return -errno;
  122. *fh_ = reinterpret_cast<uint64_t>(new FileInfo(rv,fusepath_));
  123. return 0;
  124. }
  125. static
  126. int
  127. create(const Policy::Search &searchFunc_,
  128. const Policy::Create &createFunc_,
  129. const Branches &branches_,
  130. const char *fusepath_,
  131. const mode_t mode_,
  132. const mode_t umask_,
  133. const int flags_,
  134. uint64_t *fh_)
  135. {
  136. int rv;
  137. std::string fullpath;
  138. std::string fusedirpath;
  139. StrVec createpaths;
  140. StrVec existingpaths;
  141. fusedirpath = fs::path::dirname(fusepath_);
  142. rv = searchFunc_(branches_,fusedirpath,&existingpaths);
  143. if(rv == -1)
  144. return -errno;
  145. rv = createFunc_(branches_,fusedirpath,&createpaths);
  146. if(rv == -1)
  147. return -errno;
  148. rv = fs::clonepath_as_root(existingpaths[0],createpaths[0],fusedirpath);
  149. if(rv == -1)
  150. return -errno;
  151. return l::create_core(createpaths[0],
  152. fusepath_,
  153. mode_,
  154. umask_,
  155. flags_,
  156. fh_);
  157. }
  158. }
  159. namespace FUSE
  160. {
  161. int
  162. create(const char *fusepath_,
  163. mode_t mode_,
  164. fuse_file_info_t *ffi_)
  165. {
  166. Config::Read cfg;
  167. const fuse_context *fc = fuse_get_context();
  168. const ugid::Set ugid(fc->uid,fc->gid);
  169. l::config_to_ffi_flags(cfg,fc->pid,ffi_);
  170. if(cfg->writeback_cache)
  171. l::tweak_flags_writeback_cache(&ffi_->flags);
  172. return l::create(cfg->func.getattr.policy,
  173. cfg->func.create.policy,
  174. cfg->branches,
  175. fusepath_,
  176. mode_,
  177. fc->umask,
  178. ffi_->flags,
  179. &ffi_->fh);
  180. }
  181. }