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.

332 lines
8.4 KiB

10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
9 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
9 months ago
9 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 "state.hpp"
  15. #include "toml.hpp"
  16. #include "fs_readahead.hpp"
  17. #include "fs_wait_for_mount.hpp"
  18. #include "syslog.hpp"
  19. #include "fs_path.hpp"
  20. #include "fs_umount2.hpp"
  21. #include "mergerfs.hpp"
  22. #include "option_parser.hpp"
  23. #include "procfs_get_name.hpp"
  24. #include "resources.hpp"
  25. #include "strvec.hpp"
  26. #include "gidcache.hpp"
  27. #include "fuse_access.hpp"
  28. #include "fuse_bmap.hpp"
  29. #include "fuse_chmod.hpp"
  30. #include "fuse_chown.hpp"
  31. #include "fuse_copy_file_range.hpp"
  32. #include "fuse_create.hpp"
  33. #include "fuse_destroy.hpp"
  34. #include "fuse_fallocate.hpp"
  35. #include "fuse_fchmod.hpp"
  36. #include "fuse_fchown.hpp"
  37. #include "fuse_fgetattr.hpp"
  38. #include "fuse_flock.hpp"
  39. #include "fuse_flush.hpp"
  40. #include "fuse_free_hide.hpp"
  41. #include "fuse_fsync.hpp"
  42. #include "fuse_fsyncdir.hpp"
  43. #include "fuse_ftruncate.hpp"
  44. #include "fuse_futimens.hpp"
  45. #include "fuse_getattr.hpp"
  46. #include "fuse_getxattr.hpp"
  47. #include "fuse_init.hpp"
  48. #include "fuse_ioctl.hpp"
  49. #include "fuse_link.hpp"
  50. #include "fuse_listxattr.hpp"
  51. #include "fuse_lock.hpp"
  52. #include "fuse_mkdir.hpp"
  53. #include "fuse_mknod.hpp"
  54. #include "fuse_open.hpp"
  55. #include "fuse_opendir.hpp"
  56. #include "fuse_poll.hpp"
  57. #include "fuse_prepare_hide.hpp"
  58. #include "fuse_read.hpp"
  59. #include "fuse_readdir.hpp"
  60. #include "fuse_readdir_plus.hpp"
  61. #include "fuse_readlink.hpp"
  62. #include "fuse_release.hpp"
  63. #include "fuse_releasedir.hpp"
  64. #include "fuse_removemapping.hpp"
  65. #include "fuse_removexattr.hpp"
  66. #include "fuse_rename.hpp"
  67. #include "fuse_rmdir.hpp"
  68. #include "fuse_setupmapping.hpp"
  69. #include "fuse_setxattr.hpp"
  70. #include "fuse_statfs.hpp"
  71. #include "fuse_symlink.hpp"
  72. #include "fuse_syncfs.hpp"
  73. #include "fuse_tmpfile.hpp"
  74. #include "fuse_truncate.hpp"
  75. #include "fuse_unlink.hpp"
  76. #include "fuse_utimens.hpp"
  77. #include "fuse_write.hpp"
  78. #include "fuse.h"
  79. #include <csignal>
  80. #include <cstdlib>
  81. #include <iostream>
  82. #include <string.h>
  83. namespace l
  84. {
  85. static
  86. void
  87. get_fuse_operations(struct fuse_operations &ops_,
  88. const bool nullrw_)
  89. {
  90. ops_.access = FUSE::access;
  91. ops_.bmap = FUSE::bmap;
  92. ops_.chmod = FUSE::chmod;
  93. ops_.chown = FUSE::chown;
  94. ops_.copy_file_range = FUSE::copy_file_range;
  95. ops_.create = FUSE::create;
  96. ops_.destroy = FUSE::destroy;
  97. ops_.fallocate = FUSE::fallocate;
  98. ops_.fchmod = FUSE::fchmod;
  99. ops_.fchown = FUSE::fchown;
  100. ops_.fgetattr = FUSE::fgetattr;
  101. ops_.flock = FUSE::flock;
  102. ops_.flush = FUSE::flush;
  103. ops_.free_hide = FUSE::free_hide;
  104. ops_.fsync = FUSE::fsync;
  105. ops_.fsyncdir = FUSE::fsyncdir;
  106. ops_.ftruncate = FUSE::ftruncate;
  107. ops_.futimens = FUSE::futimens;
  108. ops_.getattr = FUSE::getattr;
  109. ops_.getxattr = FUSE::getxattr;
  110. ops_.init = FUSE::init;
  111. ops_.ioctl = FUSE::ioctl;
  112. ops_.link = FUSE::link;
  113. ops_.listxattr = FUSE::listxattr;
  114. ops_.lock = FUSE::lock;
  115. ops_.mkdir = FUSE::mkdir;
  116. ops_.mknod = FUSE::mknod;
  117. ops_.open = FUSE::open;
  118. ops_.opendir = FUSE::opendir;
  119. ops_.poll = FUSE::poll;;
  120. ops_.prepare_hide = FUSE::prepare_hide;
  121. ops_.read = (nullrw_ ? FUSE::read_null : FUSE::read);
  122. ops_.readdir = FUSE::readdir;
  123. ops_.readdir_plus = FUSE::readdir_plus;
  124. ops_.readlink = FUSE::readlink;
  125. ops_.release = FUSE::release;
  126. ops_.releasedir = FUSE::releasedir;
  127. ops_.removemapping = FUSE::removemapping;
  128. ops_.removexattr = FUSE::removexattr;
  129. ops_.rename = FUSE::rename;
  130. ops_.rmdir = FUSE::rmdir;
  131. ops_.setupmapping = FUSE::setupmapping;
  132. ops_.setxattr = FUSE::setxattr;
  133. ops_.statfs = FUSE::statfs;
  134. ops_.symlink = FUSE::symlink;
  135. ops_.syncfs = FUSE::syncfs;
  136. ops_.tmpfile = FUSE::tmpfile;
  137. ops_.truncate = FUSE::truncate;
  138. ops_.unlink = FUSE::unlink;
  139. ops_.utimens = FUSE::utimens;
  140. ops_.write = (nullrw_ ? FUSE::write_null : FUSE::write);
  141. return;
  142. }
  143. static
  144. void
  145. setup_resources(const int scheduling_priority_)
  146. {
  147. std::srand(time(NULL));
  148. resources::reset_umask();
  149. resources::maxout_rlimit_nofile();
  150. resources::maxout_rlimit_fsize();
  151. resources::setpriority(scheduling_priority_);
  152. }
  153. static
  154. void
  155. wait_for_mount(const Config::Read &cfg_)
  156. {
  157. fs::PathVector paths;
  158. std::chrono::milliseconds timeout;
  159. paths = cfg_->branches->to_paths();
  160. syslog_info("Waiting %u seconds for branches to mount",
  161. (uint64_t)cfg_->branches_mount_timeout);
  162. timeout = std::chrono::milliseconds(cfg_->branches_mount_timeout * 1000);
  163. fs::wait_for_mount((std::string)cfg_->mountpoint,
  164. paths,
  165. timeout);
  166. }
  167. static
  168. void
  169. lazy_umount(const std::string target_)
  170. {
  171. int rv;
  172. rv = fs::umount_lazy(target_);
  173. switch(rv)
  174. {
  175. case 0:
  176. syslog_notice("%s has been successfully lazily unmounted",
  177. target_.c_str());
  178. break;
  179. case -EINVAL:
  180. syslog_notice("%s was not a mount point needing to be unmounted",
  181. target_.c_str());
  182. break;
  183. default:
  184. syslog_error("Error unmounting %s: %d - %s",
  185. target_.c_str(),
  186. -rv,
  187. strerror(-rv));
  188. break;
  189. }
  190. }
  191. static
  192. void
  193. usr1_signal_handler(int signal_)
  194. {
  195. syslog_info("Received SIGUSR1 - invalidating all nodes");
  196. fuse_invalidate_all_nodes();
  197. }
  198. static
  199. void
  200. usr2_signal_handler(int signal_)
  201. {
  202. syslog_info("Received SIGUSR2 - triggering thorough gc");
  203. fuse_gc();
  204. GIDCache::invalidate_all_caches();
  205. }
  206. static
  207. void
  208. setup_signal_handlers()
  209. {
  210. std::signal(SIGUSR1,l::usr1_signal_handler);
  211. std::signal(SIGUSR2,l::usr2_signal_handler);
  212. }
  213. static
  214. void
  215. warn_if_not_root()
  216. {
  217. uid_t uid;
  218. uid = geteuid();
  219. if(uid == 0)
  220. return;
  221. char const *s = "mergerfs is not running as root and may not work correctly\n";
  222. fprintf(stderr,"warning: %s",s);
  223. syslog_warning(s);
  224. }
  225. int
  226. main(const int argc_,
  227. char **argv_)
  228. {
  229. int rv;
  230. Config::Read cfg;
  231. Config::ErrVec errs;
  232. fuse_args args;
  233. fuse_operations ops;
  234. syslog_open();
  235. l::warn_if_not_root();
  236. memset(&ops,0,sizeof(fuse_operations));
  237. args.argc = argc_;
  238. args.argv = argv_;
  239. args.allocated = 0;
  240. options::parse(&args,&errs);
  241. if(errs.size())
  242. {
  243. std::cerr << errs << std::endl;
  244. return 1;
  245. }
  246. if(cfg->branches_mount_timeout > 0)
  247. l::wait_for_mount(cfg);
  248. l::setup_resources(cfg->scheduling_priority);
  249. l::setup_signal_handlers();
  250. l::get_fuse_operations(ops,cfg->nullrw);
  251. if(cfg->lazy_umount_mountpoint)
  252. l::lazy_umount(cfg->mountpoint);
  253. procfs::init();
  254. rv = fuse_main(args.argc,
  255. args.argv,
  256. &ops);
  257. syslog_info("exiting main loop with return code %d",rv);
  258. return rv;
  259. }
  260. }
  261. int
  262. main(int argc_,
  263. char **argv_)
  264. {
  265. State state;
  266. auto data = toml::parse<toml::preserve_comments>("config.toml");
  267. fuse_file_info_t ffi;
  268. Branches2 branches(data["branches"]);
  269. state.create(branches,
  270. "",
  271. O_RDWR,
  272. &ffi);
  273. std::cout << data["branches"] << '\n';
  274. for(auto &branch_tier : branches)
  275. {
  276. for(auto &branch : branch_tier)
  277. {
  278. fmt::print("{}\n",branch.enabled);
  279. }
  280. }
  281. pause();
  282. return 0;
  283. return l::main(argc_,argv_);
  284. }