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.

162 lines
3.9 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 "ugid.hpp"
  16. #include "fs_readahead.hpp"
  17. #include "syslog.hpp"
  18. #include "fmt/core.h"
  19. #include "fuse.h"
  20. #include <thread>
  21. namespace l
  22. {
  23. static
  24. void
  25. want(fuse_conn_info *conn_,
  26. const int flag_)
  27. {
  28. conn_->want |= flag_;
  29. }
  30. static
  31. bool
  32. capable(fuse_conn_info *conn_,
  33. const int flag_)
  34. {
  35. return !!(conn_->capable & flag_);
  36. }
  37. static
  38. void
  39. want_if_capable(fuse_conn_info *conn_,
  40. const int flag_)
  41. {
  42. if(capable(conn_,flag_))
  43. want(conn_,flag_);
  44. }
  45. static
  46. void
  47. want_if_capable(fuse_conn_info *conn_,
  48. const int flag_,
  49. ConfigBOOL *want_)
  50. {
  51. if(*want_ && l::capable(conn_,flag_))
  52. {
  53. l::want(conn_,flag_);
  54. return;
  55. }
  56. *want_ = false;
  57. }
  58. static
  59. void
  60. want_if_capable_max_pages(fuse_conn_info *conn_,
  61. Config::Write &cfg_)
  62. {
  63. if(l::capable(conn_,FUSE_CAP_MAX_PAGES))
  64. {
  65. l::want(conn_,FUSE_CAP_MAX_PAGES);
  66. conn_->max_pages = cfg_->fuse_msg_size;
  67. }
  68. else
  69. {
  70. cfg_->fuse_msg_size = FUSE_DEFAULT_MAX_PAGES_PER_REQ;
  71. }
  72. }
  73. static
  74. void
  75. readahead(const std::string path_,
  76. const int readahead_)
  77. {
  78. int rv;
  79. rv = fs::readahead(path_,readahead_);
  80. if(rv == 0)
  81. syslog_info("%s - readahead set to %d",path_.c_str(),readahead_);
  82. else
  83. syslog_error("%s - unable to set readahead",path_.c_str());
  84. }
  85. static
  86. void
  87. set_readahead_on_mount_and_branches()
  88. {
  89. Config::Read cfg;
  90. Branches::CPtr branches;
  91. if((uint64_t)cfg->readahead == 0)
  92. return;
  93. l::readahead(cfg->mountpoint,cfg->readahead);
  94. branches = cfg->branches;
  95. for(auto const &branch : *branches)
  96. l::readahead(branch.path,cfg->readahead);
  97. }
  98. // Spawn a thread to do this because before init returns calls to
  99. // set the value will block leading to a deadlock. This is just
  100. // easier.
  101. static
  102. void
  103. spawn_thread_to_set_readahead()
  104. {
  105. std::thread readahead_thread(l::set_readahead_on_mount_and_branches);
  106. readahead_thread.detach();
  107. }
  108. }
  109. namespace FUSE
  110. {
  111. void *
  112. init(fuse_conn_info *conn_)
  113. {
  114. Config::Write cfg;
  115. ugid::init();
  116. cfg->readdir.initialize();
  117. l::want_if_capable(conn_,FUSE_CAP_ASYNC_DIO);
  118. l::want_if_capable(conn_,FUSE_CAP_ASYNC_READ,&cfg->async_read);
  119. l::want_if_capable(conn_,FUSE_CAP_ATOMIC_O_TRUNC);
  120. l::want_if_capable(conn_,FUSE_CAP_BIG_WRITES);
  121. l::want_if_capable(conn_,FUSE_CAP_CACHE_SYMLINKS,&cfg->cache_symlinks);
  122. l::want_if_capable(conn_,FUSE_CAP_DONT_MASK);
  123. l::want_if_capable(conn_,FUSE_CAP_IOCTL_DIR);
  124. l::want_if_capable(conn_,FUSE_CAP_PARALLEL_DIROPS);
  125. l::want_if_capable(conn_,FUSE_CAP_READDIR_PLUS,&cfg->readdirplus);
  126. //l::want_if_capable(conn_,FUSE_CAP_READDIR_PLUS_AUTO);
  127. l::want_if_capable(conn_,FUSE_CAP_POSIX_ACL,&cfg->posix_acl);
  128. l::want_if_capable(conn_,FUSE_CAP_WRITEBACK_CACHE,&cfg->writeback_cache);
  129. l::want_if_capable_max_pages(conn_,cfg);
  130. conn_->want &= ~FUSE_CAP_POSIX_LOCKS;
  131. conn_->want &= ~FUSE_CAP_FLOCK_LOCKS;
  132. l::spawn_thread_to_set_readahead();
  133. return NULL;
  134. }
  135. }