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.

229 lines
6.0 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_base_chmod.hpp"
  18. #include "fs_base_fchmod.hpp"
  19. #include "fs_base_open.hpp"
  20. #include "fs_base_stat.hpp"
  21. #include "fs_cow.hpp"
  22. #include "fs_path.hpp"
  23. #include "policy_cache.hpp"
  24. #include "stat_util.hpp"
  25. #include "ugid.hpp"
  26. #include "fuse.h"
  27. #include <string>
  28. #include <vector>
  29. using std::string;
  30. using std::vector;
  31. namespace l
  32. {
  33. static
  34. bool
  35. rdonly(const int flags_)
  36. {
  37. return ((flags_ & O_ACCMODE) == O_RDONLY);
  38. }
  39. static
  40. int
  41. chmod_and_open_if_not_writable_and_empty(const string &fullpath_,
  42. const int flags_)
  43. {
  44. int rv;
  45. struct stat st;
  46. rv = fs::lstat(fullpath_,&st);
  47. if(rv == -1)
  48. return (errno=EACCES,-1);
  49. if(StatUtil::writable(st))
  50. return (errno=EACCES,-1);
  51. rv = fs::chmod(fullpath_,(st.st_mode|S_IWUSR|S_IWGRP));
  52. if(rv == -1)
  53. return (errno=EACCES,-1);
  54. rv = fs::open(fullpath_,flags_);
  55. if(rv == -1)
  56. return (errno=EACCES,-1);
  57. fs::fchmod(rv,st.st_mode);
  58. return rv;
  59. }
  60. static
  61. int
  62. nfsopenhack(const std::string &fullpath_,
  63. const int flags_,
  64. const NFSOpenHack nfsopenhack_)
  65. {
  66. switch(nfsopenhack_)
  67. {
  68. default:
  69. case NFSOpenHack::ENUM::OFF:
  70. return (errno=EACCES,-1);
  71. case NFSOpenHack::ENUM::GIT:
  72. if(l::rdonly(flags_))
  73. return (errno=EACCES,-1);
  74. if(fullpath_.find("/.git/") == string::npos)
  75. return (errno=EACCES,-1);
  76. return l::chmod_and_open_if_not_writable_and_empty(fullpath_,flags_);
  77. case NFSOpenHack::ENUM::ALL:
  78. if(l::rdonly(flags_))
  79. return (errno=EACCES,-1);
  80. return l::chmod_and_open_if_not_writable_and_empty(fullpath_,flags_);
  81. }
  82. }
  83. /*
  84. The kernel expects being able to issue read requests when running
  85. with writeback caching enabled so we must change O_WRONLY to
  86. O_RDWR.
  87. With writeback caching enabled the kernel handles O_APPEND. Could
  88. be an issue if the underlying file changes out of band but that is
  89. true of any caching.
  90. */
  91. static
  92. void
  93. tweak_flags_writeback_cache(int *flags_)
  94. {
  95. if((*flags_ & O_ACCMODE) == O_WRONLY)
  96. *flags_ = ((*flags_ & ~O_ACCMODE) | O_RDWR);
  97. if(*flags_ & O_APPEND)
  98. *flags_ &= ~O_APPEND;
  99. }
  100. static
  101. void
  102. config_to_ffi_flags(const Config &config_,
  103. fuse_file_info *ffi_)
  104. {
  105. switch(config_.cache_files)
  106. {
  107. case CacheFiles::ENUM::LIBFUSE:
  108. ffi_->direct_io = config_.direct_io;
  109. ffi_->keep_cache = config_.kernel_cache;
  110. ffi_->auto_cache = config_.auto_cache;
  111. break;
  112. case CacheFiles::ENUM::OFF:
  113. ffi_->direct_io = 1;
  114. ffi_->keep_cache = 0;
  115. ffi_->auto_cache = 0;
  116. break;
  117. case CacheFiles::ENUM::PARTIAL:
  118. ffi_->direct_io = 0;
  119. ffi_->keep_cache = 0;
  120. ffi_->auto_cache = 0;
  121. break;
  122. case CacheFiles::ENUM::FULL:
  123. ffi_->direct_io = 0;
  124. ffi_->keep_cache = 1;
  125. ffi_->auto_cache = 0;
  126. break;
  127. case CacheFiles::ENUM::AUTO_FULL:
  128. ffi_->direct_io = 0;
  129. ffi_->keep_cache = 0;
  130. ffi_->auto_cache = 1;
  131. break;
  132. }
  133. }
  134. static
  135. int
  136. open_core(const string &basepath_,
  137. const char *fusepath_,
  138. const int flags_,
  139. const bool link_cow_,
  140. const NFSOpenHack nfsopenhack_,
  141. uint64_t *fh_)
  142. {
  143. int fd;
  144. string fullpath;
  145. fullpath = fs::path::make(basepath_,fusepath_);
  146. if(link_cow_ && fs::cow::is_eligible(fullpath.c_str(),flags_))
  147. fs::cow::break_link(fullpath.c_str());
  148. fd = fs::open(fullpath,flags_);
  149. if((fd == -1) && (errno == EACCES))
  150. fd = l::nfsopenhack(fullpath,flags_,nfsopenhack_);
  151. if(fd == -1)
  152. return -errno;
  153. *fh_ = reinterpret_cast<uint64_t>(new FileInfo(fd,fusepath_));
  154. return 0;
  155. }
  156. static
  157. int
  158. open(Policy::Func::Search searchFunc_,
  159. PolicyCache &cache,
  160. const Branches &branches_,
  161. const uint64_t minfreespace_,
  162. const char *fusepath_,
  163. const int flags_,
  164. const bool link_cow_,
  165. const NFSOpenHack nfsopenhack_,
  166. uint64_t *fh_)
  167. {
  168. int rv;
  169. string basepath;
  170. rv = cache(searchFunc_,branches_,fusepath_,minfreespace_,&basepath);
  171. if(rv == -1)
  172. return -errno;
  173. return l::open_core(basepath,fusepath_,flags_,link_cow_,nfsopenhack_,fh_);
  174. }
  175. }
  176. namespace FUSE
  177. {
  178. int
  179. open(const char *fusepath_,
  180. fuse_file_info *ffi_)
  181. {
  182. const fuse_context *fc = fuse_get_context();
  183. const Config &config = Config::ro();
  184. const ugid::Set ugid(fc->uid,fc->gid);
  185. l::config_to_ffi_flags(config,ffi_);
  186. if(config.writeback_cache)
  187. l::tweak_flags_writeback_cache(&ffi_->flags);
  188. return l::open(config.func.open.policy,
  189. config.open_cache,
  190. config.branches,
  191. config.minfreespace,
  192. fusepath_,
  193. ffi_->flags,
  194. config.link_cow,
  195. config.nfsopenhack,
  196. &ffi_->fh);
  197. }
  198. }