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.

149 lines
3.7 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_info.hpp"
  16. #include "fs_path.hpp"
  17. #include "policies.hpp"
  18. #include "policy.hpp"
  19. #include "policy_error.hpp"
  20. #include "policy_pfrd.hpp"
  21. #include "rnd.hpp"
  22. #include "strvec.hpp"
  23. #include <string>
  24. #include <vector>
  25. using std::string;
  26. using std::vector;
  27. struct BranchInfo
  28. {
  29. uint64_t spaceavail;
  30. const string *basepath;
  31. };
  32. typedef vector<BranchInfo> BranchInfoVec;
  33. namespace pfrd
  34. {
  35. static
  36. int
  37. get_branchinfo(const Branches::CPtr &branches_,
  38. BranchInfoVec *branchinfo_,
  39. uint64_t *sum_)
  40. {
  41. int rv;
  42. int error;
  43. BranchInfo bi;
  44. fs::info_t info;
  45. *sum_ = 0;
  46. error = ENOENT;
  47. for(auto &branch : *branches_)
  48. {
  49. if(branch.ro_or_nc())
  50. error_and_continue(error,EROFS);
  51. rv = fs::info(branch.path,&info);
  52. if(rv == -1)
  53. error_and_continue(error,ENOENT);
  54. if(info.readonly)
  55. error_and_continue(error,EROFS);
  56. if(info.spaceavail < branch.minfreespace())
  57. error_and_continue(error,ENOSPC);
  58. *sum_ += info.spaceavail;
  59. bi.spaceavail = info.spaceavail;
  60. bi.basepath = &branch.path;
  61. branchinfo_->push_back(bi);
  62. }
  63. return error;
  64. }
  65. static
  66. const
  67. string*
  68. get_branch(const BranchInfoVec &branchinfo_,
  69. const uint64_t sum_)
  70. {
  71. uint64_t idx;
  72. uint64_t threshold;
  73. if(sum_ == 0)
  74. return NULL;
  75. idx = 0;
  76. threshold = RND::rand64(sum_);
  77. for(size_t i = 0; i < branchinfo_.size(); i++)
  78. {
  79. idx += branchinfo_[i].spaceavail;
  80. if(idx < threshold)
  81. continue;
  82. return branchinfo_[i].basepath;
  83. }
  84. return NULL;
  85. }
  86. static
  87. int
  88. create(const Branches::CPtr &branches_,
  89. const char *fusepath_,
  90. StrVec *paths_)
  91. {
  92. int error;
  93. uint64_t sum;
  94. const string *basepath;
  95. BranchInfoVec branchinfo;
  96. error = pfrd::get_branchinfo(branches_,&branchinfo,&sum);
  97. basepath = pfrd::get_branch(branchinfo,sum);
  98. if(basepath == NULL)
  99. return (errno=error,-1);
  100. paths_->push_back(*basepath);
  101. return 0;
  102. }
  103. }
  104. int
  105. Policy::PFRD::Action::operator()(const Branches::CPtr &branches_,
  106. const char *fusepath_,
  107. StrVec *paths_) const
  108. {
  109. return Policies::Action::eppfrd(branches_,fusepath_,paths_);
  110. }
  111. int
  112. Policy::PFRD::Create::operator()(const Branches::CPtr &branches_,
  113. const char *fusepath_,
  114. StrVec *paths_) const
  115. {
  116. return ::pfrd::create(branches_,fusepath_,paths_);
  117. }
  118. int
  119. Policy::PFRD::Search::operator()(const Branches::CPtr &branches_,
  120. const char *fusepath_,
  121. StrVec *paths_) const
  122. {
  123. return Policies::Search::eppfrd(branches_,fusepath_,paths_);
  124. }