mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-11-26 01:46:31 -05:00
NFS open/creat hack
A hack to work around non-POSIX NFS <-> FUSE behavior where it turns an open(O_EXCL|O_CREAT,0444) into multiple calls that results in EACCES.
This commit is contained in:
parent
5a0568495b
commit
93218a343a
12 changed files with 280 additions and 18 deletions
|
|
@ -88,6 +88,7 @@ Config::Config()
|
|||
minfreespace(MINFREESPACE_DEFAULT),
|
||||
mount(),
|
||||
moveonenospc(false),
|
||||
nfsopenhack(NFSOpenHack::ENUM::OFF),
|
||||
nullrw(false),
|
||||
pid(::getpid()),
|
||||
posix_acl(false),
|
||||
|
|
@ -149,6 +150,7 @@ Config::Config()
|
|||
_map["minfreespace"] = &minfreespace;
|
||||
_map["mount"] = &mount;
|
||||
_map["moveonenospc"] = &moveonenospc;
|
||||
_map["nfsopenhack"] = &nfsopenhack;
|
||||
_map["nullrw"] = &nullrw;
|
||||
_map["pid"] = &pid;
|
||||
_map["posix_acl"] = &posix_acl;
|
||||
|
|
|
|||
|
|
@ -18,8 +18,9 @@
|
|||
|
||||
#include "branch.hpp"
|
||||
#include "config_inodecalc.hpp"
|
||||
#include "config_readdir.hpp"
|
||||
#include "config_moveonenospc.hpp"
|
||||
#include "config_nfsopenhack.hpp"
|
||||
#include "config_readdir.hpp"
|
||||
#include "enum.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "func_category.hpp"
|
||||
|
|
@ -111,6 +112,7 @@ public:
|
|||
ConfigUINT64 minfreespace;
|
||||
ConfigSTR mount;
|
||||
MoveOnENOSPC moveonenospc;
|
||||
NFSOpenHack nfsopenhack;
|
||||
ConfigBOOL nullrw;
|
||||
ConfigUINT64 pid;
|
||||
ConfigBOOL posix_acl;
|
||||
|
|
|
|||
53
src/config_nfsopenhack.cpp
Normal file
53
src/config_nfsopenhack.cpp
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "config_nfsopenhack.hpp"
|
||||
#include "ef.hpp"
|
||||
|
||||
template<>
|
||||
int
|
||||
NFSOpenHack::from_string(const std::string &s_)
|
||||
{
|
||||
if(s_ == "off")
|
||||
_data = NFSOpenHack::ENUM::OFF;
|
||||
ef(s_ == "git")
|
||||
_data = NFSOpenHack::ENUM::GIT;
|
||||
ef(s_ == "all")
|
||||
_data = NFSOpenHack::ENUM::ALL;
|
||||
else
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<>
|
||||
std::string
|
||||
NFSOpenHack::to_string(void) const
|
||||
{
|
||||
switch(_data)
|
||||
{
|
||||
case NFSOpenHack::ENUM::OFF:
|
||||
return "off";
|
||||
case NFSOpenHack::ENUM::GIT:
|
||||
return "git";
|
||||
case NFSOpenHack::ENUM::ALL:
|
||||
return "all";
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
30
src/config_nfsopenhack.hpp
Normal file
30
src/config_nfsopenhack.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "enum.hpp"
|
||||
|
||||
enum class NFSOpenHackEnum
|
||||
{
|
||||
OFF,
|
||||
GIT,
|
||||
ALL
|
||||
};
|
||||
|
||||
typedef Enum<NFSOpenHackEnum> NFSOpenHack;
|
||||
|
|
@ -25,14 +25,14 @@
|
|||
#include "fs_base_stat.hpp"
|
||||
#include "fs_base_unlink.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
static
|
||||
|
|
@ -63,7 +63,12 @@ namespace fs
|
|||
bool
|
||||
is_eligible(const int flags_)
|
||||
{
|
||||
return ((flags_ & O_RDWR) || (flags_ & O_WRONLY));
|
||||
int accmode;
|
||||
|
||||
accmode = (flags_ & O_ACCMODE);
|
||||
|
||||
return ((accmode == O_RDWR) ||
|
||||
(accmode == O_WRONLY));
|
||||
}
|
||||
|
||||
bool
|
||||
|
|
|
|||
|
|
@ -48,6 +48,23 @@ namespace fs
|
|||
base += suffix;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
string
|
||||
make(const char *base_,
|
||||
const char *suffix_)
|
||||
{
|
||||
char back;
|
||||
string path(base_);
|
||||
|
||||
back = *path.rbegin();
|
||||
if((back != '/') && (suffix_[0] != '/'))
|
||||
path.push_back('/');
|
||||
path += suffix_;
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
string
|
||||
|
|
|
|||
|
|
@ -21,11 +21,13 @@
|
|||
#include "rv.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include <fuse.h>
|
||||
#include "fuse.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@
|
|||
|
||||
#include <fuse.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace l
|
||||
{
|
||||
static
|
||||
|
|
|
|||
|
|
@ -17,13 +17,17 @@
|
|||
#include "config.hpp"
|
||||
#include "errno.hpp"
|
||||
#include "fileinfo.hpp"
|
||||
#include "fs_base_chmod.hpp"
|
||||
#include "fs_base_fchmod.hpp"
|
||||
#include "fs_base_open.hpp"
|
||||
#include "fs_base_stat.hpp"
|
||||
#include "fs_cow.hpp"
|
||||
#include "fs_path.hpp"
|
||||
#include "policy_cache.hpp"
|
||||
#include "stat_util.hpp"
|
||||
#include "ugid.hpp"
|
||||
|
||||
#include <fuse.h>
|
||||
#include "fuse.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
|
@ -34,6 +38,65 @@ typedef Config::CacheFiles CacheFiles;
|
|||
|
||||
namespace l
|
||||
{
|
||||
static
|
||||
bool
|
||||
rdonly(const int flags_)
|
||||
{
|
||||
return ((flags_ & O_ACCMODE) == O_RDONLY);
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
chmod_and_open_if_not_writable_and_empty(const string &fullpath_,
|
||||
const int flags_)
|
||||
{
|
||||
int rv;
|
||||
struct stat st;
|
||||
|
||||
rv = fs::lstat(fullpath_,&st);
|
||||
if(rv == -1)
|
||||
return (errno=EACCES,-1);
|
||||
|
||||
if(StatUtil::writable_or_not_empty(st))
|
||||
return (errno=EACCES,-1);
|
||||
|
||||
rv = fs::chmod(fullpath_,(st.st_mode|S_IWUSR|S_IWGRP));
|
||||
if(rv == -1)
|
||||
return (errno=EACCES,-1);
|
||||
|
||||
rv = fs::open(fullpath_,flags_);
|
||||
if(rv == -1)
|
||||
return (errno=EACCES,-1);
|
||||
|
||||
fs::fchmod(rv,st.st_mode);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static
|
||||
int
|
||||
nfsopenhack(const std::string &fullpath_,
|
||||
const int flags_,
|
||||
const NFSOpenHack nfsopenhack_)
|
||||
{
|
||||
switch(nfsopenhack_)
|
||||
{
|
||||
default:
|
||||
case NFSOpenHack::ENUM::OFF:
|
||||
return (errno=EACCES,-1);
|
||||
case NFSOpenHack::ENUM::GIT:
|
||||
if(l::rdonly(flags_))
|
||||
return (errno=EACCES,-1);
|
||||
if(fullpath_.find("/.git/") == string::npos)
|
||||
return (errno=EACCES,-1);
|
||||
return l::chmod_and_open_if_not_writable_and_empty(fullpath_,flags_);
|
||||
case NFSOpenHack::ENUM::ALL:
|
||||
if(l::rdonly(flags_))
|
||||
return (errno=EACCES,-1);
|
||||
return l::chmod_and_open_if_not_writable_and_empty(fullpath_,flags_);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
The kernel expects being able to issue read requests when running
|
||||
with writeback caching enabled so we must change O_WRONLY to
|
||||
|
|
@ -90,11 +153,12 @@ namespace l
|
|||
|
||||
static
|
||||
int
|
||||
open_core(const string &basepath_,
|
||||
const char *fusepath_,
|
||||
const int flags_,
|
||||
const bool link_cow_,
|
||||
uint64_t *fh_)
|
||||
open_core(const string &basepath_,
|
||||
const char *fusepath_,
|
||||
const int flags_,
|
||||
const bool link_cow_,
|
||||
const NFSOpenHack nfsopenhack_,
|
||||
uint64_t *fh_)
|
||||
{
|
||||
int fd;
|
||||
string fullpath;
|
||||
|
|
@ -105,6 +169,8 @@ namespace l
|
|||
fs::cow::break_link(fullpath.c_str());
|
||||
|
||||
fd = fs::open(fullpath,flags_);
|
||||
if((fd == -1) && (errno == EACCES))
|
||||
fd = l::nfsopenhack(fullpath,flags_,nfsopenhack_);
|
||||
if(fd == -1)
|
||||
return -errno;
|
||||
|
||||
|
|
@ -122,6 +188,7 @@ namespace l
|
|||
const char *fusepath_,
|
||||
const int flags_,
|
||||
const bool link_cow_,
|
||||
const NFSOpenHack nfsopenhack_,
|
||||
uint64_t *fh_)
|
||||
{
|
||||
int rv;
|
||||
|
|
@ -131,7 +198,7 @@ namespace l
|
|||
if(rv == -1)
|
||||
return -errno;
|
||||
|
||||
return l::open_core(basepath,fusepath_,flags_,link_cow_,fh_);
|
||||
return l::open_core(basepath,fusepath_,flags_,link_cow_,nfsopenhack_,fh_);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -157,6 +224,7 @@ namespace FUSE
|
|||
fusepath_,
|
||||
ffi_->flags,
|
||||
config.link_cow,
|
||||
config.nfsopenhack,
|
||||
&ffi_->fh);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
48
src/stat_util.hpp
Normal file
48
src/stat_util.hpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace StatUtil
|
||||
{
|
||||
static
|
||||
inline
|
||||
bool
|
||||
empty(const struct stat &st_)
|
||||
{
|
||||
return (st_.st_size == 0);
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
bool
|
||||
writable(const struct stat &st_)
|
||||
{
|
||||
return (st_.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH));
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
bool
|
||||
writable_or_not_empty(const struct stat &st_)
|
||||
{
|
||||
return (StatUtil::writable(st_) || !StatUtil::empty(st_));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue