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.

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