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.

262 lines
6.5 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 "errno.hpp"
  15. #include "fs_exists.hpp"
  16. #include "fs_info.hpp"
  17. #include "fs_path.hpp"
  18. #include "fs_statvfs_cache.hpp"
  19. #include "policy.hpp"
  20. #include "policy_eppfrd.hpp"
  21. #include "policy_error.hpp"
  22. #include "rnd.hpp"
  23. #include "rwlock.hpp"
  24. #include "strvec.hpp"
  25. #include <string>
  26. #include <vector>
  27. using std::string;
  28. using std::vector;
  29. struct BranchInfo
  30. {
  31. uint64_t spaceavail;
  32. const string *basepath;
  33. };
  34. typedef vector<BranchInfo> BranchInfoVec;
  35. namespace eppfrd
  36. {
  37. static
  38. int
  39. get_branchinfo_create(const Branches::CPtr &branches_,
  40. const char *fusepath_,
  41. BranchInfoVec *branchinfo_,
  42. uint64_t *sum_)
  43. {
  44. int rv;
  45. int error;
  46. BranchInfo bi;
  47. fs::info_t info;
  48. *sum_ = 0;
  49. error = ENOENT;
  50. for(auto &branch : *branches_)
  51. {
  52. if(branch.ro_or_nc())
  53. error_and_continue(error,EROFS);
  54. if(!fs::exists(branch.path,fusepath_))
  55. error_and_continue(error,ENOENT);
  56. rv = fs::info(branch.path,&info);
  57. if(rv == -1)
  58. error_and_continue(error,ENOENT);
  59. if(info.readonly)
  60. error_and_continue(error,EROFS);
  61. if(info.spaceavail < branch.minfreespace())
  62. error_and_continue(error,ENOSPC);
  63. *sum_ += info.spaceavail;
  64. bi.spaceavail = info.spaceavail;
  65. bi.basepath = &branch.path;
  66. branchinfo_->push_back(bi);
  67. }
  68. return error;
  69. }
  70. static
  71. int
  72. get_branchinfo_action(const Branches::CPtr &branches_,
  73. const char *fusepath_,
  74. BranchInfoVec *branchinfo_,
  75. uint64_t *sum_)
  76. {
  77. int rv;
  78. int error;
  79. BranchInfo bi;
  80. fs::info_t info;
  81. *sum_ = 0;
  82. error = ENOENT;
  83. for(auto &branch : *branches_)
  84. {
  85. if(branch.ro())
  86. error_and_continue(error,EROFS);
  87. if(!fs::exists(branch.path,fusepath_))
  88. error_and_continue(error,ENOENT);
  89. rv = fs::info(branch.path,&info);
  90. if(rv == -1)
  91. error_and_continue(error,ENOENT);
  92. if(info.readonly)
  93. error_and_continue(error,EROFS);
  94. *sum_ += info.spaceavail;
  95. bi.spaceavail = info.spaceavail;
  96. bi.basepath = &branch.path;
  97. branchinfo_->push_back(bi);
  98. }
  99. return error;
  100. }
  101. static
  102. int
  103. get_branchinfo_search(const Branches::CPtr &branches_,
  104. const char *fusepath_,
  105. BranchInfoVec *branchinfo_,
  106. uint64_t *sum_)
  107. {
  108. int rv;
  109. BranchInfo bi;
  110. uint64_t spaceavail;
  111. *sum_ = 0;
  112. for(auto &branch : *branches_)
  113. {
  114. if(!fs::exists(branch.path,fusepath_))
  115. continue;
  116. rv = fs::statvfs_cache_spaceavail(branch.path,&spaceavail);
  117. if(rv == -1)
  118. continue;
  119. *sum_ += spaceavail;
  120. bi.spaceavail = spaceavail;
  121. bi.basepath = &branch.path;
  122. branchinfo_->push_back(bi);
  123. }
  124. return ENOENT;
  125. }
  126. static
  127. const
  128. string*
  129. get_branch(const BranchInfoVec &branchinfo_,
  130. const uint64_t sum_)
  131. {
  132. uint64_t idx;
  133. uint64_t threshold;
  134. if(sum_ == 0)
  135. return NULL;
  136. idx = 0;
  137. threshold = RND::rand64(sum_);
  138. for(size_t i = 0; i < branchinfo_.size(); i++)
  139. {
  140. idx += branchinfo_[i].spaceavail;
  141. if(idx < threshold)
  142. continue;
  143. return branchinfo_[i].basepath;
  144. }
  145. return NULL;
  146. }
  147. static
  148. int
  149. create(const Branches::CPtr &branches_,
  150. const char *fusepath_,
  151. StrVec *paths_)
  152. {
  153. int error;
  154. uint64_t sum;
  155. const string *basepath;
  156. BranchInfoVec branchinfo;
  157. error = eppfrd::get_branchinfo_create(branches_,fusepath_,&branchinfo,&sum);
  158. basepath = eppfrd::get_branch(branchinfo,sum);
  159. if(basepath == NULL)
  160. return (errno=error,-1);
  161. paths_->push_back(*basepath);
  162. return 0;
  163. }
  164. static
  165. int
  166. action(const Branches::CPtr &branches_,
  167. const char *fusepath_,
  168. StrVec *paths_)
  169. {
  170. int error;
  171. uint64_t sum;
  172. const string *basepath;
  173. BranchInfoVec branchinfo;
  174. error = eppfrd::get_branchinfo_action(branches_,fusepath_,&branchinfo,&sum);
  175. basepath = eppfrd::get_branch(branchinfo,sum);
  176. if(basepath == NULL)
  177. return (errno=error,-1);
  178. paths_->push_back(*basepath);
  179. return 0;
  180. }
  181. static
  182. int
  183. search(const Branches::CPtr &branches_,
  184. const char *fusepath_,
  185. StrVec *paths_)
  186. {
  187. int error;
  188. uint64_t sum;
  189. const string *basepath;
  190. BranchInfoVec branchinfo;
  191. error = eppfrd::get_branchinfo_search(branches_,fusepath_,&branchinfo,&sum);
  192. basepath = eppfrd::get_branch(branchinfo,sum);
  193. if(basepath == NULL)
  194. return (errno=error,-1);
  195. paths_->push_back(*basepath);
  196. return 0;
  197. }
  198. }
  199. int
  200. Policy::EPPFRD::Action::operator()(const Branches::CPtr &branches_,
  201. const char *fusepath_,
  202. StrVec *paths_) const
  203. {
  204. return ::eppfrd::action(branches_,fusepath_,paths_);
  205. }
  206. int
  207. Policy::EPPFRD::Create::operator()(const Branches::CPtr &branches_,
  208. const char *fusepath_,
  209. StrVec *paths_) const
  210. {
  211. return ::eppfrd::create(branches_,fusepath_,paths_);
  212. }
  213. int
  214. Policy::EPPFRD::Search::operator()(const Branches::CPtr &branches_,
  215. const char *fusepath_,
  216. StrVec *paths_) const
  217. {
  218. return ::eppfrd::search(branches_,fusepath_,paths_);
  219. }