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.

458 lines
12 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 "fs_glob.hpp"
  17. #include "num.hpp"
  18. #include "policy.hpp"
  19. #include "str.hpp"
  20. #include "version.hpp"
  21. #include <fuse.h>
  22. #include <iomanip>
  23. #include <iostream>
  24. #include <sstream>
  25. #include <string>
  26. #include <vector>
  27. #include <stddef.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. using std::string;
  33. using std::vector;
  34. using namespace mergerfs;
  35. enum
  36. {
  37. MERGERFS_OPT_HELP,
  38. MERGERFS_OPT_VERSION
  39. };
  40. static
  41. void
  42. set_option(fuse_args &args,
  43. const std::string &option_)
  44. {
  45. string option;
  46. option = "-o" + option_;
  47. fuse_opt_insert_arg(&args,1,option.c_str());
  48. }
  49. static
  50. void
  51. set_kv_option(fuse_args &args,
  52. const std::string &key,
  53. const std::string &value)
  54. {
  55. std::string option;
  56. option = key + '=' + value;
  57. set_option(args,option);
  58. }
  59. static
  60. void
  61. set_fsname(fuse_args &args,
  62. const Branches &branches_)
  63. {
  64. vector<string> branches;
  65. branches_.to_paths(branches);
  66. if(branches.size() > 0)
  67. {
  68. std::string fsname;
  69. fsname = str::remove_common_prefix_and_join(branches,':');
  70. set_kv_option(args,"fsname",fsname);
  71. }
  72. }
  73. static
  74. void
  75. set_subtype(fuse_args &args)
  76. {
  77. set_kv_option(args,"subtype","mergerfs");
  78. }
  79. static
  80. void
  81. set_default_options(fuse_args &args)
  82. {
  83. set_option(args,"atomic_o_trunc");
  84. set_option(args,"auto_cache");
  85. set_option(args,"big_writes");
  86. set_option(args,"default_permissions");
  87. set_option(args,"splice_move");
  88. set_option(args,"splice_read");
  89. set_option(args,"splice_write");
  90. }
  91. static
  92. int
  93. parse_and_process(const std::string &value,
  94. uint64_t &minfreespace)
  95. {
  96. int rv;
  97. rv = num::to_uint64_t(value,minfreespace);
  98. if(rv == -1)
  99. return 1;
  100. return 0;
  101. }
  102. static
  103. int
  104. parse_and_process(const std::string &value,
  105. time_t &time)
  106. {
  107. int rv;
  108. rv = num::to_time_t(value,time);
  109. if(rv == -1)
  110. return 1;
  111. return 0;
  112. }
  113. static
  114. int
  115. parse_and_process(const std::string &value,
  116. bool &boolean)
  117. {
  118. if(value == "false")
  119. boolean = false;
  120. else if(value == "true")
  121. boolean = true;
  122. else
  123. return 1;
  124. return 0;
  125. }
  126. static
  127. int
  128. parse_and_process_errno(const std::string &value_,
  129. int &errno_)
  130. {
  131. if(value_ == "passthrough")
  132. errno_ = 0;
  133. else if(value_ == "nosys")
  134. errno_ = ENOSYS;
  135. else if(value_ == "noattr")
  136. errno_ = ENOATTR;
  137. else
  138. return 1;
  139. return 0;
  140. }
  141. static
  142. int
  143. parse_and_process_statfs(const std::string &value_,
  144. Config::StatFS::Enum &enum_)
  145. {
  146. if(value_ == "base")
  147. enum_ = Config::StatFS::BASE;
  148. else if(value_ == "full")
  149. enum_ = Config::StatFS::FULL;
  150. else
  151. return 1;
  152. return 0;
  153. }
  154. static
  155. int
  156. parse_and_process_statfsignore(const std::string &value_,
  157. Config::StatFSIgnore::Enum &enum_)
  158. {
  159. if(value_ == "none")
  160. enum_ = Config::StatFSIgnore::NONE;
  161. else if(value_ == "ro")
  162. enum_ = Config::StatFSIgnore::RO;
  163. else if(value_ == "nc")
  164. enum_ = Config::StatFSIgnore::NC;
  165. else
  166. return 1;
  167. return 0;
  168. }
  169. static
  170. int
  171. parse_and_process_arg(Config &config,
  172. const std::string &arg,
  173. fuse_args *outargs)
  174. {
  175. if(arg == "defaults")
  176. return (set_default_options(*outargs),0);
  177. else if(arg == "direct_io")
  178. return (config.direct_io=true,0);
  179. return 1;
  180. }
  181. static
  182. int
  183. parse_and_process_kv_arg(Config &config,
  184. const std::string &key,
  185. const std::string &value)
  186. {
  187. int rv;
  188. std::vector<std::string> keypart;
  189. rv = -1;
  190. str::split(keypart,key,'.');
  191. if(keypart.size() == 2)
  192. {
  193. if(keypart[0] == "func")
  194. rv = config.set_func_policy(keypart[1],value);
  195. else if(keypart[0] == "category")
  196. rv = config.set_category_policy(keypart[1],value);
  197. }
  198. else
  199. {
  200. if(key == "minfreespace")
  201. rv = parse_and_process(value,config.minfreespace);
  202. else if(key == "moveonenospc")
  203. rv = parse_and_process(value,config.moveonenospc);
  204. else if(key == "dropcacheonclose")
  205. rv = parse_and_process(value,config.dropcacheonclose);
  206. else if(key == "symlinkify")
  207. rv = parse_and_process(value,config.symlinkify);
  208. else if(key == "symlinkify_timeout")
  209. rv = parse_and_process(value,config.symlinkify_timeout);
  210. else if(key == "nullrw")
  211. rv = parse_and_process(value,config.nullrw);
  212. else if(key == "ignorepponrename")
  213. rv = parse_and_process(value,config.ignorepponrename);
  214. else if(key == "security_capability")
  215. rv = parse_and_process(value,config.security_capability);
  216. else if(key == "link_cow")
  217. rv = parse_and_process(value,config.link_cow);
  218. else if(key == "xattr")
  219. rv = parse_and_process_errno(value,config.xattr);
  220. else if(key == "statfs")
  221. rv = parse_and_process_statfs(value,config.statfs);
  222. else if(key == "statfs_ignore")
  223. rv = parse_and_process_statfsignore(value,config.statfs_ignore);
  224. }
  225. if(rv == -1)
  226. rv = 1;
  227. return rv;
  228. }
  229. static
  230. int
  231. process_opt(Config &config,
  232. const std::string &arg,
  233. fuse_args *outargs)
  234. {
  235. int rv;
  236. std::vector<std::string> argvalue;
  237. str::split(argvalue,arg,'=');
  238. switch(argvalue.size())
  239. {
  240. case 1:
  241. rv = parse_and_process_arg(config,argvalue[0],outargs);
  242. break;
  243. case 2:
  244. rv = parse_and_process_kv_arg(config,argvalue[0],argvalue[1]);
  245. break;
  246. default:
  247. rv = 1;
  248. break;
  249. };
  250. return rv;
  251. }
  252. static
  253. int
  254. process_branches(const char *arg,
  255. Config &config)
  256. {
  257. config.branches.set(arg);
  258. return 0;
  259. }
  260. static
  261. int
  262. process_destmounts(const char *arg,
  263. Config &config)
  264. {
  265. config.destmount = arg;
  266. return 1;
  267. }
  268. static
  269. void
  270. usage(void)
  271. {
  272. std::cout <<
  273. "Usage: mergerfs [options] <srcpaths> <destpath>\n"
  274. "\n"
  275. " -o [opt,...] mount options\n"
  276. " -h --help print help\n"
  277. " -v --version print version\n"
  278. "\n"
  279. "mergerfs options:\n"
  280. " <srcpaths> ':' delimited list of directories. Supports\n"
  281. " shell globbing (must be escaped in shell)\n"
  282. " -o defaults Default FUSE options which seem to provide the\n"
  283. " best performance: atomic_o_trunc, auto_cache,\n"
  284. " big_writes, default_permissions, splice_read,\n"
  285. " splice_write, splice_move\n"
  286. " -o func.<f>=<p> Set function <f> to policy <p>\n"
  287. " -o category.<c>=<p> Set functions in category <c> to <p>\n"
  288. " -o direct_io Bypass page caching, may increase write\n"
  289. " speeds at the cost of reads. Please read docs\n"
  290. " for more details as there are tradeoffs.\n"
  291. " -o use_ino Have mergerfs generate inode values rather than\n"
  292. " autogenerated by libfuse. Suggested.\n"
  293. " -o minfreespace=<int> minimum free space needed for certain policies.\n"
  294. " default = 4G\n"
  295. " -o moveonenospc=<bool> Try to move file to another drive when ENOSPC\n"
  296. " on write. default = false\n"
  297. " -o dropcacheonclose=<bool>\n"
  298. " When a file is closed suggest to OS it drop\n"
  299. " the file's cache. This is useful when direct_io\n"
  300. " is disabled. default = false\n"
  301. " -o symlinkify=<bool> Read-only files, after a timeout, will be turned\n"
  302. " into symlinks. Read docs for limitations and\n"
  303. " possible issues. default = false\n"
  304. " -o symlinkify_timeout=<int>\n"
  305. " timeout in seconds before will turn to symlinks.\n"
  306. " default = 3600\n"
  307. " -o nullrw=<bool> Disables reads and writes. For benchmarking.\n"
  308. " default = false\n"
  309. " -o ignorepponrename=<bool>\n"
  310. " Ignore path preserving when performing renames\n"
  311. " and links. default = false\n"
  312. " -o link_cow=<bool> delink/clone file on open to simulate CoW.\n"
  313. " default = false\n"
  314. " -o security_capability=<bool>\n"
  315. " When disabled return ENOATTR when the xattr\n"
  316. " security.capability is queried. default = true\n"
  317. " -o xattr=passthrough|noattr|nosys\n"
  318. " Runtime control of xattrs. By default xattr\n"
  319. " requests will pass through to the underlying\n"
  320. " filesystems. notattr will short circuit as if\n"
  321. " nothing exists. nosys will respond as if not\n"
  322. " supported or disabled. default = passthrough\n"
  323. " -o statfs=base|full When set to 'base' statfs will use all branches\n"
  324. " when performing statfs calculations. 'full' will\n"
  325. " only include branches on which that path is\n"
  326. " available. default = base\n"
  327. " -o statfs_ignore=none|ro|nc\n"
  328. " 'ro' will cause statfs calculations to ignore\n"
  329. " available space for branches mounted or tagged\n"
  330. " as 'read only' or 'no create'. 'nc' will ignore\n"
  331. " available space for branches tagged as\n"
  332. " 'no create'. default = none\n"
  333. << std::endl;
  334. }
  335. static
  336. int
  337. option_processor(void *data,
  338. const char *arg,
  339. int key,
  340. fuse_args *outargs)
  341. {
  342. int rv = 0;
  343. Config &config = *(Config*)data;
  344. switch(key)
  345. {
  346. case FUSE_OPT_KEY_OPT:
  347. rv = process_opt(config,arg,outargs);
  348. break;
  349. case FUSE_OPT_KEY_NONOPT:
  350. rv = config.branches.empty() ?
  351. process_branches(arg,config) :
  352. process_destmounts(arg,config);
  353. break;
  354. case MERGERFS_OPT_HELP:
  355. usage();
  356. close(2);
  357. dup(1);
  358. fuse_opt_add_arg(outargs,"-ho");
  359. break;
  360. case MERGERFS_OPT_VERSION:
  361. std::cout << "mergerfs version: "
  362. << (MERGERFS_VERSION[0] ? MERGERFS_VERSION : "unknown")
  363. << std::endl;
  364. fuse_opt_add_arg(outargs,"--version");
  365. break;
  366. default:
  367. break;
  368. }
  369. return rv;
  370. }
  371. namespace mergerfs
  372. {
  373. namespace options
  374. {
  375. void
  376. parse(fuse_args &args,
  377. Config &config)
  378. {
  379. const struct fuse_opt opts[] =
  380. {
  381. FUSE_OPT_KEY("-h",MERGERFS_OPT_HELP),
  382. FUSE_OPT_KEY("--help",MERGERFS_OPT_HELP),
  383. FUSE_OPT_KEY("-v",MERGERFS_OPT_VERSION),
  384. FUSE_OPT_KEY("-V",MERGERFS_OPT_VERSION),
  385. FUSE_OPT_KEY("--version",MERGERFS_OPT_VERSION),
  386. {NULL,-1U,0}
  387. };
  388. fuse_opt_parse(&args,
  389. &config,
  390. opts,
  391. ::option_processor);
  392. set_fsname(args,config.branches);
  393. set_subtype(args);
  394. }
  395. }
  396. }