mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-25 01:16:30 -05:00
Implement bespoke branch policies
Consolidate branch policy selection behind the BranchPolicy interfaces and update filesystem operations to use the bespoke policy implementations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
42ab831d97
commit
96d8232853
451 changed files with 18495 additions and 7562 deletions
|
|
@ -21,28 +21,20 @@
|
|||
#include "base_types.h"
|
||||
#include "errno.hpp"
|
||||
#include "fs_clonepath.hpp"
|
||||
#include "fs_close.hpp"
|
||||
#include "fs_copyfile.hpp"
|
||||
#include "fs_file_size.hpp"
|
||||
#include "fs_findonfs.hpp"
|
||||
#include "fs_getfl.hpp"
|
||||
#include "fs_has_space.hpp"
|
||||
#include "fs_mktemp.hpp"
|
||||
#include "fs_info.hpp"
|
||||
#include "fs_info_t.hpp"
|
||||
#include "fs_open.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "fs_rename.hpp"
|
||||
#include "fs_stat.hpp"
|
||||
#include "fs_unlink.hpp"
|
||||
#include "policy.hpp"
|
||||
#include "rnd.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
static
|
||||
|
|
@ -59,13 +51,61 @@ _cleanup_flags(const int flags_)
|
|||
return rv;
|
||||
}
|
||||
|
||||
// Pick a writable destination branch using PFRD (proportional to free
|
||||
// space). Self-contained for moveonenospc — mirrors the same algorithm
|
||||
// used by Func2::CreatePFRD but on a fresh selection (no clonepath/open).
|
||||
static
|
||||
int
|
||||
_movefile_and_open(const Policy::Create &createFunc_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
int origfd_)
|
||||
_pick_pfrd_branch(const Branches &branches_,
|
||||
Branch *&chosen_)
|
||||
{
|
||||
int err = ENOENT;
|
||||
Branches::Ptr branches = branches_;
|
||||
fs::info_t info;
|
||||
struct W { Branch *b; uint64_t weight; };
|
||||
std::vector<W> writable;
|
||||
uint64_t sum = 0;
|
||||
|
||||
for(auto &branch : *branches)
|
||||
{
|
||||
if(branch.ro_or_nc())
|
||||
{ if(err == ENOENT) err = EROFS; continue; }
|
||||
const int irv = fs::info(branch.path,&info);
|
||||
if(irv < 0)
|
||||
{ if(err == ENOENT) err = ENOENT; continue; }
|
||||
if(info.readonly)
|
||||
{ if(err == ENOENT) err = EROFS; continue; }
|
||||
if(info.spaceavail < branch.minfreespace())
|
||||
{ if(err == ENOENT) err = ENOSPC; continue; }
|
||||
|
||||
writable.push_back({&branch,info.spaceavail});
|
||||
sum += info.spaceavail;
|
||||
}
|
||||
|
||||
if(writable.empty())
|
||||
return -err;
|
||||
if(sum == 0)
|
||||
return -ENOSPC;
|
||||
|
||||
const uint64_t threshold = RND::rand64(sum);
|
||||
uint64_t idx = 0;
|
||||
for(const auto &w : writable)
|
||||
{
|
||||
if(w.weight == 0) continue;
|
||||
idx += w.weight;
|
||||
if(idx > threshold) { chosen_ = w.b; return 0; }
|
||||
}
|
||||
|
||||
chosen_ = writable.back().b;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_movefile_and_open(const Branches &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
int origfd_)
|
||||
{
|
||||
int rv;
|
||||
int dstfd_flags;
|
||||
|
|
@ -75,14 +115,14 @@ _movefile_and_open(const Policy::Create &createFunc_,
|
|||
fs::path src_branch;
|
||||
fs::path src_filepath;
|
||||
fs::path dst_filepath;
|
||||
std::vector<Branch*> dst_branch;
|
||||
Branch *dst_branch = nullptr;
|
||||
|
||||
src_branch = branchpath_;
|
||||
|
||||
rv = createFunc_(branches_,fusepath_,dst_branch);
|
||||
rv = ::_pick_pfrd_branch(branches_,dst_branch);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
if(dst_branch.empty())
|
||||
if(dst_branch == nullptr)
|
||||
return -ENOSPC;
|
||||
|
||||
origfd_flags = fs::getfl(origfd_);
|
||||
|
|
@ -93,17 +133,17 @@ _movefile_and_open(const Policy::Create &createFunc_,
|
|||
if(src_size < 0)
|
||||
return src_size;
|
||||
|
||||
if(fs::has_space(dst_branch[0]->path,src_size) == false)
|
||||
if(fs::has_space(dst_branch->path,src_size) == false)
|
||||
return -ENOSPC;
|
||||
|
||||
fusedir = fusepath_.parent_path();
|
||||
|
||||
rv = fs::clonepath(src_branch,dst_branch[0]->path,fusedir);
|
||||
rv = fs::clonepath(src_branch,dst_branch->path,fusedir);
|
||||
if(rv < 0)
|
||||
return -ENOSPC;
|
||||
|
||||
src_filepath = src_branch / fusepath_;
|
||||
dst_filepath = dst_branch[0]->path / fusepath_;
|
||||
dst_filepath = dst_branch->path / fusepath_;
|
||||
|
||||
rv = fs::copyfile(src_filepath,dst_filepath,{.cleanup_failure = true});
|
||||
if(rv < 0)
|
||||
|
|
@ -120,29 +160,19 @@ _movefile_and_open(const Policy::Create &createFunc_,
|
|||
}
|
||||
|
||||
int
|
||||
fs::movefile_and_open(const Policy::Create &policy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
const int origfd_)
|
||||
fs::movefile_and_open(const Branches &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
const int origfd_)
|
||||
{
|
||||
return ::_movefile_and_open(policy_,
|
||||
branches_,
|
||||
branchpath_,
|
||||
fusepath_,
|
||||
origfd_);
|
||||
return ::_movefile_and_open(branches_,branchpath_,fusepath_,origfd_);
|
||||
}
|
||||
|
||||
int
|
||||
fs::movefile_and_open_as_root(const Policy::Create &policy_,
|
||||
const Branches::Ptr &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
const int origfd_)
|
||||
fs::movefile_and_open_as_root(const Branches &branches_,
|
||||
const fs::path &branchpath_,
|
||||
const fs::path &fusepath_,
|
||||
const int origfd_)
|
||||
{
|
||||
return fs::movefile_and_open(policy_,
|
||||
branches_,
|
||||
branchpath_,
|
||||
fusepath_,
|
||||
origfd_);
|
||||
return fs::movefile_and_open(branches_,branchpath_,fusepath_,origfd_);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue