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.

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