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.

331 lines
8.3 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 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_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 <boost/unordered/concurrent_flat_map.hpp>
  28. #include <set>
  29. #include <string>
  30. #include <vector>
  31. static boost::unordered::concurrent_flat_map<std::string,int> foo;
  32. namespace l
  33. {
  34. static
  35. bool
  36. rdonly(const int flags_)
  37. {
  38. return ((flags_ & O_ACCMODE) == O_RDONLY);
  39. }
  40. static
  41. int
  42. lchmod_and_open_if_not_writable_and_empty(const std::string &fullpath_,
  43. const int flags_)
  44. {
  45. int rv;
  46. struct stat st;
  47. rv = fs::lstat(fullpath_,&st);
  48. if(rv == -1)
  49. return (errno=EACCES,-1);
  50. if(StatUtil::writable(st))
  51. return (errno=EACCES,-1);
  52. rv = fs::lchmod(fullpath_,(st.st_mode|S_IWUSR|S_IWGRP));
  53. if(rv == -1)
  54. return (errno=EACCES,-1);
  55. rv = fs::open(fullpath_,flags_);
  56. if(rv == -1)
  57. return (errno=EACCES,-1);
  58. fs::fchmod(rv,st.st_mode);
  59. return rv;
  60. }
  61. static
  62. int
  63. nfsopenhack(const std::string &fullpath_,
  64. const int flags_,
  65. const NFSOpenHack nfsopenhack_)
  66. {
  67. switch(nfsopenhack_)
  68. {
  69. default:
  70. case NFSOpenHack::ENUM::OFF:
  71. return (errno=EACCES,-1);
  72. case NFSOpenHack::ENUM::GIT:
  73. if(l::rdonly(flags_))
  74. return (errno=EACCES,-1);
  75. if(fullpath_.find("/.git/") == std::string::npos)
  76. return (errno=EACCES,-1);
  77. return l::lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_);
  78. case NFSOpenHack::ENUM::ALL:
  79. if(l::rdonly(flags_))
  80. return (errno=EACCES,-1);
  81. return l::lchmod_and_open_if_not_writable_and_empty(fullpath_,flags_);
  82. }
  83. }
  84. /*
  85. The kernel expects being able to issue read requests when running
  86. with writeback caching enabled so we must change O_WRONLY to
  87. O_RDWR.
  88. With writeback caching enabled the kernel handles O_APPEND. Could
  89. be an issue if the underlying file changes out of band but that is
  90. true of any caching.
  91. */
  92. static
  93. void
  94. tweak_flags_writeback_cache(int *flags_)
  95. {
  96. if((*flags_ & O_ACCMODE) == O_WRONLY)
  97. *flags_ = ((*flags_ & ~O_ACCMODE) | O_RDWR);
  98. if(*flags_ & O_APPEND)
  99. *flags_ &= ~O_APPEND;
  100. }
  101. static
  102. bool
  103. calculate_flush(FlushOnClose const flushonclose_,
  104. int const flags_)
  105. {
  106. switch(flushonclose_)
  107. {
  108. case FlushOnCloseEnum::NEVER:
  109. return false;
  110. case FlushOnCloseEnum::OPENED_FOR_WRITE:
  111. return !l::rdonly(flags_);
  112. case FlushOnCloseEnum::ALWAYS:
  113. return true;
  114. }
  115. return true;
  116. }
  117. static
  118. void
  119. config_to_ffi_flags(Config::Read &cfg_,
  120. const int tid_,
  121. fuse_file_info_t *ffi_)
  122. {
  123. switch(cfg_->cache_files)
  124. {
  125. case CacheFiles::ENUM::LIBFUSE:
  126. ffi_->direct_io = cfg_->direct_io;
  127. ffi_->keep_cache = cfg_->kernel_cache;
  128. ffi_->auto_cache = cfg_->auto_cache;
  129. break;
  130. case CacheFiles::ENUM::OFF:
  131. ffi_->direct_io = 1;
  132. ffi_->keep_cache = 0;
  133. ffi_->auto_cache = 0;
  134. break;
  135. case CacheFiles::ENUM::PARTIAL:
  136. ffi_->direct_io = 0;
  137. ffi_->keep_cache = 0;
  138. ffi_->auto_cache = 0;
  139. break;
  140. case CacheFiles::ENUM::FULL:
  141. ffi_->direct_io = 0;
  142. ffi_->keep_cache = 1;
  143. ffi_->auto_cache = 0;
  144. break;
  145. case CacheFiles::ENUM::AUTO_FULL:
  146. ffi_->direct_io = 0;
  147. ffi_->keep_cache = 0;
  148. ffi_->auto_cache = 1;
  149. break;
  150. case CacheFiles::ENUM::PER_PROCESS:
  151. std::string proc_name;
  152. proc_name = procfs::get_name(tid_);
  153. if(cfg_->cache_files_process_names.count(proc_name) == 0)
  154. {
  155. ffi_->direct_io = 1;
  156. ffi_->keep_cache = 0;
  157. ffi_->auto_cache = 0;
  158. }
  159. else
  160. {
  161. ffi_->direct_io = 0;
  162. ffi_->keep_cache = 0;
  163. ffi_->auto_cache = 0;
  164. }
  165. break;
  166. }
  167. if(cfg_->parallel_direct_writes == true)
  168. ffi_->parallel_direct_writes = ffi_->direct_io;
  169. }
  170. static
  171. int
  172. open_core(const std::string &basepath_,
  173. const char *fusepath_,
  174. fuse_file_info_t *ffi_,
  175. const bool link_cow_,
  176. const NFSOpenHack nfsopenhack_)
  177. {
  178. int fd;
  179. FileInfo *fi;
  180. std::string fullpath;
  181. fullpath = fs::path::make(basepath_,fusepath_);
  182. if(link_cow_ && fs::cow::is_eligible(fullpath.c_str(),ffi_->flags))
  183. fs::cow::break_link(fullpath.c_str());
  184. fd = fs::open(fullpath,ffi_->flags);
  185. if((fd == -1) && (errno == EACCES))
  186. fd = l::nfsopenhack(fullpath,ffi_->flags,nfsopenhack_);
  187. if(fd == -1)
  188. return -errno;
  189. fi = new FileInfo(fd,fusepath_,ffi_->direct_io);
  190. ffi_->fh = reinterpret_cast<uint64_t>(fi);
  191. return 0;
  192. }
  193. static
  194. int
  195. passthrough(const fuse_context *fc_,
  196. fuse_file_info_t *ffi_)
  197. {
  198. int backing_id;
  199. FileInfo *fi;
  200. const ugid::SetRootGuard ugid;
  201. fi = reinterpret_cast<FileInfo*>(ffi_->fh);
  202. backing_id = 0;
  203. foo.visit(fi->fusepath,
  204. [&](const std::pair<std::string,int> &e_)
  205. {
  206. backing_id = e_.second;
  207. });
  208. if(backing_id == 0)
  209. {
  210. backing_id = fuse_passthrough_open(fc_,fi->fd);
  211. if(backing_id <= 0)
  212. return 0;
  213. foo.insert({fi->fusepath,backing_id});
  214. }
  215. ffi_->passthrough = true;
  216. ffi_->keep_cache = true;
  217. fi->backing_id = backing_id;
  218. ffi_->backing_id = backing_id;
  219. return 0;
  220. }
  221. static
  222. int
  223. open(const Policy::Search &searchFunc_,
  224. const Branches &branches_,
  225. const char *fusepath_,
  226. fuse_file_info_t *ffi_,
  227. const bool link_cow_,
  228. const NFSOpenHack nfsopenhack_)
  229. {
  230. int rv;
  231. StrVec basepaths;
  232. rv = searchFunc_(branches_,fusepath_,&basepaths);
  233. if(rv == -1)
  234. return -errno;
  235. return l::open_core(basepaths[0],fusepath_,ffi_,link_cow_,nfsopenhack_);
  236. }
  237. }
  238. constexpr
  239. const
  240. uint64_t
  241. _(const PassthroughEnum e_,
  242. const uint64_t m_)
  243. {
  244. return ((((uint64_t)e_) << 32) | (m_ & O_ACCMODE));
  245. }
  246. namespace FUSE
  247. {
  248. int
  249. open(const char *fusepath_,
  250. fuse_file_info_t *ffi_)
  251. {
  252. int rv;
  253. Config::Read cfg;
  254. const fuse_context *fc = fuse_get_context();
  255. const ugid::Set ugid(fc->uid,fc->gid);
  256. l::config_to_ffi_flags(cfg,fc->pid,ffi_);
  257. if(cfg->writeback_cache)
  258. l::tweak_flags_writeback_cache(&ffi_->flags);
  259. ffi_->noflush = !l::calculate_flush(cfg->flushonclose,
  260. ffi_->flags);
  261. rv = l::open(cfg->func.open.policy,
  262. cfg->branches,
  263. fusepath_,
  264. ffi_,
  265. cfg->link_cow,
  266. cfg->nfsopenhack);
  267. if(rv != 0)
  268. return rv;
  269. switch(_(cfg->passthrough,ffi_->flags))
  270. {
  271. case _(PassthroughEnum::r, O_RDONLY):
  272. case _(PassthroughEnum::r, O_RDWR):
  273. case _(PassthroughEnum::w, O_WRONLY):
  274. case _(PassthroughEnum::w, O_RDWR):
  275. case _(PassthroughEnum::ro,O_RDONLY):
  276. case _(PassthroughEnum::wo,O_WRONLY):
  277. case _(PassthroughEnum::rw,O_RDONLY):
  278. case _(PassthroughEnum::rw,O_WRONLY):
  279. case _(PassthroughEnum::rw,O_RDWR):
  280. return l::passthrough(fc,ffi_);
  281. }
  282. return 0;
  283. }
  284. }