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.

474 lines
16 KiB

4 years 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. #define FMT_HEADER_ONLY
  15. #include "config.hpp"
  16. #include "ef.hpp"
  17. #include "errno.hpp"
  18. #include "fmt/core.h"
  19. #include "fs_glob.hpp"
  20. #include "fs_statvfs_cache.hpp"
  21. #include "hw_cpu.hpp"
  22. #include "num.hpp"
  23. #include "policy.hpp"
  24. #include "str.hpp"
  25. #include "syslog.hpp"
  26. #include "version.hpp"
  27. #include "fuse.h"
  28. #include "fuse_config.hpp"
  29. #include <fstream>
  30. #include <iomanip>
  31. #include <iostream>
  32. #include <string>
  33. #include <vector>
  34. #include <stddef.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. using std::string;
  40. using std::vector;
  41. enum
  42. {
  43. MERGERFS_OPT_HELP,
  44. MERGERFS_OPT_VERSION
  45. };
  46. static
  47. void
  48. set_option(const std::string &option_,
  49. fuse_args *args_)
  50. {
  51. fuse_opt_add_arg(args_,"-o");
  52. fuse_opt_add_arg(args_,option_.c_str());
  53. }
  54. static
  55. void
  56. set_kv_option(const std::string &key_,
  57. const std::string &val_,
  58. fuse_args *args_)
  59. {
  60. std::string option;
  61. option = key_ + '=' + val_;
  62. set_option(option,args_);
  63. }
  64. static
  65. void
  66. set_fuse_threads(Config::Write &cfg_)
  67. {
  68. fuse_config_set_read_thread_count(cfg_->fuse_read_thread_count);
  69. fuse_config_set_process_thread_count(cfg_->fuse_process_thread_count);
  70. fuse_config_set_process_thread_queue_depth(cfg_->fuse_process_thread_queue_depth);
  71. fuse_config_set_pin_threads(cfg_->fuse_pin_threads);
  72. }
  73. static
  74. void
  75. set_fsname(Config::Write &cfg_,
  76. fuse_args *args_)
  77. {
  78. if(cfg_->fsname->empty())
  79. {
  80. vector<string> paths;
  81. cfg_->branches->to_paths(paths);
  82. if(paths.size() > 0)
  83. cfg_->fsname = str::remove_common_prefix_and_join(paths,':');
  84. }
  85. set_kv_option("fsname",cfg_->fsname,args_);
  86. }
  87. static
  88. void
  89. set_subtype(fuse_args *args_)
  90. {
  91. set_kv_option("subtype","mergerfs",args_);
  92. }
  93. static
  94. void
  95. set_default_options(fuse_args *args_)
  96. {
  97. set_option("default_permissions",args_);
  98. if(geteuid() == 0)
  99. set_option("allow_other",args_);
  100. else
  101. syslog_notice("not auto setting allow_other since not running as root");
  102. }
  103. static
  104. bool
  105. should_ignore(const std::string &key_)
  106. {
  107. static const std::set<std::string> IGNORED_KEYS =
  108. {
  109. "atomic_o_trunc",
  110. "big_writes",
  111. "cache.open",
  112. "defaults",
  113. "hard_remove",
  114. "no_splice_move",
  115. "no_splice_read",
  116. "no_splice_write",
  117. "nonempty",
  118. "splice_move",
  119. "splice_read",
  120. "splice_write",
  121. "use_ino",
  122. };
  123. return (IGNORED_KEYS.find(key_) != IGNORED_KEYS.end());
  124. }
  125. static
  126. int
  127. parse_and_process_kv_arg(Config::Write &cfg_,
  128. Config::ErrVec *errs_,
  129. const std::string &key_,
  130. const std::string &val_)
  131. {
  132. int rv;
  133. std::string key(key_);
  134. std::string val(val_);
  135. rv = 0;
  136. if(key == "config")
  137. return ((cfg_->from_file(val_,errs_) < 0) ? 1 : 0);
  138. ef(key == "attr_timeout")
  139. key = "cache.attr";
  140. ef(key == "entry_timeout")
  141. key = "cache.entry";
  142. ef(key == "negative_entry")
  143. key = "cache.negative_entry";
  144. ef(key == "direct_io" && val.empty())
  145. val = "true";
  146. ef(key == "kernel_cache" && val.empty())
  147. val = "true";
  148. ef(key == "auto_cache" && val.empty())
  149. val = "true";
  150. ef(key == "async_read" && val.empty())
  151. val = "true";
  152. ef(key == "sync_read" && val.empty())
  153. {key = "async_read", val = "false";}
  154. ef(::should_ignore(key_))
  155. return 0;
  156. if(cfg_->has_key(key) == false)
  157. return 1;
  158. rv = cfg_->set_raw(key,val);
  159. if(rv)
  160. errs_->push_back({rv,key+'='+val});
  161. return 0;
  162. }
  163. static
  164. int
  165. process_opt(Config::Write &cfg_,
  166. Config::ErrVec *errs_,
  167. const std::string &arg_)
  168. {
  169. std::string key;
  170. std::string val;
  171. str::splitkv(arg_,'=',&key,&val);
  172. key = str::trim(key);
  173. val = str::trim(val);
  174. return parse_and_process_kv_arg(cfg_,errs_,key,val);
  175. }
  176. static
  177. int
  178. process_branches(Config::Write &cfg_,
  179. Config::ErrVec *errs_,
  180. const char *arg_)
  181. {
  182. int rv;
  183. string arg;
  184. arg = arg_;
  185. rv = cfg_->set_raw("branches",arg);
  186. if(rv)
  187. errs_->push_back({rv,"branches="+arg});
  188. return 0;
  189. }
  190. static
  191. int
  192. process_mount(Config::Write &cfg_,
  193. Config::ErrVec *errs_,
  194. const char *arg_)
  195. {
  196. int rv;
  197. string arg;
  198. arg = arg_;
  199. rv = cfg_->set_raw("mount",arg);
  200. if(rv)
  201. errs_->push_back({rv,"mount="+arg});
  202. return 1;
  203. }
  204. static
  205. void
  206. usage(void)
  207. {
  208. std::cout <<
  209. "Usage: mergerfs [options] <branches> <destpath>\n"
  210. "\n"
  211. " -o [opt,...] mount options\n"
  212. " -h --help print help\n"
  213. " -v --version print version\n"
  214. " -f run mergerfs in foreground\n"
  215. " -d, -o debug enable debug output (enables -f)\n"
  216. "\n"
  217. "mergerfs options:\n"
  218. " <branches> ':' delimited list of directories. Supports\n"
  219. " shell globbing (must be escaped in shell)\n"
  220. " -o config=FILE Read options from file in key=val format\n"
  221. " -o func.FUNC=POLICY Set function FUNC to policy POLICY\n"
  222. " -o category.CAT=POLICY Set functions in category CAT to POLICY\n"
  223. " -o fsname=STR Sets the name of the filesystem.\n"
  224. " -o read-thread-count=INT\n"
  225. " Number of threads used to read from FUSE (and process)\n"
  226. " * 0 = number of logical cores\n"
  227. " * negative value = number of logical cores / abs(value)\n"
  228. " * -1 in combination with process-thread-count=-1 will\n"
  229. " split thread count among read and process threads\n"
  230. " default=0\n"
  231. " -o process-thread-count=INT\n"
  232. " Same as read-thread-count but for FUSE message processing\n"
  233. " If not set then read threads will do both read and process\n"
  234. " -o cache.statfs=INT 'statfs' cache timeout in seconds. Used by\n"
  235. " policies. default = 0 (disabled)\n"
  236. " -o cache.files=libfuse|off|partial|full|auto-full|per-process\n"
  237. " * libfuse: Use direct_io, kernel_cache, auto_cache\n"
  238. " values directly\n"
  239. " * off: Disable page caching\n"
  240. " * partial: Clear page cache on file open\n"
  241. " * full: Keep cache on file open\n"
  242. " * auto-full: Keep cache if mtime & size not changed\n"
  243. " * per-process: Enable caching for only for processes\n"
  244. " which comm name match value in cache.files.process-names\n"
  245. " default = libfuse\n"
  246. " -o cache.files.process-names=STRLIST\n"
  247. " Pipe delimited list of process comm names.\n"
  248. " defulat=rtorrent|qbittorrent-nox\n"
  249. " -o cache.writeback=BOOL\n"
  250. " Enable kernel writeback caching (if supported)\n"
  251. " cache.files must be enabled as well.\n"
  252. " default = false\n"
  253. " -o cache.symlinks=BOOL\n"
  254. " Enable kernel caching of symlinks (if supported)\n"
  255. " default = false\n"
  256. " -o cache.readdir=BOOL\n"
  257. " Enable kernel caching readdir (if supported)\n"
  258. " default = false\n"
  259. " -o cache.attr=INT File attribute cache timeout in seconds.\n"
  260. " default = 1\n"
  261. " -o cache.entry=INT File name lookup cache timeout in seconds.\n"
  262. " default = 1\n"
  263. " -o cache.negative_entry=INT\n"
  264. " Negative file name lookup cache timeout in\n"
  265. " seconds. default = 0\n"
  266. " -o inodecalc=passthrough|path-hash|devino-hash|hybrid-hash\n"
  267. " Selects the inode calculation algorithm.\n"
  268. " default = hybrid-hash\n"
  269. " -o minfreespace=INT Minimum free space needed for certain policies.\n"
  270. " default = 4G\n"
  271. " -o moveonenospc=BOOL Try to move file to another drive when ENOSPC\n"
  272. " on write. default = false\n"
  273. " -o dropcacheonclose=BOOL\n"
  274. " When a file is closed suggest to OS it drop\n"
  275. " the file's cache. This is useful when using\n"
  276. " 'cache.files'. default = false\n"
  277. " -o symlinkify=BOOL Read-only files, after a timeout, will be turned\n"
  278. " into symlinks. Read docs for limitations and\n"
  279. " possible issues. default = false\n"
  280. " -o symlinkify_timeout=INT\n"
  281. " Timeout in seconds before files turn to symlinks.\n"
  282. " default = 3600\n"
  283. " -o nullrw=BOOL Disables reads and writes. For benchmarking.\n"
  284. " default = false\n"
  285. " -o ignorepponrename=BOOL\n"
  286. " Ignore path preserving when performing renames\n"
  287. " and links. default = false\n"
  288. " -o link_cow=BOOL Delink/clone file on open to simulate CoW.\n"
  289. " default = false\n"
  290. " -o nfsopenhack=off|git|all\n"
  291. " A workaround for exporting mergerfs over NFS\n"
  292. " where there are issues with creating files for\n"
  293. " write while setting the mode to read-only.\n"
  294. " default = off\n"
  295. " -o security_capability=BOOL\n"
  296. " When disabled return ENOATTR when the xattr\n"
  297. " security.capability is queried. default = true\n"
  298. " -o xattr=passthrough|noattr|nosys\n"
  299. " Runtime control of xattrs. By default xattr\n"
  300. " requests will pass through to the underlying\n"
  301. " filesystems. notattr will short circuit as if\n"
  302. " nothing exists. nosys will respond as if not\n"
  303. " supported or disabled. default = passthrough\n"
  304. " -o statfs=base|full When set to 'base' statfs will use all branches\n"
  305. " when performing statfs calculations. 'full' will\n"
  306. " only include branches on which that path is\n"
  307. " available. default = base\n"
  308. " -o statfs_ignore=none|ro|nc\n"
  309. " 'ro' will cause statfs calculations to ignore\n"
  310. " available space for branches mounted or tagged\n"
  311. " as 'read only' or 'no create'. 'nc' will ignore\n"
  312. " available space for branches tagged as\n"
  313. " 'no create'. default = none\n"
  314. " -o posix_acl=BOOL Enable POSIX ACL support. default = false\n"
  315. " -o async_read=BOOL If disabled or unavailable the kernel will\n"
  316. " ensure there is at most one pending read \n"
  317. " request per file and will attempt to order\n"
  318. " requests by offset. default = true\n"
  319. << std::endl;
  320. }
  321. static
  322. int
  323. option_processor(void *data_,
  324. const char *arg_,
  325. int key_,
  326. fuse_args *outargs_)
  327. {
  328. Config::Write cfg;
  329. Config::ErrVec *errs = (Config::ErrVec*)data_;
  330. switch(key_)
  331. {
  332. case FUSE_OPT_KEY_OPT:
  333. return process_opt(cfg,errs,arg_);
  334. case FUSE_OPT_KEY_NONOPT:
  335. if(cfg->branches->empty())
  336. return process_branches(cfg,errs,arg_);
  337. else
  338. return process_mount(cfg,errs,arg_);
  339. case MERGERFS_OPT_HELP:
  340. usage();
  341. exit(0);
  342. case MERGERFS_OPT_VERSION:
  343. fmt::print("mergerfs v{}\n\n"
  344. "https://github.com/trapexit/mergerfs\n"
  345. "https://github.com/trapexit/support\n\n"
  346. "ISC License (ISC)\n\n"
  347. "Copyright 2023, Antonio SJ Musumeci <trapexit@spawn.link>\n\n"
  348. "Permission to use, copy, modify, and/or distribute this software for\n"
  349. "any purpose with or without fee is hereby granted, provided that the\n"
  350. "above copyright notice and this permission notice appear in all\n"
  351. "copies.\n\n"
  352. "THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL\n"
  353. "WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED\n"
  354. "WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE\n"
  355. "AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL\n"
  356. "DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR\n"
  357. "PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER\n"
  358. "TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\n"
  359. "PERFORMANCE OF THIS SOFTWARE.\n\n"
  360. ,
  361. (MERGERFS_VERSION[0] ? MERGERFS_VERSION : "unknown"));
  362. exit(0);
  363. default:
  364. break;
  365. }
  366. return 0;
  367. }
  368. static
  369. void
  370. check_for_mount_loop(Config::Write &cfg_,
  371. Config::ErrVec *errs_)
  372. {
  373. fs::Path mount;
  374. fs::PathVector branches;
  375. std::error_code ec;
  376. mount = (std::string)cfg_->mountpoint;
  377. branches = cfg_->branches->to_paths();
  378. for(const auto &branch : branches)
  379. {
  380. if(ghc::filesystem::equivalent(branch,mount,ec))
  381. {
  382. std::string errstr;
  383. errstr = fmt::format("branches can not include the mountpoint: {}",branch.string());
  384. errs_->push_back({0,errstr});
  385. }
  386. }
  387. }
  388. namespace options
  389. {
  390. void
  391. parse(fuse_args *args_,
  392. Config::ErrVec *errs_)
  393. {
  394. Config::Write cfg;
  395. const struct fuse_opt opts[] =
  396. {
  397. FUSE_OPT_KEY("-h",MERGERFS_OPT_HELP),
  398. FUSE_OPT_KEY("--help",MERGERFS_OPT_HELP),
  399. FUSE_OPT_KEY("-v",MERGERFS_OPT_VERSION),
  400. FUSE_OPT_KEY("-V",MERGERFS_OPT_VERSION),
  401. FUSE_OPT_KEY("--version",MERGERFS_OPT_VERSION),
  402. {NULL,-1U,0}
  403. };
  404. fuse_opt_parse(args_,
  405. errs_,
  406. opts,
  407. ::option_processor);
  408. if(cfg->branches->empty())
  409. errs_->push_back({0,"branches not set"});
  410. if(cfg->mountpoint->empty())
  411. errs_->push_back({0,"mountpoint not set"});
  412. check_for_mount_loop(cfg,errs_);
  413. set_default_options(args_);
  414. set_fsname(cfg,args_);
  415. set_subtype(args_);
  416. set_fuse_threads(cfg);
  417. cfg->finish_initializing();
  418. }
  419. }