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.

180 lines
4.2 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "errno.hpp"
  16. #include "fs_exists.hpp"
  17. #include "fs_info.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_statvfs_cache.hpp"
  20. #include "policy.hpp"
  21. #include "policy_error.hpp"
  22. #include "rnd.hpp"
  23. #include "rwlock.hpp"
  24. #include <string>
  25. #include <vector>
  26. using std::string;
  27. using std::vector;
  28. struct BranchInfo
  29. {
  30. uint64_t spaceavail;
  31. const string *basepath;
  32. };
  33. typedef vector<BranchInfo> BranchInfoVec;
  34. namespace msppfrd
  35. {
  36. static
  37. int
  38. create_1(const BranchVec &branches_,
  39. const string &fusepath_,
  40. BranchInfoVec *branchinfo_,
  41. uint64_t *sum_)
  42. {
  43. int rv;
  44. int error;
  45. BranchInfo bi;
  46. fs::info_t info;
  47. const Branch *branch;
  48. *sum_ = 0;
  49. error = ENOENT;
  50. for(size_t i = 0, ei = branches_.size(); i < ei; i++)
  51. {
  52. branch = &branches_[i];
  53. if(branch->ro_or_nc())
  54. error_and_continue(error,EROFS);
  55. if(!fs::exists(branch->path,fusepath_))
  56. error_and_continue(error,ENOENT);
  57. rv = fs::info(branch->path,&info);
  58. if(rv == -1)
  59. error_and_continue(error,ENOENT);
  60. if(info.readonly)
  61. error_and_continue(error,EROFS);
  62. if(info.spaceavail < branch->minfreespace())
  63. error_and_continue(error,ENOSPC);
  64. *sum_ += info.spaceavail;
  65. bi.spaceavail = info.spaceavail;
  66. bi.basepath = &branch->path;
  67. branchinfo_->push_back(bi);
  68. }
  69. return error;
  70. }
  71. static
  72. int
  73. create_1(const Branches &branches_,
  74. const string &fusepath_,
  75. BranchInfoVec *branchinfo_,
  76. uint64_t *sum_)
  77. {
  78. rwlock::ReadGuard guard(branches_.lock);
  79. branchinfo_->reserve(branches_.vec.size());
  80. return msppfrd::create_1(branches_.vec,fusepath_,branchinfo_,sum_);
  81. }
  82. static
  83. int
  84. get_branchinfo(const Branches &branches_,
  85. const char *fusepath_,
  86. BranchInfoVec *branchinfo_,
  87. uint64_t *sum_)
  88. {
  89. int error;
  90. string fusepath;
  91. fusepath = fusepath_;
  92. do
  93. {
  94. error = msppfrd::create_1(branches_,fusepath,branchinfo_,sum_);
  95. if(branchinfo_->size())
  96. return error;
  97. fusepath = fs::path::dirname(fusepath);
  98. }
  99. while(!fusepath.empty());
  100. return error;
  101. }
  102. static
  103. const
  104. string*
  105. get_branch(const BranchInfoVec &branchinfo_,
  106. const uint64_t sum_)
  107. {
  108. uint64_t idx;
  109. uint64_t threshold;
  110. idx = 0;
  111. threshold = RND::rand64(sum_);
  112. for(size_t i = 0; i < branchinfo_.size(); i++)
  113. {
  114. idx += branchinfo_[i].spaceavail;
  115. if(idx < threshold)
  116. continue;
  117. return branchinfo_[i].basepath;
  118. }
  119. return NULL;
  120. }
  121. static
  122. int
  123. create(const Branches &branches_,
  124. const char *fusepath_,
  125. vector<string> *paths_)
  126. {
  127. int error;
  128. uint64_t sum;
  129. const string *basepath;
  130. BranchInfoVec branchinfo;
  131. error = msppfrd::get_branchinfo(branches_,fusepath_,&branchinfo,&sum);
  132. basepath = msppfrd::get_branch(branchinfo,sum);
  133. if(basepath == NULL)
  134. return (errno=error,-1);
  135. paths_->push_back(*basepath);
  136. return 0;
  137. }
  138. }
  139. int
  140. Policy::Func::msppfrd(const Category type_,
  141. const Branches &branches_,
  142. const char *fusepath_,
  143. vector<string> *paths_)
  144. {
  145. if(type_ == Category::CREATE)
  146. return msppfrd::create(branches_,fusepath_,paths_);
  147. return Policy::Func::eppfrd(type_,branches_,fusepath_,paths_);
  148. }