diff --git a/src/policy.cpp b/src/policy.cpp index 8e1cee3c..33c59b11 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -44,7 +44,7 @@ const std::vector Policy::_policies_ = (POLICY(mspmfs,PRESERVES_PATH)) (POLICY(newest,DOESNT_PRESERVE_PATH)) (POLICY(rand,DOESNT_PRESERVE_PATH)) - (POLICY(fair,DOESNT_PRESERVE_PATH)); + (POLICY(pfrd,DOESNT_PRESERVE_PATH)); const Policy * const Policy::policies = &_policies_[1]; @@ -68,7 +68,7 @@ CONST_POLICY(msplus); CONST_POLICY(mspmfs); CONST_POLICY(newest); CONST_POLICY(rand); -CONST_POLICY(fair); +CONST_POLICY(pfrd); const Policy& Policy::find(const std::string &str) diff --git a/src/policy.hpp b/src/policy.hpp index 87d68ea8..bacf6238 100644 --- a/src/policy.hpp +++ b/src/policy.hpp @@ -49,7 +49,7 @@ public: mspmfs, newest, rand, - fair, + pfrd, END }; @@ -131,7 +131,7 @@ public: static int mspmfs(CType,const Branches&,const char *,cuint64_t,strvec*); static int newest(CType,const Branches&,const char *,cuint64_t,strvec*); static int rand(CType,const Branches&,const char *,cuint64_t,strvec*); - static int fair(CType,const Branches&,const char *,cuint64_t,strvec*); + static int pfrd(CType,const Branches&,const char *,cuint64_t,strvec*); }; private: @@ -213,7 +213,7 @@ public: static const Policy &mspmfs; static const Policy &newest; static const Policy &rand; - static const Policy &fair; + static const Policy &pfrd; }; namespace std diff --git a/src/policy_fair.cpp b/src/policy_pfrd.cpp similarity index 72% rename from src/policy_fair.cpp rename to src/policy_pfrd.cpp index 4bb0d8ca..09c625cc 100644 --- a/src/policy_fair.cpp +++ b/src/policy_pfrd.cpp @@ -29,7 +29,7 @@ using std::string; using std::vector; -namespace fair +namespace pfrd { uint64_t get_int64_rand(uint64_t const& min, uint64_t const& max) { @@ -47,15 +47,19 @@ namespace fair int rv; int error; uint64_t sum, index; - uint64_t random_byte; + uint64_t byte_index; fs::info_t info; const Branch *branch; - const string *fairbasepath; + const string *pfrdbasepath; + uint64_t cache_spaceavail_branches[256]; + bool useable_branches[256]; + const string *cache_basepath_branches[256]; + size_t branch_count = branches_.size(); error = ENOENT; - fairbasepath = NULL; + pfrdbasepath = NULL; sum = 0, index = 0; - for(size_t i = 0, ei = branches_.size(); i != ei; i++) + for(size_t i = 0; i != branch_count; i++) { branch = &branches_[i]; @@ -69,51 +73,46 @@ namespace fair if(info.spaceavail < minfreespace_) error_and_continue(error,ENOSPC); + cache_spaceavail_branches[i] = info.spaceavail; + useable_branches[i] = true; + cache_basepath_branches[i] = &branch->path; + sum += info.spaceavail; } - random_byte = get_int64_rand(0, sum); + byte_index = get_int64_rand(0, sum); - for(size_t i = 0, ei = branches_.size(); i != ei; i++) + for(size_t i = 0; i != branch_count; i++) { - branch = &branches_[i]; - - if(branch->ro_or_nc()) - error_and_continue(error,EROFS); - rv = fs::info(&branch->path,&info); - if(rv == -1) - error_and_continue(error,ENOENT); - if(info.readonly) - error_and_continue(error,EROFS); - if(info.spaceavail < minfreespace_) - error_and_continue(error,ENOSPC); + if (!useable_branches[i]) + continue; - index += info.spaceavail; + index += cache_spaceavail_branches[i]; - if(index > random_byte) { - fairbasepath = &branch->path; + if(index > byte_index) { + pfrdbasepath = cache_basepath_branches[i]; break; } } - if(fairbasepath == NULL) + if(pfrdbasepath == NULL) return (errno=error,-1); - paths_->push_back(*fairbasepath); + paths_->push_back(*pfrdbasepath); return 0; } } int -Policy::Func::fair(const Category::Enum::Type type, +Policy::Func::pfrd(const Category::Enum::Type type, const Branches &branches_, const char *fusepath, const uint64_t minfreespace, vector *paths) { if(type == Category::Enum::create) - return fair::create(branches_,minfreespace,paths); + return pfrd::create(branches_,minfreespace,paths); return Policy::Func::epmfs(type,branches_,fusepath,minfreespace,paths); }