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.

459 lines
11 KiB

  1. /*
  2. Copyright (c) 2018, 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 "fs_base_getxattr.hpp"
  17. #include "fs_path.hpp"
  18. #include "rwlock.hpp"
  19. #include "str.hpp"
  20. #include "ugid.hpp"
  21. #include "version.hpp"
  22. #include <fuse.h>
  23. #include <algorithm>
  24. #include <set>
  25. #include <sstream>
  26. #include <string>
  27. #include <vector>
  28. #include <stdio.h>
  29. #include <string.h>
  30. static const char SECURITY_CAPABILITY[] = "security.capability";
  31. using std::string;
  32. using std::vector;
  33. using std::set;
  34. using namespace mergerfs;
  35. static
  36. bool
  37. _is_attrname_security_capability(const char *attrname_)
  38. {
  39. return (strcmp(attrname_,SECURITY_CAPABILITY) == 0);
  40. }
  41. static
  42. int
  43. _lgetxattr(const string &path,
  44. const char *attrname,
  45. void *value,
  46. const size_t size)
  47. {
  48. int rv;
  49. rv = fs::lgetxattr(path,attrname,value,size);
  50. return ((rv == -1) ? -errno : rv);
  51. }
  52. static
  53. void
  54. _getxattr_controlfile_fusefunc_policy(const Config &config,
  55. const string &attr,
  56. string &attrvalue)
  57. {
  58. FuseFunc fusefunc;
  59. fusefunc = FuseFunc::find(attr);
  60. if(fusefunc != FuseFunc::invalid)
  61. attrvalue = (std::string)*config.policies[(FuseFunc::Enum::Type)*fusefunc];
  62. }
  63. static
  64. void
  65. _getxattr_controlfile_category_policy(const Config &config,
  66. const string &attr,
  67. string &attrvalue)
  68. {
  69. Category cat;
  70. cat = Category::find(attr);
  71. if(cat != Category::invalid)
  72. {
  73. vector<string> policies;
  74. for(int i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++)
  75. {
  76. if(cat == (Category::Enum::Type)*FuseFunc::fusefuncs[i])
  77. policies.push_back(*config.policies[i]);
  78. }
  79. std::sort(policies.begin(),policies.end());
  80. policies.erase(std::unique(policies.begin(),policies.end()),
  81. policies.end());
  82. attrvalue = str::join(policies,',');
  83. }
  84. }
  85. static
  86. void
  87. _getxattr_controlfile_srcmounts(const Config &config,
  88. string &attrvalue)
  89. {
  90. attrvalue = config.branches.to_string();
  91. }
  92. static
  93. void
  94. _getxattr_controlfile_branches(const Config &config,
  95. string &attrvalue)
  96. {
  97. attrvalue = config.branches.to_string(true);
  98. }
  99. static
  100. void
  101. _getxattr_controlfile_uint64_t(const uint64_t uint,
  102. string &attrvalue)
  103. {
  104. std::ostringstream os;
  105. os << uint;
  106. attrvalue = os.str();
  107. }
  108. static
  109. void
  110. _getxattr_controlfile_time_t(const time_t time,
  111. string &attrvalue)
  112. {
  113. std::ostringstream os;
  114. os << time;
  115. attrvalue = os.str();
  116. }
  117. static
  118. void
  119. _getxattr_controlfile_bool(const bool boolvalue,
  120. string &attrvalue)
  121. {
  122. attrvalue = (boolvalue ? "true" : "false");
  123. }
  124. static
  125. void
  126. _getxattr_controlfile_errno(const int errno_,
  127. string &attrvalue)
  128. {
  129. switch(errno_)
  130. {
  131. case 0:
  132. attrvalue = "passthrough";
  133. break;
  134. case ENOATTR:
  135. attrvalue = "noattr";
  136. break;
  137. case ENOSYS:
  138. attrvalue = "nosys";
  139. break;
  140. default:
  141. attrvalue = "ERROR";
  142. break;
  143. }
  144. }
  145. static
  146. void
  147. _getxattr_controlfile_statfs(const Config::StatFS::Enum enum_,
  148. string &attrvalue_)
  149. {
  150. switch(enum_)
  151. {
  152. case Config::StatFS::BASE:
  153. attrvalue_ = "base";
  154. break;
  155. case Config::StatFS::FULL:
  156. attrvalue_ = "full";
  157. break;
  158. default:
  159. attrvalue_ = "ERROR";
  160. break;
  161. }
  162. }
  163. static
  164. void
  165. _getxattr_controlfile_statfsignore(const Config::StatFSIgnore::Enum enum_,
  166. string &attrvalue_)
  167. {
  168. switch(enum_)
  169. {
  170. case Config::StatFSIgnore::NONE:
  171. attrvalue_ = "none";
  172. break;
  173. case Config::StatFSIgnore::RO:
  174. attrvalue_ = "ro";
  175. break;
  176. case Config::StatFSIgnore::NC:
  177. attrvalue_ = "nc";
  178. break;
  179. default:
  180. attrvalue_ = "ERROR";
  181. break;
  182. }
  183. }
  184. static
  185. void
  186. _getxattr_controlfile_policies(const Config &config,
  187. string &attrvalue)
  188. {
  189. size_t i = Policy::Enum::begin();
  190. attrvalue = (string)Policy::policies[i];
  191. for(i++; i < Policy::Enum::end(); i++)
  192. attrvalue += ',' + (string)Policy::policies[i];
  193. }
  194. static
  195. void
  196. _getxattr_controlfile_version(string &attrvalue)
  197. {
  198. attrvalue = MERGERFS_VERSION;
  199. if(attrvalue.empty())
  200. attrvalue = "unknown_possible_problem_with_build";
  201. }
  202. static
  203. void
  204. _getxattr_pid(string &attrvalue)
  205. {
  206. int pid;
  207. char buf[32];
  208. pid = getpid();
  209. snprintf(buf,sizeof(buf),"%d",pid);
  210. attrvalue = buf;
  211. }
  212. static
  213. int
  214. _getxattr_controlfile(const Config &config,
  215. const char *attrname,
  216. char *buf,
  217. const size_t count)
  218. {
  219. size_t len;
  220. string attrvalue;
  221. vector<string> attr;
  222. str::split(attr,attrname,'.');
  223. if((attr[0] != "user") || (attr[1] != "mergerfs"))
  224. return -ENOATTR;
  225. switch(attr.size())
  226. {
  227. case 3:
  228. if(attr[2] == "srcmounts")
  229. _getxattr_controlfile_srcmounts(config,attrvalue);
  230. else if(attr[2] == "branches")
  231. _getxattr_controlfile_branches(config,attrvalue);
  232. else if(attr[2] == "minfreespace")
  233. _getxattr_controlfile_uint64_t(config.minfreespace,attrvalue);
  234. else if(attr[2] == "moveonenospc")
  235. _getxattr_controlfile_bool(config.moveonenospc,attrvalue);
  236. else if(attr[2] == "dropcacheonclose")
  237. _getxattr_controlfile_bool(config.dropcacheonclose,attrvalue);
  238. else if(attr[2] == "symlinkify")
  239. _getxattr_controlfile_bool(config.symlinkify,attrvalue);
  240. else if(attr[2] == "symlinkify_timeout")
  241. _getxattr_controlfile_time_t(config.symlinkify_timeout,attrvalue);
  242. else if(attr[2] == "nullrw")
  243. _getxattr_controlfile_bool(config.nullrw,attrvalue);
  244. else if(attr[2] == "ignorepponrename")
  245. _getxattr_controlfile_bool(config.ignorepponrename,attrvalue);
  246. else if(attr[2] == "security_capability")
  247. _getxattr_controlfile_bool(config.security_capability,attrvalue);
  248. else if(attr[2] == "xattr")
  249. _getxattr_controlfile_errno(config.xattr,attrvalue);
  250. else if(attr[2] == "link_cow")
  251. _getxattr_controlfile_bool(config.link_cow,attrvalue);
  252. else if(attr[2] == "statfs")
  253. _getxattr_controlfile_statfs(config.statfs,attrvalue);
  254. else if(attr[2] == "statfs_ignore")
  255. _getxattr_controlfile_statfsignore(config.statfs_ignore,attrvalue);
  256. else if(attr[2] == "policies")
  257. _getxattr_controlfile_policies(config,attrvalue);
  258. else if(attr[2] == "version")
  259. _getxattr_controlfile_version(attrvalue);
  260. else if(attr[2] == "pid")
  261. _getxattr_pid(attrvalue);
  262. else if(attr[2] == "direct_io")
  263. _getxattr_controlfile_bool(config.direct_io,attrvalue);
  264. break;
  265. case 4:
  266. if(attr[2] == "category")
  267. _getxattr_controlfile_category_policy(config,attr[3],attrvalue);
  268. else if(attr[2] == "func")
  269. _getxattr_controlfile_fusefunc_policy(config,attr[3],attrvalue);
  270. break;
  271. }
  272. if(attrvalue.empty())
  273. return -ENOATTR;
  274. len = attrvalue.size();
  275. if(count == 0)
  276. return len;
  277. if(count < len)
  278. return -ERANGE;
  279. memcpy(buf,attrvalue.c_str(),len);
  280. return (int)len;
  281. }
  282. static
  283. int
  284. _getxattr_from_string(char *destbuf,
  285. const size_t destbufsize,
  286. const string &src)
  287. {
  288. const size_t srcbufsize = src.size();
  289. if(destbufsize == 0)
  290. return srcbufsize;
  291. if(srcbufsize > destbufsize)
  292. return -ERANGE;
  293. memcpy(destbuf,src.data(),srcbufsize);
  294. return srcbufsize;
  295. }
  296. static
  297. int
  298. _getxattr_user_mergerfs_allpaths(const Branches &branches_,
  299. const char *fusepath,
  300. char *buf,
  301. const size_t count)
  302. {
  303. string concated;
  304. vector<string> paths;
  305. vector<string> branches;
  306. branches_.to_paths(branches);
  307. fs::findallfiles(branches,fusepath,paths);
  308. concated = str::join(paths,'\0');
  309. return _getxattr_from_string(buf,count,concated);
  310. }
  311. static
  312. int
  313. _getxattr_user_mergerfs(const string &basepath,
  314. const char *fusepath,
  315. const string &fullpath,
  316. const Branches &branches_,
  317. const char *attrname,
  318. char *buf,
  319. const size_t count)
  320. {
  321. vector<string> attr;
  322. str::split(attr,attrname,'.');
  323. if(attr[2] == "basepath")
  324. return _getxattr_from_string(buf,count,basepath);
  325. else if(attr[2] == "relpath")
  326. return _getxattr_from_string(buf,count,fusepath);
  327. else if(attr[2] == "fullpath")
  328. return _getxattr_from_string(buf,count,fullpath);
  329. else if(attr[2] == "allpaths")
  330. return _getxattr_user_mergerfs_allpaths(branches_,fusepath,buf,count);
  331. return -ENOATTR;
  332. }
  333. static
  334. int
  335. _getxattr(Policy::Func::Search searchFunc,
  336. const Branches &branches_,
  337. const size_t minfreespace,
  338. const char *fusepath,
  339. const char *attrname,
  340. char *buf,
  341. const size_t count)
  342. {
  343. int rv;
  344. string fullpath;
  345. vector<const string*> basepaths;
  346. rv = searchFunc(branches_,fusepath,minfreespace,basepaths);
  347. if(rv == -1)
  348. return -errno;
  349. fs::path::make(basepaths[0],fusepath,fullpath);
  350. if(str::isprefix(attrname,"user.mergerfs."))
  351. rv = _getxattr_user_mergerfs(*basepaths[0],fusepath,fullpath,branches_,attrname,buf,count);
  352. else
  353. rv = _lgetxattr(fullpath,attrname,buf,count);
  354. return rv;
  355. }
  356. namespace mergerfs
  357. {
  358. namespace fuse
  359. {
  360. int
  361. getxattr(const char *fusepath,
  362. const char *attrname,
  363. char *buf,
  364. size_t count)
  365. {
  366. const fuse_context *fc = fuse_get_context();
  367. const Config &config = Config::get(fc);
  368. if(fusepath == config.controlfile)
  369. return _getxattr_controlfile(config,
  370. attrname,
  371. buf,
  372. count);
  373. if((config.security_capability == false) &&
  374. _is_attrname_security_capability(attrname))
  375. return -ENOATTR;
  376. if(config.xattr)
  377. return -config.xattr;
  378. const ugid::Set ugid(fc->uid,fc->gid);
  379. const rwlock::ReadGuard readlock(&config.branches_lock);
  380. return _getxattr(config.getxattr,
  381. config.branches,
  382. config.minfreespace,
  383. fusepath,
  384. attrname,
  385. buf,
  386. count);
  387. }
  388. }
  389. }