From 28d809f2f8ff0da56e7601727fb152d15d67f20c Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Wed, 28 Oct 2026 11:28:26 -0500 Subject: [PATCH] Fix MSP create policy hangs when no branches qualify - Stop parent traversal after evaluating the empty relative root. - Preserve filtering errors, parent fallback, and closest-path selection. - Cover all four MSP policies with rejection and fallback regressions. - Document errors when traversal exhausts the branch root. --- .../config/functions_categories_policies.md | 8 +- src/policy_msplfs.cpp | 2 +- src/policy_msplus.cpp | 2 +- src/policy_mspmfs.cpp | 2 +- src/policy_msppfrd.cpp | 2 +- tests/tests.cpp | 161 ++++++++++++++++++ 6 files changed, 169 insertions(+), 8 deletions(-) diff --git a/mkdocs/docs/config/functions_categories_policies.md b/mkdocs/docs/config/functions_categories_policies.md index 1318dbb3..f306ecb2 100644 --- a/mkdocs/docs/config/functions_categories_policies.md +++ b/mkdocs/docs/config/functions_categories_policies.md @@ -119,10 +119,10 @@ but it makes things a bit more uniform. | lup (least used percent) | Pick the branch with the least used percentage of space | | lus (least used space) | Pick the branch with the least used space. | | all | Search: For **mkdir**, **mknod**, and **symlink** it will apply to all branches. **create** works like **ff**. | -| msppfrd (most shared path, percentage free random distribution) | Like **eppfrd** but if it fails to find a branch it will try again with the parent directory. Continues this pattern till finding one. | -| mspmfs (most shared path, most free space) | Like **epmfs** but if it fails to find a branch it will try again with the parent directory. Continues this pattern till finding one. | -| msplfs (most shared path, least free space) | Like **eplfs** but if it fails to find a branch it will try again with the parent directory. Continues this pattern till finding one. | -| msplus (most shared path, least used space) | Like **eplus** but if it fails to find a branch it will try again with the parent directory. Continues this pattern till finding one. | +| msppfrd (most shared path, percentage free random distribution) | Like **eppfrd** but if it fails to find a branch it will try again with the parent directory. Continues through the branch root, returning an error if no branch qualifies. | +| mspmfs (most shared path, most free space) | Like **epmfs** but if it fails to find a branch it will try again with the parent directory. Continues through the branch root, returning an error if no branch qualifies. | +| msplfs (most shared path, least free space) | Like **eplfs** but if it fails to find a branch it will try again with the parent directory. Continues through the branch root, returning an error if no branch qualifies. | +| msplus (most shared path, least used space) | Like **eplus** but if it fails to find a branch it will try again with the parent directory. Continues through the branch root, returning an error if no branch qualifies. | | eppfrd (existing path, percentage free random distribution) | Like **pfrd** but limited to existing paths. | | epmfs (existing path, most free space) | Of all the branches on which the relative path exists choose the branch with the most free space. | | eprand (existing path, random) | Calls **epall** and then randomizes. Returns 1. | diff --git a/src/policy_msplfs.cpp b/src/policy_msplfs.cpp index d3b88350..cbf08ae6 100644 --- a/src/policy_msplfs.cpp +++ b/src/policy_msplfs.cpp @@ -86,7 +86,7 @@ _create(const Branches::Ptr &branches_, branch = ::_create_1(branches_,fusepath,&error); if(branch) break; - if(fusepath == "/") + if(fusepath.empty()) break; fusepath = fusepath.parent_path(); } diff --git a/src/policy_msplus.cpp b/src/policy_msplus.cpp index 6ce2e58a..ddb85bee 100644 --- a/src/policy_msplus.cpp +++ b/src/policy_msplus.cpp @@ -83,7 +83,7 @@ _create(const Branches::Ptr &branches_, branch = ::_create_1(branches_,fusepath,&err); if(branch) break; - if(fusepath == "/") + if(fusepath.empty()) break; fusepath = fusepath.parent_path(); } diff --git a/src/policy_mspmfs.cpp b/src/policy_mspmfs.cpp index 0a090213..3f60f5a6 100644 --- a/src/policy_mspmfs.cpp +++ b/src/policy_mspmfs.cpp @@ -86,7 +86,7 @@ _create(const Branches::Ptr &branches_, branch = ::_create_1(branches_,fusepath,&error); if(branch) break; - if(fusepath == "/") + if(fusepath.empty()) break; fusepath = fusepath.parent_path(); diff --git a/src/policy_msppfrd.cpp b/src/policy_msppfrd.cpp index a5c349f2..68e6f638 100644 --- a/src/policy_msppfrd.cpp +++ b/src/policy_msppfrd.cpp @@ -91,7 +91,7 @@ _get_branchinfo(const Branches::Ptr &branches_, rv = ::_create_1(branches_,fusepath,branchinfo_,sum_); if(branchinfo_->size()) break; - if(fusepath == "/") + if(fusepath.empty()) break; fusepath = fusepath.parent_path(); } diff --git a/tests/tests.cpp b/tests/tests.cpp index 89ffee31..238f5c70 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -6,6 +6,7 @@ #include "from_string.hpp" #include "hashset.hpp" #include "num.hpp" +#include "policies.hpp" #include "rapidhash/rapidhash.h" #include "rnd.hpp" #include "str.hpp" @@ -18,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -2963,6 +2965,160 @@ test_config_prune_cmd_xattr() TEST_CHECK(Config::prune_cmd_xattr("") == sv("")); } +static +void +test_msp_create_rejected(const Policy::CreateImpl &policy_) +{ + char tmp_template[] = "/tmp/mergerfs-test-msp-rejected-XXXXXX"; + if(::mkdtemp(tmp_template) == nullptr) + { + TEST_CHECK(false); + return; + } + + const fs::path tmp_dir = tmp_template; + const fs::path first = tmp_dir / "first"; + const fs::path second = tmp_dir / "second"; + for(const auto &path : {first,second}) + { + std::error_code ec; + const bool created = std::filesystem::create_directory(path,ec); + TEST_CHECK(created && !ec); + if(!created || ec) + { + std::filesystem::remove_all(tmp_dir); + return; + } + } + + Branches branches; + branches.minfreespace = 0; + const int rv = branches.from_string(first.string() + ":" + second.string()); + TEST_CHECK(rv == 0); + Branches::Ptr ptr = branches; + TEST_CHECK(ptr->size() == 2); + if((rv != 0) || (ptr->size() != 2)) + { + std::filesystem::remove_all(tmp_dir); + return; + } + + for(const auto mode : {Branch::Mode::RW,Branch::Mode::RO,Branch::Mode::NC}) + { + for(auto &branch : *ptr) + { + branch.mode = mode; + branch.set_minfreespace(std::numeric_limits::max()); + } + + for(const auto &path : {fs::path("missing/child"),fs::path("")}) + { + std::vector selected; + const int expected = (mode == Branch::Mode::RW) ? -ENOSPC : -EROFS; + TEST_CHECK(policy_(ptr,path,selected) == expected); + TEST_MSG("policy: %s; path: '%s'; mode: %d", + policy_.name.c_str(),path.c_str(),static_cast(mode)); + TEST_CHECK(selected.empty()); + } + } + + std::filesystem::remove_all(tmp_dir); +} + +void +test_msppfrd_create_rejected_relative() +{ + test_msp_create_rejected(Policies::Create::msppfrd); +} + +void +test_mspmfs_create_rejected_relative() +{ + test_msp_create_rejected(Policies::Create::mspmfs); +} + +void +test_msplfs_create_rejected_relative() +{ + test_msp_create_rejected(Policies::Create::msplfs); +} + +void +test_msplus_create_rejected_relative() +{ + test_msp_create_rejected(Policies::Create::msplus); +} + +void +test_msp_create_relative_fallback() +{ + char tmp_template[] = "/tmp/mergerfs-test-msp-fallback-XXXXXX"; + if(::mkdtemp(tmp_template) == nullptr) + { + TEST_CHECK(false); + return; + } + + const fs::path tmp_dir = tmp_template; + const fs::path farther = tmp_dir / "farther"; + const fs::path closer = tmp_dir / "closer"; + for(const auto &path : {farther / "parent",closer / "parent/child", + farther / "rejected/child",closer / "rejected"}) + { + std::error_code ec; + const bool created = std::filesystem::create_directories(path,ec); + TEST_CHECK(created && !ec); + if(!created || ec) + { + std::filesystem::remove_all(tmp_dir); + return; + } + } + + Branches branches; + branches.minfreespace = 0; + const int rv = branches.from_string(farther.string() + ":" + closer.string()); + TEST_CHECK(rv == 0); + Branches::Ptr ptr = branches; + TEST_CHECK(ptr->size() == 2); + if((rv != 0) || (ptr->size() != 2)) + { + std::filesystem::remove_all(tmp_dir); + return; + } + + const Policy::CreateImpl *policies[] = { + &Policies::Create::msppfrd, + &Policies::Create::mspmfs, + &Policies::Create::msplfs, + &Policies::Create::msplus + }; + for(const auto policy : policies) + { + (*ptr)[0].set_minfreespace(std::numeric_limits::max()); + for(const auto &path : {fs::path("missing/child"),fs::path(""), + fs::path("rejected/child")}) + { + std::vector selected; + TEST_CHECK((*policy)(ptr,path,selected) == 0); + TEST_MSG("policy: %s; path: '%s'",policy->name.c_str(),path.c_str()); + TEST_CHECK(selected.size() == 1); + if(selected.size() == 1) + TEST_CHECK(selected[0] == &(*ptr)[1]); + } + + (*ptr)[0].set_minfreespace(0); + std::vector selected; + TEST_CHECK((*policy)(ptr,"parent/child/missing",selected) == 0); + TEST_MSG("policy: %s",policy->name.c_str()); + TEST_CHECK(selected.size() == 1); + if(selected.size() == 1) + TEST_CHECK(selected[0] == &(*ptr)[1]); + } + + std::filesystem::remove_all(tmp_dir); +} + void test_fs_copyfile_basic() { @@ -3657,6 +3813,11 @@ TEST_LIST = {"hashset_put_and_size",test_hashset_put_and_size}, {"rapidhash_withSeed_preserves_default_output",test_rapidhash_withSeed_preserves_default_output}, {"config_branches",test_config_branches}, + {"msppfrd_create_rejected_relative",test_msppfrd_create_rejected_relative}, + {"mspmfs_create_rejected_relative",test_mspmfs_create_rejected_relative}, + {"msplfs_create_rejected_relative",test_msplfs_create_rejected_relative}, + {"msplus_create_rejected_relative",test_msplus_create_rejected_relative}, + {"msp_create_relative_fallback",test_msp_create_relative_fallback}, {"branch_default_ctor_mode",test_branch_default_ctor_mode}, {"branch_to_string_modes",test_branch_to_string_modes}, {"branch_to_string_with_minfreespace",test_branch_to_string_with_minfreespace},