mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-21 15:36:31 -05:00
Rework of runtime interface (#1498)
Also have special error handling for when branches are invalid and ENOENT would be returned for getattr and readdir so users understand what is going on and the runtime interface can still be used to fix the problem.
This commit is contained in:
parent
e87867cd2a
commit
2b500f194a
37 changed files with 3376 additions and 822 deletions
|
|
@ -14,187 +14,215 @@
|
|||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "fuse_setxattr.hpp"
|
||||
|
||||
#include "config.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fs_glob.hpp"
|
||||
#include "fs_lsetxattr.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "fs_statvfs_cache.hpp"
|
||||
#include "gidcache.hpp"
|
||||
#include "num.hpp"
|
||||
#include "policy_rv.hpp"
|
||||
#include "str.hpp"
|
||||
#include "ugid.hpp"
|
||||
#include "syslog.hpp"
|
||||
|
||||
#include "fuse.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
|
||||
static const char SECURITY_CAPABILITY[] = "security.capability";
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
|
||||
namespace l
|
||||
static
|
||||
int
|
||||
_setxattr_cmd_xattr(const std::string_view &attrname_,
|
||||
const std::string_view &attrval_)
|
||||
{
|
||||
static
|
||||
bool
|
||||
is_attrname_security_capability(const char *attrname_)
|
||||
{
|
||||
return (strcmp(attrname_,SECURITY_CAPABILITY) == 0);
|
||||
}
|
||||
std::string_view cmd;
|
||||
|
||||
static
|
||||
int
|
||||
setxattr_controlfile(const string &attrname_,
|
||||
const string &attrval_,
|
||||
const int flags_)
|
||||
{
|
||||
int rv;
|
||||
string key;
|
||||
Config::Write cfg;
|
||||
cmd = Config::prune_cmd_xattr(attrname_);
|
||||
|
||||
if(!str::startswith(attrname_,"user.mergerfs."))
|
||||
return -ENOATTR;
|
||||
SysLog::info("command requested: {}={}",cmd,attrval_);
|
||||
|
||||
key = &attrname_[14];
|
||||
if(cmd == "gc")
|
||||
return (fuse_gc(),0);
|
||||
if(cmd == "gc1")
|
||||
return (fuse_gc1(),0);
|
||||
if(cmd == "invalidate-all-nodes")
|
||||
return (fuse_invalidate_all_nodes(),0);
|
||||
if(cmd == "invalidate-gid-cache")
|
||||
return (GIDCache::invalidate_all(),0);
|
||||
if(cmd == "clear-gid-cache")
|
||||
return (GIDCache::clear_all(),0);
|
||||
|
||||
if(cfg->has_key(key) == false)
|
||||
return -ENOATTR;
|
||||
return -ENOATTR;
|
||||
}
|
||||
|
||||
if((flags_ & XATTR_CREATE) == XATTR_CREATE)
|
||||
return -EEXIST;
|
||||
static
|
||||
bool
|
||||
_is_attrname_security_capability(const char *attrname_)
|
||||
{
|
||||
return str::eq(attrname_,SECURITY_CAPABILITY);
|
||||
}
|
||||
|
||||
rv = cfg->set(key,attrval_);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
static
|
||||
int
|
||||
_setxattr_ctrl_file(const char *attrname_,
|
||||
const char *attrval_,
|
||||
size_t attrvalsize_,
|
||||
const int flags_)
|
||||
{
|
||||
int rv;
|
||||
std::string key;
|
||||
Config::Write cfg;
|
||||
|
||||
fs::statvfs_cache_timeout(cfg->cache_statfs);
|
||||
if(!Config::is_mergerfs_xattr(attrname_))
|
||||
return -ENOATTR;
|
||||
|
||||
if(Config::is_cmd_xattr(attrname_))
|
||||
return ::_setxattr_cmd_xattr(attrname_,
|
||||
std::string_view{attrval_,attrvalsize_});
|
||||
|
||||
key = Config::prune_ctrl_xattr(attrname_);
|
||||
|
||||
if(cfg->has_key(key) == false)
|
||||
return -ENOATTR;
|
||||
|
||||
if((flags_ & XATTR_CREATE) == XATTR_CREATE)
|
||||
return -EEXIST;
|
||||
|
||||
rv = cfg->set(key,attrval_);
|
||||
if(rv < 0)
|
||||
return rv;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
setxattr_loop_core(const string &basepath_,
|
||||
const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
const size_t attrvalsize_,
|
||||
const int flags_,
|
||||
PolicyRV *prv_)
|
||||
{
|
||||
string fullpath;
|
||||
fs::statvfs_cache_timeout(cfg->cache_statfs);
|
||||
|
||||
fullpath = fs::path::make(basepath_,fusepath_);
|
||||
|
||||
errno = 0;
|
||||
fs::lsetxattr(fullpath,attrname_,attrval_,attrvalsize_,flags_);
|
||||
|
||||
prv_->insert(errno,basepath_);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
setxattr_loop(const std::vector<Branch*> &branches_,
|
||||
const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
const size_t attrvalsize_,
|
||||
const int flags_,
|
||||
PolicyRV *prv_)
|
||||
{
|
||||
for(auto &branch : branches_)
|
||||
{
|
||||
l::setxattr_loop_core(branch->path,
|
||||
fusepath_,
|
||||
attrname_,
|
||||
attrval_,attrvalsize_,
|
||||
flags_,
|
||||
prv_);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
setxattr(const Policy::Action &setxattrPolicy_,
|
||||
const Policy::Search &getxattrPolicy_,
|
||||
const Branches &branches_,
|
||||
const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
const size_t attrvalsize_,
|
||||
const int flags_)
|
||||
{
|
||||
int rv;
|
||||
PolicyRV prv;
|
||||
std::vector<Branch*> branches;
|
||||
|
||||
rv = setxattrPolicy_(branches_,fusepath_,branches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
l::setxattr_loop(branches,fusepath_,attrname_,attrval_,attrvalsize_,flags_,&prv);
|
||||
if(prv.errors.empty())
|
||||
return 0;
|
||||
if(prv.successes.empty())
|
||||
return prv.errors[0].rv;
|
||||
|
||||
branches.clear();
|
||||
rv = getxattrPolicy_(branches_,fusepath_,branches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
return prv.get_error(branches[0]->path);
|
||||
}
|
||||
|
||||
int
|
||||
setxattr(const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
size_t attrvalsize_,
|
||||
int flags_)
|
||||
{
|
||||
Config::Read cfg;
|
||||
|
||||
if((cfg->security_capability == false) &&
|
||||
l::is_attrname_security_capability(attrname_))
|
||||
return -ENOATTR;
|
||||
|
||||
if(cfg->xattr.to_int())
|
||||
return -cfg->xattr.to_int();
|
||||
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
const ugid::Set ugid(fc->uid,fc->gid);
|
||||
|
||||
return l::setxattr(cfg->func.setxattr.policy,
|
||||
cfg->func.getxattr.policy,
|
||||
cfg->branches,
|
||||
fusepath_,
|
||||
attrname_,
|
||||
attrval_,
|
||||
attrvalsize_,
|
||||
flags_);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
namespace FUSE
|
||||
static
|
||||
void
|
||||
_setxattr_loop_core(const std::string &basepath_,
|
||||
const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
const size_t attrvalsize_,
|
||||
const int flags_,
|
||||
PolicyRV *prv_)
|
||||
{
|
||||
int
|
||||
setxattr(const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
size_t attrvalsize_,
|
||||
int flags_)
|
||||
{
|
||||
if(fusepath_ == CONTROLFILE)
|
||||
return l::setxattr_controlfile(attrname_,
|
||||
string(attrval_,attrvalsize_),
|
||||
flags_);
|
||||
std::string fullpath;
|
||||
|
||||
return l::setxattr(fusepath_,attrname_,attrval_,attrvalsize_,flags_);
|
||||
}
|
||||
fullpath = fs::path::make(basepath_,fusepath_);
|
||||
|
||||
errno = 0;
|
||||
fs::lsetxattr(fullpath,attrname_,attrval_,attrvalsize_,flags_);
|
||||
|
||||
prv_->insert(errno,basepath_);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
_setxattr_loop(const std::vector<Branch*> &branches_,
|
||||
const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
const size_t attrvalsize_,
|
||||
const int flags_,
|
||||
PolicyRV *prv_)
|
||||
{
|
||||
for(auto &branch : branches_)
|
||||
{
|
||||
::_setxattr_loop_core(branch->path,
|
||||
fusepath_,
|
||||
attrname_,
|
||||
attrval_,attrvalsize_,
|
||||
flags_,
|
||||
prv_);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_setxattr(const Policy::Action &setxattrPolicy_,
|
||||
const Policy::Search &getxattrPolicy_,
|
||||
const Branches &branches_,
|
||||
const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
const size_t attrvalsize_,
|
||||
const int flags_)
|
||||
{
|
||||
int rv;
|
||||
PolicyRV prv;
|
||||
std::vector<Branch*> branches;
|
||||
|
||||
rv = setxattrPolicy_(branches_,fusepath_,branches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
::_setxattr_loop(branches,fusepath_,attrname_,attrval_,attrvalsize_,flags_,&prv);
|
||||
if(prv.errors.empty())
|
||||
return 0;
|
||||
if(prv.successes.empty())
|
||||
return prv.errors[0].rv;
|
||||
|
||||
branches.clear();
|
||||
rv = getxattrPolicy_(branches_,fusepath_,branches);
|
||||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
return prv.get_error(branches[0]->path);
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
_setxattr(const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
size_t attrvalsize_,
|
||||
int flags_)
|
||||
{
|
||||
Config::Read cfg;
|
||||
|
||||
if((cfg->security_capability == false) &&
|
||||
::_is_attrname_security_capability(attrname_))
|
||||
return -ENOATTR;
|
||||
|
||||
if(cfg->xattr.to_int())
|
||||
return -cfg->xattr.to_int();
|
||||
|
||||
const fuse_context *fc = fuse_get_context();
|
||||
const ugid::Set ugid(fc->uid,fc->gid);
|
||||
|
||||
return ::_setxattr(cfg->func.setxattr.policy,
|
||||
cfg->func.getxattr.policy,
|
||||
cfg->branches,
|
||||
fusepath_,
|
||||
attrname_,
|
||||
attrval_,
|
||||
attrvalsize_,
|
||||
flags_);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
FUSE::setxattr(const char *fusepath_,
|
||||
const char *attrname_,
|
||||
const char *attrval_,
|
||||
size_t attrvalsize_,
|
||||
int flags_)
|
||||
{
|
||||
if(Config::is_ctrl_file(fusepath_))
|
||||
return ::_setxattr_ctrl_file(attrname_,
|
||||
attrval_,
|
||||
attrvalsize_,
|
||||
flags_);
|
||||
|
||||
return ::_setxattr(fusepath_,attrname_,attrval_,attrvalsize_,flags_);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue