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.

219 lines
6.3 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 "fs_wait_for_mount.hpp"
  15. #include "syslog.hpp"
  16. #include "fs_path.hpp"
  17. #include "mergerfs.hpp"
  18. #include "option_parser.hpp"
  19. #include "resources.hpp"
  20. #include "strvec.hpp"
  21. #include "fuse_access.hpp"
  22. #include "fuse_bmap.hpp"
  23. #include "fuse_chmod.hpp"
  24. #include "fuse_chown.hpp"
  25. #include "fuse_copy_file_range.hpp"
  26. #include "fuse_create.hpp"
  27. #include "fuse_destroy.hpp"
  28. #include "fuse_fallocate.hpp"
  29. #include "fuse_fchmod.hpp"
  30. #include "fuse_fchown.hpp"
  31. #include "fuse_fgetattr.hpp"
  32. #include "fuse_flock.hpp"
  33. #include "fuse_flush.hpp"
  34. #include "fuse_free_hide.hpp"
  35. #include "fuse_fsync.hpp"
  36. #include "fuse_fsyncdir.hpp"
  37. #include "fuse_ftruncate.hpp"
  38. #include "fuse_futimens.hpp"
  39. #include "fuse_getattr.hpp"
  40. #include "fuse_getxattr.hpp"
  41. #include "fuse_init.hpp"
  42. #include "fuse_ioctl.hpp"
  43. #include "fuse_link.hpp"
  44. #include "fuse_listxattr.hpp"
  45. #include "fuse_lock.hpp"
  46. #include "fuse_mkdir.hpp"
  47. #include "fuse_mknod.hpp"
  48. #include "fuse_open.hpp"
  49. #include "fuse_opendir.hpp"
  50. #include "fuse_poll.hpp"
  51. #include "fuse_prepare_hide.hpp"
  52. #include "fuse_read_buf.hpp"
  53. #include "fuse_readdir.hpp"
  54. #include "fuse_readdir_plus.hpp"
  55. #include "fuse_readlink.hpp"
  56. #include "fuse_release.hpp"
  57. #include "fuse_releasedir.hpp"
  58. #include "fuse_removexattr.hpp"
  59. #include "fuse_rename.hpp"
  60. #include "fuse_rmdir.hpp"
  61. #include "fuse_setxattr.hpp"
  62. #include "fuse_statfs.hpp"
  63. #include "fuse_symlink.hpp"
  64. #include "fuse_truncate.hpp"
  65. #include "fuse_unlink.hpp"
  66. #include "fuse_utimens.hpp"
  67. #include "fuse_write.hpp"
  68. #include "fuse.h"
  69. #include <cstdlib>
  70. #include <iostream>
  71. #include <string.h>
  72. namespace l
  73. {
  74. static
  75. void
  76. get_fuse_operations(struct fuse_operations &ops_,
  77. const bool nullrw_)
  78. {
  79. ops_.access = FUSE::access;
  80. ops_.bmap = FUSE::bmap;
  81. ops_.chmod = FUSE::chmod;
  82. ops_.chown = FUSE::chown;
  83. ops_.copy_file_range = FUSE::copy_file_range;
  84. ops_.create = FUSE::create;
  85. ops_.destroy = FUSE::destroy;
  86. ops_.fallocate = FUSE::fallocate;
  87. ops_.fchmod = FUSE::fchmod;
  88. ops_.fchown = FUSE::fchown;
  89. ops_.fgetattr = FUSE::fgetattr;
  90. ops_.flock = FUSE::flock;
  91. ops_.flush = FUSE::flush;
  92. ops_.free_hide = FUSE::free_hide;
  93. ops_.fsync = FUSE::fsync;
  94. ops_.fsyncdir = FUSE::fsyncdir;
  95. ops_.ftruncate = FUSE::ftruncate;
  96. ops_.futimens = FUSE::futimens;
  97. ops_.getattr = FUSE::getattr;
  98. ops_.getxattr = FUSE::getxattr;
  99. ops_.init = FUSE::init;
  100. ops_.ioctl = FUSE::ioctl;
  101. ops_.link = FUSE::link;
  102. ops_.listxattr = FUSE::listxattr;
  103. ops_.lock = FUSE::lock;
  104. ops_.mkdir = FUSE::mkdir;
  105. ops_.mknod = FUSE::mknod;
  106. ops_.open = FUSE::open;
  107. ops_.opendir = FUSE::opendir;
  108. ops_.poll = FUSE::poll;;
  109. ops_.prepare_hide = FUSE::prepare_hide;
  110. ops_.read_buf = (nullrw_ ? FUSE::read_buf_null : FUSE::read_buf);
  111. ops_.readdir = FUSE::readdir;
  112. ops_.readdir_plus = FUSE::readdir_plus;
  113. ops_.readlink = FUSE::readlink;
  114. ops_.release = FUSE::release;
  115. ops_.releasedir = FUSE::releasedir;
  116. ops_.removexattr = FUSE::removexattr;
  117. ops_.rename = FUSE::rename;
  118. ops_.rmdir = FUSE::rmdir;
  119. ops_.setxattr = FUSE::setxattr;
  120. ops_.statfs = FUSE::statfs;
  121. ops_.symlink = FUSE::symlink;
  122. ops_.truncate = FUSE::truncate;
  123. ops_.unlink = FUSE::unlink;
  124. ops_.utimens = FUSE::utimens;
  125. ops_.write = (nullrw_ ? FUSE::write_null : FUSE::write);
  126. return;
  127. }
  128. static
  129. void
  130. setup_resources(const int scheduling_priority_)
  131. {
  132. std::srand(time(NULL));
  133. resources::reset_umask();
  134. resources::maxout_rlimit_nofile();
  135. resources::maxout_rlimit_fsize();
  136. resources::setpriority(scheduling_priority_);
  137. }
  138. static
  139. void
  140. wait_for_mount(const Config::Read &cfg_)
  141. {
  142. fs::PathVector paths;
  143. fs::PathVector failed;
  144. std::chrono::milliseconds timeout;
  145. paths = cfg_->branches->to_paths();
  146. syslog_info("Waiting %u seconds for branches to mount",
  147. (uint64_t)cfg_->branches_mount_timeout);
  148. timeout = std::chrono::milliseconds(cfg_->branches_mount_timeout * 1000);
  149. fs::wait_for_mount((std::string)cfg_->mount,
  150. paths,
  151. timeout,
  152. failed);
  153. for(auto &path : failed)
  154. syslog_warning("Branch %s was not mounted within timeout",
  155. path.c_str());
  156. if(failed.size())
  157. syslog_warning("Continuing to mount mergerfs despite %u branches not "
  158. "being different from the mountpoint filesystem",
  159. failed.size());
  160. }
  161. int
  162. main(const int argc_,
  163. char **argv_)
  164. {
  165. Config::Read cfg;
  166. Config::ErrVec errs;
  167. fuse_args args;
  168. fuse_operations ops;
  169. syslog_open();
  170. memset(&ops,0,sizeof(fuse_operations));
  171. args.argc = argc_;
  172. args.argv = argv_;
  173. args.allocated = 0;
  174. options::parse(&args,&errs);
  175. if(errs.size())
  176. {
  177. std::cerr << errs << std::endl;
  178. return 1;
  179. }
  180. if(cfg->branches_mount_timeout > 0)
  181. l::wait_for_mount(cfg);
  182. l::setup_resources(cfg->scheduling_priority);
  183. l::get_fuse_operations(ops,cfg->nullrw);
  184. return fuse_main(args.argc,
  185. args.argv,
  186. &ops);
  187. }
  188. }
  189. int
  190. main(int argc_,
  191. char **argv_)
  192. {
  193. return l::main(argc_,argv_);
  194. }