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.

451 lines
11 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 "ef.hpp"
  16. #include "errno.hpp"
  17. #include "from_string.hpp"
  18. #include "num.hpp"
  19. #include "rwlock.hpp"
  20. #include "str.hpp"
  21. #include "to_string.hpp"
  22. #include "version.hpp"
  23. #include <algorithm>
  24. #include <cstdint>
  25. #include <fstream>
  26. #include <iostream>
  27. #include <string>
  28. #include <pthread.h>
  29. #include <string.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #define MINFREESPACE_DEFAULT (4294967295ULL)
  33. #define IFERT(S) if(S == s_) return true
  34. const std::string CONTROLFILE = "/.mergerfs";
  35. constexpr static const char CACHE_FILES_PROCESS_NAMES_DEFAULT[] =
  36. "rtorrent|"
  37. "qbittorrent-nox";
  38. Config Config::_singleton;
  39. namespace l
  40. {
  41. static
  42. bool
  43. readonly(const std::string &s_)
  44. {
  45. IFERT("async_read");
  46. IFERT("branches-mount-timeout");
  47. IFERT("cache.symlinks");
  48. IFERT("cache.writeback");
  49. IFERT("direct-io-allow-mmap");
  50. IFERT("export-support");
  51. IFERT("fsname");
  52. IFERT("fuse_msg_size");
  53. IFERT("handle-killpriv");
  54. IFERT("handle-killpriv-v2");
  55. IFERT("mount");
  56. IFERT("nullrw");
  57. IFERT("pid");
  58. IFERT("pin-threads");
  59. IFERT("process-thread-count");
  60. IFERT("process-thread-queue-depth");
  61. IFERT("read-thread-count");
  62. IFERT("readdirplus");
  63. IFERT("scheduling-priority");
  64. IFERT("srcmounts");
  65. IFERT("threads");
  66. IFERT("version");
  67. return false;
  68. }
  69. }
  70. Config::Config()
  71. : async_read(true),
  72. auto_cache(false),
  73. minfreespace(MINFREESPACE_DEFAULT),
  74. branches(minfreespace),
  75. branches_mount_timeout(0),
  76. cache_attr(1),
  77. cache_entry(1),
  78. cache_files(CacheFiles::ENUM::LIBFUSE),
  79. cache_files_process_names(CACHE_FILES_PROCESS_NAMES_DEFAULT),
  80. cache_negative_entry(0),
  81. cache_readdir(false),
  82. cache_statfs(0),
  83. cache_symlinks(false),
  84. category(func),
  85. direct_io(false),
  86. direct_io_allow_mmap(true),
  87. dropcacheonclose(false),
  88. export_support(true),
  89. flushonclose(FlushOnClose::ENUM::OPENED_FOR_WRITE),
  90. follow_symlinks(FollowSymlinks::ENUM::NEVER),
  91. fsname(),
  92. func(),
  93. fuse_msg_size(FUSE_MAX_MAX_PAGES),
  94. handle_killpriv(true),
  95. handle_killpriv_v2(true),
  96. ignorepponrename(false),
  97. inodecalc("hybrid-hash"),
  98. lazy_umount_mountpoint(false),
  99. link_cow(false),
  100. link_exdev(LinkEXDEV::ENUM::PASSTHROUGH),
  101. log_metrics(false),
  102. mountpoint(),
  103. moveonenospc(false),
  104. nfsopenhack(NFSOpenHack::ENUM::OFF),
  105. nullrw(false),
  106. parallel_direct_writes(false),
  107. passthrough(false),
  108. posix_acl(false),
  109. readahead(0),
  110. readdir("seq"),
  111. readdirplus(false),
  112. rename_exdev(RenameEXDEV::ENUM::PASSTHROUGH),
  113. scheduling_priority(-10),
  114. security_capability(true),
  115. srcmounts(branches),
  116. statfs(StatFS::ENUM::BASE),
  117. statfs_ignore(StatFSIgnore::ENUM::NONE),
  118. symlinkify(false),
  119. symlinkify_timeout(3600),
  120. fuse_read_thread_count(0),
  121. fuse_process_thread_count(-1),
  122. fuse_process_thread_queue_depth(0),
  123. fuse_pin_threads("false"),
  124. version(MERGERFS_VERSION),
  125. writeback_cache(false),
  126. xattr(XAttr::ENUM::PASSTHROUGH),
  127. _initialized(false)
  128. {
  129. _map["async_read"] = &async_read;
  130. _map["auto_cache"] = &auto_cache;
  131. _map["branches"] = &branches;
  132. _map["branches-mount-timeout"] = &branches_mount_timeout;
  133. _map["cache.attr"] = &cache_attr;
  134. _map["cache.entry"] = &cache_entry;
  135. _map["cache.files"] = &cache_files;
  136. _map["cache.files.process-names"] = &cache_files_process_names;
  137. _map["cache.negative_entry"] = &cache_negative_entry;
  138. _map["cache.readdir"] = &cache_readdir;
  139. _map["cache.statfs"] = &cache_statfs;
  140. _map["cache.symlinks"] = &cache_symlinks;
  141. _map["cache.writeback"] = &writeback_cache;
  142. _map["category.action"] = &category.action;
  143. _map["category.create"] = &category.create;
  144. _map["category.search"] = &category.search;
  145. _map["direct_io"] = &direct_io;
  146. _map["direct-io-allow-mmap"] = &direct_io_allow_mmap;
  147. _map["dropcacheonclose"] = &dropcacheonclose;
  148. _map["export-support"] = &export_support;
  149. _map["flush-on-close"] = &flushonclose;
  150. _map["follow-symlinks"] = &follow_symlinks;
  151. _map["fsname"] = &fsname;
  152. _map["func.access"] = &func.access;
  153. _map["func.chmod"] = &func.chmod;
  154. _map["func.chown"] = &func.chown;
  155. _map["func.create"] = &func.create;
  156. _map["func.getattr"] = &func.getattr;
  157. _map["func.getxattr"] = &func.getxattr;
  158. _map["func.link"] = &func.link;
  159. _map["func.listxattr"] = &func.listxattr;
  160. _map["func.mkdir"] = &func.mkdir;
  161. _map["func.mknod"] = &func.mknod;
  162. _map["func.open"] = &func.open;
  163. _map["func.readdir"] = &readdir;
  164. _map["func.readlink"] = &func.readlink;
  165. _map["func.removexattr"] = &func.removexattr;
  166. _map["func.rename"] = &func.rename;
  167. _map["func.rmdir"] = &func.rmdir;
  168. _map["func.setxattr"] = &func.setxattr;
  169. _map["func.symlink"] = &func.symlink;
  170. _map["func.truncate"] = &func.truncate;
  171. _map["func.unlink"] = &func.unlink;
  172. _map["func.utimens"] = &func.utimens;
  173. _map["fuse_msg_size"] = &fuse_msg_size;
  174. _map["handle-killpriv"] = &handle_killpriv;
  175. _map["handle-killpriv-v2"] = &handle_killpriv_v2;
  176. _map["ignorepponrename"] = &ignorepponrename;
  177. _map["inodecalc"] = &inodecalc;
  178. _map["kernel_cache"] = &kernel_cache;
  179. _map["lazy-umount-mountpoint"] = &lazy_umount_mountpoint;
  180. _map["link_cow"] = &link_cow;
  181. _map["link-exdev"] = &link_exdev;
  182. _map["log.metrics"] = &log_metrics;
  183. _map["minfreespace"] = &minfreespace;
  184. _map["mount"] = &mountpoint;
  185. _map["moveonenospc"] = &moveonenospc;
  186. _map["nfsopenhack"] = &nfsopenhack;
  187. _map["nullrw"] = &nullrw;
  188. _map["pid"] = &pid;
  189. _map["parallel-direct-writes"] = &parallel_direct_writes;
  190. _map["passthrough"] = &passthrough;
  191. _map["pin-threads"] = &fuse_pin_threads;
  192. _map["posix_acl"] = &posix_acl;
  193. _map["readahead"] = &readahead;
  194. _map["readdirplus"] = &readdirplus;
  195. _map["rename-exdev"] = &rename_exdev;
  196. _map["scheduling-priority"] = &scheduling_priority;
  197. _map["security_capability"] = &security_capability;
  198. _map["srcmounts"] = &srcmounts;
  199. _map["statfs"] = &statfs;
  200. _map["statfs_ignore"] = &statfs_ignore;
  201. _map["symlinkify"] = &symlinkify;
  202. _map["symlinkify_timeout"] = &symlinkify_timeout;
  203. _map["threads"] = &fuse_read_thread_count;
  204. _map["read-thread-count"] = &fuse_read_thread_count;
  205. _map["process-thread-count"] = &fuse_process_thread_count;
  206. _map["process-thread-queue-depth"] = &fuse_process_thread_queue_depth;
  207. _map["version"] = &version;
  208. _map["xattr"] = &xattr;
  209. }
  210. Config&
  211. Config::operator=(const Config &cfg_)
  212. {
  213. int rv;
  214. std::string val;
  215. for(auto &kv : _map)
  216. {
  217. rv = cfg_.get(kv.first,&val);
  218. if(rv)
  219. continue;
  220. kv.second->from_string(val);
  221. }
  222. return *this;
  223. }
  224. bool
  225. Config::has_key(const std::string &key_) const
  226. {
  227. return _map.count(key_);
  228. }
  229. void
  230. Config::keys(std::string &s_) const
  231. {
  232. Str2TFStrMap::const_iterator i;
  233. Str2TFStrMap::const_iterator ei;
  234. s_.reserve(512);
  235. i = _map.begin();
  236. ei = _map.end();
  237. for(; i != ei; ++i)
  238. {
  239. s_ += i->first;
  240. s_ += '\0';
  241. }
  242. s_.resize(s_.size() - 1);
  243. }
  244. void
  245. Config::keys_xattr(std::string &s_) const
  246. {
  247. Str2TFStrMap::const_iterator i;
  248. Str2TFStrMap::const_iterator ei;
  249. s_.reserve(1024);
  250. for(i = _map.begin(), ei = _map.end(); i != ei; ++i)
  251. {
  252. s_ += "user.mergerfs.";
  253. s_ += i->first;
  254. s_ += '\0';
  255. }
  256. }
  257. int
  258. Config::get(const std::string &key_,
  259. std::string *val_) const
  260. {
  261. Str2TFStrMap::const_iterator i;
  262. i = _map.find(key_);
  263. if(i == _map.end())
  264. return -ENOATTR;
  265. *val_ = i->second->to_string();
  266. return 0;
  267. }
  268. int
  269. Config::set_raw(const std::string &key_,
  270. const std::string &value_)
  271. {
  272. Str2TFStrMap::iterator i;
  273. i = _map.find(key_);
  274. if(i == _map.end())
  275. return -ENOATTR;
  276. return i->second->from_string(value_);
  277. }
  278. int
  279. Config::set(const std::string &key_,
  280. const std::string &value_)
  281. {
  282. if(_initialized && l::readonly(key_))
  283. return -EROFS;
  284. return set_raw(key_,value_);
  285. }
  286. int
  287. Config::set(const std::string &kv_)
  288. {
  289. std::string key;
  290. std::string val;
  291. str::splitkv(kv_,'=',&key,&val);
  292. key = str::trim(key);
  293. val = str::trim(val);
  294. return set(key,val);
  295. }
  296. int
  297. Config::from_stream(std::istream &istrm_,
  298. ErrVec *errs_)
  299. {
  300. int rv;
  301. std::string line;
  302. std::string key;
  303. std::string val;
  304. Config newcfg;
  305. newcfg = *this;
  306. while(std::getline(istrm_,line,'\n'))
  307. {
  308. line = str::trim(line);
  309. if(!line.empty() && (line[0] == '#'))
  310. continue;
  311. str::splitkv(line,'=',&key,&val);
  312. key = str::trim(key);
  313. val = str::trim(val);
  314. rv = newcfg.set(key,val);
  315. if(rv < 0)
  316. errs_->push_back({rv,key});
  317. }
  318. if(!errs_->empty())
  319. return -EINVAL;
  320. *this = newcfg;
  321. return 0;
  322. }
  323. int
  324. Config::from_file(const std::string &filepath_,
  325. ErrVec *errs_)
  326. {
  327. int rv;
  328. std::ifstream ifstrm;
  329. ifstrm.open(filepath_);
  330. if(!ifstrm.good())
  331. {
  332. errs_->push_back({-errno,filepath_});
  333. return -errno;
  334. }
  335. rv = from_stream(ifstrm,errs_);
  336. ifstrm.close();
  337. return rv;
  338. }
  339. void
  340. Config::finish_initializing()
  341. {
  342. _initialized = true;
  343. }
  344. std::ostream&
  345. operator<<(std::ostream &os_,
  346. const Config &c_)
  347. {
  348. Str2TFStrMap::const_iterator i;
  349. Str2TFStrMap::const_iterator ei;
  350. for(i = c_._map.begin(), ei = c_._map.end(); i != ei; ++i)
  351. {
  352. os_ << i->first << '=' << i->second->to_string() << std::endl;
  353. }
  354. return os_;
  355. }
  356. static
  357. std::string
  358. err2str(const int err_)
  359. {
  360. switch(err_)
  361. {
  362. case 0:
  363. return std::string();
  364. case -EINVAL:
  365. return "invalid value";
  366. case -ENOATTR:
  367. return "unknown option";
  368. case -EROFS:
  369. return "read-only option";
  370. default:
  371. return strerror(-err_);
  372. }
  373. return std::string();
  374. }
  375. std::ostream&
  376. operator<<(std::ostream &os_,
  377. const Config::ErrVec &ev_)
  378. {
  379. std::string errstr;
  380. for(auto &err : ev_)
  381. {
  382. os_ << "* ERROR: ";
  383. errstr = err2str(err.err);
  384. if(!errstr.empty())
  385. os_ << errstr << " - ";
  386. os_ << err.str << std::endl;
  387. }
  388. return os_;
  389. }